Java ffmpeg 合成音视频文件
之前有做个一个短视频的项目,一直没有动,最近试着去看的时候 ,发现新版的音视频命令和之前的有些不一样,所以这里做一下笔记,记录一下。
首先,我们来看一下下载路径
http://ffmpeg.org/download.html
这里选择下载的window版本,下载之后解压就可以用了。
接下来,我们来看具体操作。
首先旧版本 我们合成音视频文件 的命令:
1 | ffmpeg.exe -i 苏州大裤衩.mp4 -i bgm.mp3 -t 7 -y 新的视频.mp4 |
可以看到 一个命令就ok的
代码:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class MergeVideoMp3 { private String ffmpegEXE; public MergeVideoMp3(String ffmpegEXE) { super(); this.ffmpegEXE = ffmpegEXE; } public void convertor(String videoInputPath, String mp3InputPath, double seconds, String videoOutputPath) throws Exception { // ffmpeg.exe -i 苏州大裤衩.mp4 -i bgm.mp3 -t 7 -y 新的视频.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("-y"); 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("C:\\ffmpeg\\bin\\ffmpeg.exe"); try { ffmpeg.convertor("C:\\苏州大裤衩.mp4", "C:\\music.mp3", 7.1, "C:\\这是通过java生产的视频.mp4"); } catch (Exception e) { e.printStackTrace(); } } }
但是发现不行,后来从新找了新命令
//去除视屏背景音乐 ffmpeg.exe -i 吃鸡.mp4 -vcodec copy -an 吃鸡1.mp4 //合成去除背景音乐的视屏和mp3文件 ffmpeg.exe -i 吃鸡1.mp4 -i 小太阳.mp3 -t 10 -y result.mp4
代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class MergeVideoMp4 { private String ffmpegEXE; public MergeVideoMp4(String ffmpegEXE) { super(); this.ffmpegEXE = ffmpegEXE; } public void convertor(String videoInputPath, String videoTmpPath, String mp3InputPath, double seconds, String videoOutputPath) throws Exception { //去除视屏背景音乐 ffmpeg.exe -i 吃鸡.mp4 -vcodec copy -an 吃鸡1.mp4 List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i");//输入 command.add(videoInputPath); command.add("-vcodec");//分离视屏 分离音频:-acodec copy -vn command.add("copy"); command.add("-an"); // 去掉音频 command.add(videoTmpPath); for (String c : command) { System.out.print(c + " "); } this.processed(command); //合成去除背景音乐的视屏和mp3文件 ffmpeg.exe -i 吃鸡1.mp4 -i 小太阳.mp3 -t 10 -y result.mp4 List<String> command2 = new ArrayList<>(); command2.add(ffmpegEXE); command2.add("-i"); command2.add(videoTmpPath); command2.add("-i"); command2.add(mp3InputPath); command2.add("-t"); //指定视屏时间 command2.add(String.valueOf(seconds)); command2.add("-y"); // 当已存在输出文件时,不提示是否覆盖 command2.add(videoOutputPath); System.out.println(); for (String c : command2) { System.out.print(c + " "); } this.processed(command2); } /** * 执行FFmpeg命令 * @param command * @throws IOException */ private void processed(List<String> command){ ProcessBuilder builder = new ProcessBuilder(command); Process process; InputStream errorStream = null; InputStreamReader inputStreamReader = null; BufferedReader br = null; try { process = builder.start(); errorStream = process.getErrorStream(); inputStreamReader = new InputStreamReader(errorStream); br = new BufferedReader(inputStreamReader); String line = ""; while ( (line = br.readLine()) != null ) { } } catch (IOException e) { e.printStackTrace(); }finally { try { if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { MergeVideoMp4 ffmpeg = new MergeVideoMp4("C:\\ffmpeg\\bin\\ffmpeg.exe"); try { ffmpeg.convertor("D:\\吃鸡(0).mp4", "D:\\吃鸡.mp4","D:\\小太阳.mp3", 7.1, "D:\\这是通过java生产的视频.mp4"); } catch (Exception e) { e.printStackTrace(); } } }
好了,到这里,测试一下 就可以看到可以了,另外需要注意就是,如果通过应用没有生成,建议将命令复制,然后在命令太执行一下,他会告诉你错误在哪里。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步