FFmpeg保留原声音视频合成

官方下载ffmpeg.

我的版本

1、cmd命令:

我的版本:我的版本
命令:

ffmpeg.exe -i input.mp4 -i bgm.mp3 -t 6 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 output.mp4

ffmpeg.exe -i input.mp4 -i bgm.mp3 -t 6 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 output.mp4

2、java实现

package com.tang.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *@author Tang
 *@Description:音视频合成
**/
public class MergeVideoMp3 {
	
	private String ffmpegEXE;
	public MergeVideoMp3(String ffmpegEXE) {
		super();
		this.ffmpegEXE = ffmpegEXE;
	}
	
	public void convertor(String mp3InputPath,String videoInputPath, 
						  double seconds, String videoOutputPath )throws Exception {
//ffmpeg.exe -i input.mp4 -i bgm.mp3 -t 6 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 output.mp4
		
		List<String> command = new ArrayList<>();
		command.add(ffmpegEXE);
		
		command.add("-i");
		command.add(videoInputPath);
		command.add("-i");
		command.add(mp3InputPath);
		command.add("-t");
		command.add(String.valueOf(seconds));
		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();
		}
		
	}
	

	public static void main(String[] args) {
		MergeVideoMp3 ffmpeg = new MergeVideoMp3("D:\\FFmpeg\\bin\\ffmpeg.exe");
		try {
			ffmpeg.convertor("E:\\videos\\twocats.mp4", "E:\\VideosSpace\\bgm\\野摩托.mp3",6.0,"E:\\videos\\new-twocats.mp4");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值