ffmpeg在不去除原声的情况下加上背景音乐

我的未来我决定 提交于 2019-12-16 08:11:48

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();
		}
		
	}
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!