使用Java和ffmpeg把音訊和視訊合成視訊的操作方法
FFmpeg是一個開源免費跨平臺的視訊和音訊流方案,屬於自由軟體,採用LGPL或GPL許可證(依據你選擇的元件)。它提供了錄製、轉換以及流化音視訊的完整解決方案。它包含了非常先進的音訊/視訊編解碼庫libavcodec,為了保證高可移植性和編解碼質量,libavcodec裡很多codec都是從頭開發的。
FFmpeg是一套可以用來記錄、轉換數字音訊、視訊,並能將其轉化為流的開源計算機程式。它包括了目前領先的音/視訊編碼庫libavcodec。 FFmpeg是在Linux下開發出來的,但它可以在包括Windows在內的大多數作業系統中編譯。這個專案是由Fabrice Bellard發起的,現在由Michael Niedermayer主持。可以輕易地實現多種視訊格式之間的相互轉換,例如可以將攝錄下的視訊avi等轉成現在視訊網站所採用的flv格式
主要功能:
1、視訊格式轉換功能
ffmpeg視訊轉換功能。視訊格式轉換,比如可以將多種視訊格式轉換為flv格式,可不是視訊訊號轉換 。
ffmpeg可以輕易地實現多種視訊格式之間的相互轉換(wma,rm,avi,mod等),例如可以將攝錄下的視訊avi等轉成現在視訊網站所採用的flv格式。
2、視訊截圖功能
對於選定的視訊,擷取指定時間的縮圖。視訊抓圖,獲取靜態圖和動態圖,不提倡抓gif檔案;因為抓出的gif檔案大而播放不流暢
3、給視訊加水印功能
使用ffmpeg 視訊新增水印(logo)。
好了,下面開始今天的正文。
藉助第三方工具ffmpeg合成視訊
需求:在小破站上下載了一些視訊,但是放到電腦裡面看,我擦,聲音檔案和視訊檔案是分開的。
- 正確安裝ffmpeg並配置好環境變數。
- Java程式碼測試
裡面是下載的視訊和音訊
我就上程式碼遞迴了,只要用正確的ffmpeg的命令和Java呼叫ffmpeg.exe的程式,就可以合成啦。
package com.lovely.test; import java.io.BufferedReader; import java.io.File; //import java.io.FileInputStream; //import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; /** * * 視訊中獲取音訊檔案 * */ public class TestFfmpeg { // FFmpeg全路徑 private static final String FFMPEG_PATH = "D:\\softWare\\tools\\joyTool\\ffmpeg\\bin\\ffmpeg.exe"; public static void main(String[] args) { String path = "E:\\StudyVedio\\ComputerScience\\US"; try { getAll(path); } catch (Exception e) { e.printStackTrace(); } } /** * 具體合成視訊函式 * @param videoInputPath * 原視訊的全路徑 * * @param audioInputPath * 音訊的全路徑 * * @param videoOutPath * 視訊與音訊結合之後的視訊的路徑 */ public static void convetor(String videoInputPath,String audioInputPath,String videoOutPath) throws Exception { Process process = null; InputStream errorStream = null; InputStreamReader inputStreamReader = null; BufferedReader br = null; try { // ffmpeg命令 String command = FFMPEG_PATH + " -i " + videoInputPath + " -i " + audioInputPath + " -c:v copy -c:a aac -strict experimental " + " -map 0:v:0 -map 1:a:0 " + " -y " + videoOutPath; process = Runtime.getRuntime().exec(command); errorStream = process.getErrorStream(); inputStreamReader = new InputStreamReader(errorStream); br = new BufferedReader(inputStreamReader); // 用來收集錯誤資訊的 String str = ""; while ((str = br.readLine()) != null) { System.out.println(str); } process.waitFor(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } } // 遞迴函式 public static void getAll(String path) throws Exception { String videoInputPath = ""; String audioInputPath = ""; String videoOutPath = ""; File file = new File(path); if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { getAll(f.getPath()); if (f.isFile()) { if (f.getName().endsWith(".m4s")) { if (f.getName().endsWith("audio.m4s")) audioInputPath = file.getPath() + "\\audio.m4s"; if (f.getName().endsWith("video.m4s")) videoInputPath = file.getPath() + "\\video.m4s"; videoOutPath = file.getPath() + "\\all.mp4"; if (!videoInputPath.equals("")) convetor(videoInputPath,audioInputPath,videoOutPath); } } } } } }
我最後用了好幾分鐘合成了30個完整的視訊。體會了遞迴的強大。
總結
到此這篇關於使用Java和ffmpeg把音訊和視訊合成視訊的操作方法的文章就介紹到這了,更多相關java ffmpeg音訊合成視訊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!