Java ffmpeg 合成音視訊檔案
阿新 • • 發佈:2020-12-26
之前有做個一個短視訊的專案,一直沒有動,最近試著去看的時候 ,發現新版的音視訊命令和之前的有些不一樣,所以這裡做一下筆記,記錄一下。
首先,我們來看一下下載路徑
http://ffmpeg.org/download.html
這裡選擇下載的window版本,下載之後解壓就可以用了。
接下來,我們來看具體操作。
首先舊版本 我們合成音視訊檔案 的命令:
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(); } } }
好了,到這裡,測試一下 就可以看到可以了,另外需要注意就是,如果通過應用沒有生成,建議將命令複製,然後在命令太執行一下,他會告訴你錯誤在哪裡。