package cn.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Ffmpeg {
public static void main(String[] args) {
// ffmpeg -i rtsp://127.0.0.1/vod/mp4://BigBuckBunny_175k.mov -r 0.2 d:\\1\\images%05d.jpg
List<String> commend = new ArrayList<String>();
commend.add("ffmpeg");
commend.add("-i");
commend.add("rtmp:http://127.0.0.1/vod/mp4://BigBuckBunny_175k.mov");//视频
commend.add("-y");
commend.add("-f");
commend.add("image2");
commend.add("-r");//添加参数“-r",该参数指定截取图片间隔时间
commend.add("0.2");//添加参数“-r",该参数指定截取图片间隔2秒
// commend.add("-ss");// 添加参数"-ss",该参数指定截取的起始时间
// commend.add("10");// 添加起始时间为第10秒
// commend.add("-t");// 添加参数"-t",该参数指定持续时间
// commend.add("0.001");// 添加持续时间为1毫秒
commend.add("-s"); // 添加参数"-s",该参数指定截取的图片大小
commend.add("800*280"); // 添加截取的图片大小为350*240
commend.add("d:\\1\\images%05d.jpg");
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
try {
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in = process.getInputStream();
System.out.println("正在进行截图,请稍候=======================");
convertStreamToString(in);
InputStream errorStream = process.getErrorStream();
if (errorStream != null && errorStream.read() > 0) {
System.out.println("错误:");
convertStreamToString(errorStream);
}
in.close();
} catch (IOException e) {
System.out.println("错误:");
e.printStackTrace();
}
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}