1.无原声合并音视频 ffmpeg -i bgm.mp3 -i input.mp4 output.mp4
2.保留原声合并音视频 ffmpeg -i bgm.mp3 -i input.mp4 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 output.mp4
(注意:inputs=输入流数量, duration=决定流的结束,dropout_transition= 输入流结束时,容量重整时间, (longest最长输入时间,shortest最短,first第一个输入持续的时间)) 注意: 音频,视频输入流的命令顺序可能对视频合成有影响(猜测) (更多请查看ffmpeg官方文档) ps:我是看到这位pjqdyd 老哥的回答。
示例JAVA代码如下~
public class MergeVideoMp3 {
private String ffmpegEXE;
public MergeVideoMp3(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
//ffmpeg -i bgm.mp3 -i input.mp4 -t 6 -filter_complex amix=inputs=2 output.mp4
/**
* 注意:inputs=输入流数量, duration=决定流的结束,
* dropout_transition= 输入流结束时,容量重整时间,
* (longest最长输入时间,shortest最短,first第一个输入持续的时间))
* @throws Exception
*/
public void convertor(String videoInputPath, String mp3InputPath,
double seconds, String videoOutputPath) throws Exception {
// ffmpeg.exe -i 1.mp4 -i bgm.mp3 -t 7 -y 2.mp4
System.out.print(mp3InputPath);
List<String> command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(mp3InputPath);
command.add("-i");
command.add(videoInputPath);
command.add("-t");
command.add(String.valueOf(seconds));
//command.add("-y");
command.add("-filter_complex");
command.add("amix=inputs=2:duration=first:dropout_transition=2");
command.add(videoOutputPath);
// for (String c : command) {
// System.out.print(c + " ");
// }
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ( (line = br.readLine()) != null ) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
}
来源:CSDN
作者:wantLight
链接:https://blog.csdn.net/wantLight/article/details/103459713