簡介
FFmpeg 是一個跨平臺的音視頻處理工具庫/命令行工具,其核心作用是:對音視頻文件或流進行解碼、轉換(編碼)、封裝/解封裝等處理。
友情提示
本次安裝以Windows64位操作系統為例
一、下載及安裝
1、前往FFmpeg官網,點擊下載
這里我們以Windows builds from gyan.dev為例
2、進來以后選擇release builds
這邊我們選擇右邊的1或者2都可,選擇其一就可以然后shared和不帶shared主要區別就是戴shared是把依賴包全部放在lib文件下面然后ffmpeg.exe,ffplay.exe,ffprobe.exe作為可執行文件就可以直接去lib里面調用相應功能,而不帶shared的他的ffmpeg.exe,ffplay.exe,ffprobe.exe作為獨立的執行的單獨模塊會在bin得目錄下,每個文件就會大一些。這里我們以下載ffmpeg-7.0.2-full_build.7z為例
3、下載解壓完成后我們就得到了如下的目錄
點擊bin目錄我們就可以看見那三個主要的文件
二、環境變量設置
1、右擊此電腦選擇屬性
2、選擇高級系統設置,選擇環境變量
3、在系統變量中選擇path進行編輯
4、將bin的路徑直接復制過去,然后一定要把所有確定按鈕點完
5、檢驗是否生效
cmd輸入ffmpeg -version 如果出現版本號就說明配置成功
三、Java代碼中ffmpeg的調用
ffmpeg過于強大,這里咱們就舉幾個簡單例子展示一下ffmpeg的基本功能!
1、獲取視頻信息
public static void main(String[] args) {try {// 獲取視頻信息getVideoInfo("D:/video/lin.mp4");} catch (IOException | InterruptedException e) {e.printStackTrace();}}public static void getVideoInfo(String videoPath) throws IOException, InterruptedException {ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoPath);Process process = processBuilder.start();process.waitFor();printCommandOutput(process.getErrorStream());}private static void printCommandOutput(InputStream inputStream) throws IOException {InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line;while ((line = bufferedReader.readLine()) != null) {System.out.println(line);}}
首先這里包含了視頻的基本信息,路徑:'D:/video/lin.mp4' 格式為MP4
Stream #0:00x1表示視頻流:
Video: h264 (High) 表示視頻流使用的是H.264編碼,且為高配置文件。
yuv420p(tv, bt709, progressive) 是視頻的像素格式,色域和掃描方式。
544x960 是視頻的分辨率。
655 kb/s 是視頻流的比特率。
23 fps 是視頻的幀率。
23 tbr 是時間基準。
90k tbn 是時間基準。
Stream #0:10x2?表示音頻流的信息:
Audio: aac (LC) 表示音頻流使用的是AAC編碼,低復雜度配置文件。
44100 Hz 是音頻的采樣率。
mono 表示音頻是單聲道的。
fltp 是音頻樣本格式。
48 kb/s 是音頻流的比特率。
2、給視頻加字幕
public static void main(String[] args) {try {// 視頻添加字幕addSubtitle("D:/video/lin.mp4", "D:/video/lin.srt", "D:/video/lin_with_subtitle.mp4");} catch (IOException | InterruptedException e) {e.printStackTrace();}}public static void addSubtitle(String videoPath, String subtitlePath, String outputPath)throws IOException, InterruptedException {String escapedSubPath = subtitlePath.replace("\\", "\\\\").replace(":", "\\:");// 使用完整FFmpeg命令參數ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg","-i", videoPath,"-vf", "subtitles='" + escapedSubPath + "'", // 用單引號包裹路徑"-c:v", "libx264", // 指定視頻編碼器"-c:a", "copy", // 直接復制音頻流"-y", // 覆蓋輸出文件outputPath);// 打印實際執行的命令System.out.println("執行命令: " + String.join(" ", processBuilder.command()));Process process = processBuilder.start();printCommandOutput(process.getErrorStream());int exitCode = process.waitFor();if (exitCode == 0) {System.out.println("字幕添加成功!");} else {System.out.println("處理失敗,退出碼: " + exitCode);}}private static void printCommandOutput(InputStream inputStream) throws IOException {InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line;while ((line = bufferedReader.readLine()) != null) {System.out.println(line);}}
這是一個srt的字幕文件,簡單用Windows自帶的txt寫了一個簡單的的srt文件,內容就如上所示
這個就是生成的結果,lin是原視頻,lin.srt是字幕文件,lin_with_subtitle是生成后帶字幕的文件
上面兩個就為原視頻帶字幕前后的對比了
3、添加水印的操作
public static void main(String[] args) {try {// 給視頻添加水印addWatermark("D:/video/lin.mp4","D:/video/lin.png","D:/video/lin_watermark.mp4");} catch (IOException | InterruptedException e) {e.printStackTrace();}}public static void addWatermark(String videoPath, String watermarkPath, String outputPath)throws IOException, InterruptedException {// 構造FFmpeg命令(支持透明PNG/動態調整位置)ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg","-i", videoPath, // 輸入視頻"-i", watermarkPath, // 輸入水印圖片"-filter_complex","[1]format=rgba," + // 確保水印有透明通道"scale=iw/3:ih/3," + // 縮小3倍(iw/3=原寬度÷3)"colorchannelmixer=aa=0.25[wm];" + // 透明度25%"[0][wm]overlay=25:H-h-25", // 左下角位置(左邊25px,底部25px)"-c:v", "libx264", // 視頻編碼"-preset", "fast", // 編碼速度"-crf", "23", // 畫質控制"-c:a", "copy", // 復制音頻流"-y", // 覆蓋輸出outputPath);// 打印實際執行的命令System.out.println("執行命令: " + String.join(" ", processBuilder.command()));Process process = processBuilder.start();printCommandOutput(process.getErrorStream()); // FFmpeg輸出到stderrint exitCode = process.waitFor();if (exitCode == 0) {System.out.println("水印添加成功!");} else {System.out.println("處理失敗,退出碼: " + exitCode);}}private static void printCommandOutput(InputStream inputStream) throws IOException {InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line;while ((line = bufferedReader.readLine()) != null) {System.out.println(line);}}
這里主要除了固定水印位置還可以設置移動水印的效果
好了以上就是全部內容,當然FFmpeg的功能絕對不僅僅于此,還可以對視頻音頻進行降噪處理、關鍵幀的提取、Ai集成、濾鏡等等。在目前企業中也被廣泛應用。只是作者在這里偷個懶~就不全部展示了。