Java代码使用ffmpeg视频按帧存储

来源:互联网 发布:Ubuntu配置caffe CPU 编辑:程序博客网 时间:2024/05/21 09:00

public class VideoThumbTaker
{
protected static String ffmpegApp;

public VideoThumbTaker(String ffmpegApp){    this.ffmpegApp = ffmpegApp;}

@SuppressWarnings(“unused”)
/**
* 获取指定时间内的图片
* @param videoFilename:视频路径
* @param thumbFilename:图片保存路径
* @param width:图片长
* @param height:图片宽
* @param hour:指定时
* @param min:指定分
* @param sec:指定秒
* @throws IOException
* @throws InterruptedException
*/

public void V2PAll(String videoFilename, String thumbFilename,int width,
int height,int hour, int min,float sec) throws IOException, InterruptedException{
System.out.printf(“%d:%d:%.3f\n”,hour,min,sec);
System.out.println(“first enter into V2PAll!”);
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp,”-y”,
“-i”, videoFilename, thumbFilename+”1.jpg”,
“-ss”,”10”, “-t” , “0.001”,
//”-f”,”mjpeg”,//hour , “:” + min + “:” + sec,
“-s” , width + “*” + height
//”-vframes”, “200”
);
System.out.println(processBuilder.command());

   Process process = processBuilder.start();   System.out.println(process);   System.out.println("process has get start...");   InputStream stderr = process.getErrorStream();   InputStreamReader isr = new InputStreamReader(stderr);   BufferedReader br = new BufferedReader(isr);   String line;   //这里出现问题!!   while ((line = br.readLine()) !=null) {       System.out.println(line);       ;   }   process.waitFor();   if(br != null)       br.close();   if(isr != null)       isr.close();   if(stderr !=null)       stderr.close();

}

public void getThumb(String videoFilename, String thumbFilename,int width,        int height,float sec) throws IOException,        InterruptedException{    ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp,"-y",            "-i", videoFilename, "-vframes", "1", "-ss",""+ sec*0.04,            "-f", "mjpeg", "-s", width + "*" + height,            "-an", thumbFilename);    Process process = processBuilder.start();    InputStream stderr = process.getErrorStream();    InputStreamReader isr = new InputStreamReader(stderr);    BufferedReader br = new BufferedReader(isr);    String line;    while ((line = br.readLine()) !=null)        ;    process.waitFor();    if(br != null)        br.close();    if(isr != null)        isr.close();    if(stderr !=null)        stderr.close();}public static void main(String[] args){    VideoThumbTaker videoThumbTaker =             new VideoThumbTaker("D:\\ffmpeg\\ffmpeg.exe");    VideoInfo videoInfo = new VideoInfo("D:\\ffmpeg\\ffmpeg.exe",            "C:/Users/ASUS-PC/Desktop/2.mp4");    System.out.println("videoName : " + videoInfo.getVideoFilename());    try {        System.out.println("get Here!");        videoInfo.getInfo();    }catch (Exception e) {        System.out.println("This is an error!!");        e.printStackTrace();    }    System.out.println("Seconds: " + (int)videoInfo.getSeconds());    System.out.println("Path: " + videoInfo.getVideoFilename());    int width = videoInfo.getWidth();    int height = videoInfo.getHeigt();    System.out.println("Width:" + width + " ; Height :" + height);    int hour = videoInfo.getHours();    int min = videoInfo.getMinutes();    float sec = videoInfo.getSeconds();    System.out.printf("Hour:Min:Sec %d:%d:%.3f\n",hour,min,sec);    sec = hour*3600 + min*60 + sec;    for(int i = 0; i < (int)sec*25; i++) {              try        {            videoThumbTaker.getThumb("C:/Users/ASUS-PC/Desktop/2.mp4",                     "C:\\Users\\ASUS-PC\\Desktop\\Video\\"+i+".jpg",                     width, height, i);        } catch (Exception e){            e.printStackTrace();        }    }    /*try    {        videoThumbTaker.V2PAll("C:/Users/ASUS-PC/Desktop/2.mp4",                 "C:\\Users\\ASUS-PC\\Desktop\\Video\\",                 width, height, hour, min, sec);        System.out.println("get True!");    } catch (Exception e){        System.out.println("Or get False ! Error");        e.printStackTrace();    }    */    System.out.println("over");}

}

原创粉丝点击