Java使用ffmpeg对音频和视频进行合并。
需求:将多个avi格式视频跟一个mp3文件进行合并为mp4格式的视频。
步骤:
- 将多个avi合并为一个;
- 将合并后的avi跟mp3合并为一个avi;
- 将avi转换为mp4格式
/**
* @author:四个空格-https://www.4spaces.org
* 合并多个Avi文件
* @param ffmpegPath 合并工具路径
* @param txtPath 待合并的文件每行一个保存的txt文件里
* @param outPath 合并后的avi保存路径
*/
public void mergeAvis(String ffmpegPath,String txtPath,String outPath){
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-f");//合成音视频
commend.add("concat");
commend.add("-i");
commend.add(outPath);
commend.add("-c");
commend.add("copy");
commend.add(outPath);
try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将avi格式的视频转换为mp4格式
* @param ffmpegPath 合并工具路径
* @param aviPath 待转换的avi路径
* @param mp4Path 转换后的mp4保存路径
*/
public void changeAviToMp4(String ffmpegPath,String aviPath,String mp4Path){
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(aviPath);
commend.add(mp4Path);
try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 实现视频(avi)和音频(mp3)的合并
* @param ffmpegPath 合并工具路径
* @param mp3Path 需要合并的mp3路径
* @param aviPath 需要合并的avi路径
* @param outPath 合并后的avi保存路径
*/
public void mergeVideoAndAudio(String ffmpegPath,String mp3Path,String aviPath,String outPath){
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(mp3Path);
commend.add("-i");
commend.add(aviPath);
commend.add("-acodec");
commend.add("copy");
commend.add("-vcodec");
commend.add("copy");
commend.add(outPath);
try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createAviTextFile(String textFilePath) {
// 首先判断是否存在,不存在则创建
File file = new File(textFilePath);
if (!file.exists()) {
try {
if (file.createNewFile()) {
System.out.println("创建单个文件" + textFilePath + "成功!");
} else {
System.out.println("创建单个文件" + textFilePath + "失败!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 将要合并的avi写入txt文件(格式:file '/path/to/yourfile/video-00001.ts'),单个写入,每行一个
* @param textFilePath
*/
public void writeAviToTextFile(String textFilePath,String content) {
try {
File writename = new File(textFilePath);
writename.createNewFile();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(writename, true)));
out.write(content+"\r\n");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
待合并视频文件示例txt
file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00001.ts'
file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00002.ts'
file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00003.ts'
file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00004.ts'
file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00005.ts'
参考:https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html
这篇文章里有记录:https://www.4spaces.org/java-lang-processbuilder-usage/