C:\workspace\project\greejoy\picManager\web\tools\ffmpeg\bin\ffmpeg.exe -i C:\Users\Dulk\Desktop\ukulele\01\《小星星》.mp4 -f image2 -ss 10 -t 0.001 -s 320*240 C:\Users\Dulk\Desktop\ukulele\01\littleStar.jpg
C:\workspace\project\greejoy\picManager\web\tools\ffmpeg\bin\ffmpeg.exe -i C:\Users\Dulk\Desktop\ukulele\01\《小星星》.mp4 -f image2 -ss 10 -t 0.001 -s 320*240 C:\Users\Dulk\Desktop\ukulele\01\littleStar.jpg
public class FFmpegTest { public static void main(String[] args) { String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe"; String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4"; String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJava.jpg"; List<String> command = new ArrayList<String>(); command.add(ffmpegExePath); command.add("-i"); command.add(inputFilePath); command.add("-f"); command.add("image2"); command.add("-ss"); command.add("10"); command.add("-t"); command.add("0.001"); command.add("-s"); command.add("320*240"); command.add(outputFilePath); ProcessBuilder builder = new ProcessBuilder(); builder.command(command); //正常信息和錯誤信息合併輸出 builder.redirectErrorStream(true); try { //開始執行命令 Process process = builder.start(); //若是你想獲取到執行完後的信息,那麼下面的代碼也是須要的 StringBuffer sbf = new StringBuffer(); String line = null; BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = br.readLine()) != null) { sbf.append(line); sbf.append(" "); } String resultInfo = sbf.toString(); System.out.println(resultInfo); } catch (IOException e) { e.printStackTrace(); } } }
public class FFmpegTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJava.jpg";
List<String> command = new ArrayList<String>();
command.add(ffmpegExePath);
command.add("-i");
command.add(inputFilePath);
command.add("-f");
command.add("image2");
command.add("-ss");
command.add("10");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("320*240");
command.add(outputFilePath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
//正常信息和錯誤信息合併輸出
builder.redirectErrorStream(true);
try {
//開始執行命令
Process process = builder.start();
//若是你想獲取到執行完後的信息,那麼下面的代碼也是須要的
StringBuffer sbf = new StringBuffer();
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
String resultInfo = sbf.toString();
System.out.println(resultInfo);
} catch (IOException e) {
e.printStackTrace();
}
}
}
List<String> command = new ArrayList<String>(); command.add(ffmpegExePath); command.add("-i"); command.add(inputFilePath); command.add("-f"); command.add("image2"); command.add("-ss"); command.add("10"); command.add("-t"); command.add("0.001"); command.add("-s"); command.add("320*240"); command.add(outputFilePath);
List<String> command = new ArrayList<String>();
command.add(ffmpegExePath);
command.add("-i");
command.add(inputFilePath);
command.add("-f");
command.add("image2");
command.add("-ss");
command.add("10");
command.add("-t");
command.add("0.001");
command.add("-s");
command.add("320*240");
command.add(outputFilePath);
/** * FFmpeg命令的封裝類 */ public class FFmpegCommand { private List<String> command; public FFmpegCommand(List<String> command) { this.command = command == null ? new ArrayList<String>() : command; } public List<String> getCommand() { return command; } public void setCommand(List<String> command) { this.command = command; } /** * 開始執行命令 * * @param callback 回調 * @return 命令的信息輸出 * @throws FFmpegCommandException */ public String start(FFmpegCallback callback) throws FFmpegCommandException { BufferedReader br = null; StringBuffer sbf = new StringBuffer(); String resultInfo = null; try { ProcessBuilder builder = new ProcessBuilder(); builder.command(command); //正常信息和錯誤信息合併輸出 builder.redirectErrorStream(true); //開啓執行子線程 Process process = builder.start(); String line = null; br = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = br.readLine()) != null) { sbf.append(line); sbf.append(" "); } resultInfo = sbf.toString(); //等待命令子線程執行完成 int exitValue = process.waitFor(); //完成後執行回調 if (exitValue == 0 && callback != null) { callback.complete(resultInfo); } //銷燬子線程 process.destroy(); } catch (IOException e) { e.printStackTrace(); throw new FFmpegCommandException(e.getMessage()); } catch (InterruptedException e) { e.printStackTrace(); throw new FFmpegCommandException("線程阻塞異常:" + e.getMessage()); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } return resultInfo; } }
/**
* FFmpeg命令的封裝類
*/
public class FFmpegCommand {
private List<String> command;
public FFmpegCommand(List<String> command) {
this.command = command == null ? new ArrayList<String>() : command;
}
public List<String> getCommand() {
return command;
}
public void setCommand(List<String> command) {
this.command = command;
}
/**
* 開始執行命令
*
* @param callback 回調
* @return 命令的信息輸出
* @throws FFmpegCommandException
*/
public String start(FFmpegCallback callback) throws FFmpegCommandException {
BufferedReader br = null;
StringBuffer sbf = new StringBuffer();
String resultInfo = null;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
//正常信息和錯誤信息合併輸出
builder.redirectErrorStream(true);
//開啓執行子線程
Process process = builder.start();
String line = null;
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append(" ");
}
resultInfo = sbf.toString();
//等待命令子線程執行完成
int exitValue = process.waitFor();
//完成後執行回調
if (exitValue == 0 && callback != null) {
callback.complete(resultInfo);
}
//銷燬子線程
process.destroy();
} catch (IOException e) {
e.printStackTrace();
throw new FFmpegCommandException(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
throw new FFmpegCommandException("線程阻塞異常:" + e.getMessage());
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultInfo;
}
}
public class FFmpegCommandBuilder { List<String> command = new ArrayList<String>(); public FFmpegCommandBuilder(String exePath) { if (exePath == null) { throw new FFmpegCommandRuntimeException("ffmpeg.exe 路徑不得爲空"); } //添加命令的exe執行文件位置 command.add(exePath); } /** * 添加輸入文件的路徑 * * @param inputFilePath */ public FFmpegCommandBuilder input(String inputFilePath) { if (inputFilePath != null) { command.add("-i"); command.add(inputFilePath); } return this; } /** * 添加輸出文件的路徑 * * @param outputFilePath */ public FFmpegCommandBuilder output(String outputFilePath) { if (outputFilePath != null) { command.add(outputFilePath); } return this; } /** * 覆蓋輸出文件 */ public FFmpegCommandBuilder override() { command.add("-y"); return this; } /** * 強制輸出格式 * * @param format 輸出格式 */ public FFmpegCommandBuilder format(FFmpegCommandFormatEnum format) { if (format != null) { command.add("-f"); command.add(format.getValue()); } return this; } /** * 設置錄製/轉碼的時長 * * @param duration 形如 0.001 表示0.001秒,hh:mm:ss[.xxx]格式的記錄時間也支持 */ public FFmpegCommandBuilder duration(String duration) { if (duration != null) { command.add("-t"); command.add(duration); } return this; } /** * 搜索到指定的起始時間 * * @param position 形如 17 表示17秒,[-]hh:mm:ss[.xxx]的格式也支持 */ public FFmpegCommandBuilder position(String position) { if (position != null) { command.add("-ss"); command.add(position); } return this; } /** * 設置幀大小 * * @param size 形如 xxx*xxx * @return */ public FFmpegCommandBuilder size(String size) { if (size != null) { command.add("-s"); command.add(size); } return this; } /** * 建立FFmpegCommand命令封裝類 * * @return FFmpegCommand */ public FFmpegCommand build() { return new FFmpegCommand(command); } }
public class FFmpegCommandBuilder {
List<String> command = new ArrayList<String>();
public FFmpegCommandBuilder(String exePath) {
if (exePath == null) {
throw new FFmpegCommandRuntimeException("ffmpeg.exe 路徑不得爲空");
}
//添加命令的exe執行文件位置
command.add(exePath);
}
/**
* 添加輸入文件的路徑
*
* @param inputFilePath
*/
public FFmpegCommandBuilder input(String inputFilePath) {
if (inputFilePath != null) {
command.add("-i");
command.add(inputFilePath);
}
return this;
}
/**
* 添加輸出文件的路徑
*
* @param outputFilePath
*/
public FFmpegCommandBuilder output(String outputFilePath) {
if (outputFilePath != null) {
command.add(outputFilePath);
}
return this;
}
/**
* 覆蓋輸出文件
*/
public FFmpegCommandBuilder override() {
command.add("-y");
return this;
}
/**
* 強制輸出格式
*
* @param format 輸出格式
*/
public FFmpegCommandBuilder format(FFmpegCommandFormatEnum format) {
if (format != null) {
command.add("-f");
command.add(format.getValue());
}
return this;
}
/**
* 設置錄製/轉碼的時長
*
* @param duration 形如 0.001 表示0.001秒,hh:mm:ss[.xxx]格式的記錄時間也支持
*/
public FFmpegCommandBuilder duration(String duration) {
if (duration != null) {
command.add("-t");
command.add(duration);
}
return this;
}
/**
* 搜索到指定的起始時間
*
* @param position 形如 17 表示17秒,[-]hh:mm:ss[.xxx]的格式也支持
*/
public FFmpegCommandBuilder position(String position) {
if (position != null) {
command.add("-ss");
command.add(position);
}
return this;
}
/**
* 設置幀大小
*
* @param size 形如 xxx*xxx
* @return
*/
public FFmpegCommandBuilder size(String size) {
if (size != null) {
command.add("-s");
command.add(size);
}
return this;
}
/**
* 建立FFmpegCommand命令封裝類
*
* @return FFmpegCommand
*/
public FFmpegCommand build() {
return new FFmpegCommand(command);
}
}
public class FFmpegBuilderTest { public static void main(String[] args) { String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe"; String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4"; String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJavaBuilder.jpg"; FFmpegCommandBuilder builder = new FFmpegCommandBuilder(ffmpegExePath); builder.input(inputFilePath).format(FFmpegCommandFormatEnum.IMAGE) .position("10").duration("0.001").size("320*240").output(outputFilePath); FFmpegCommand command = builder.build(); try { String result = command.start(null); System.out.println(result); } catch (FFmpegCommandException e) { e.printStackTrace(); } } }
public class FFmpegBuilderTest {
public static void main(String[] args) {
String ffmpegExePath = "C:\\workspace\\project\\greejoy\\picManager\\web\\tools\\ffmpeg\\bin\\ffmpeg.exe";
String inputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\《小星星》.mp4";
String outputFilePath = "C:\\Users\\Dulk\\Desktop\\ukulele\\01\\littleStarJavaBuilder.jpg";
FFmpegCommandBuilder builder = new FFmpegCommandBuilder(ffmpegExePath);
builder.input(inputFilePath).format(FFmpegCommandFormatEnum.IMAGE)
.position("10").duration("0.001").size("320*240").output(outputFilePath);
FFmpegCommand command = builder.build();
try {
String result = command.start(null);
System.out.println(result);
} catch (FFmpegCommandException e) {
e.printStackTrace();
}
}
}