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