SpringBoot集成ffmpeg實現(xiàn)視頻轉碼播放示例詳解
背景
之前構建過文件預覽服務,對于視頻部分前端播放組件限制只能為mp4格式,為了支持更多視頻格式?jīng)Q定對方案進行升級,由于視頻格式較多,針對每一種格式定制選擇播放器不太現(xiàn)實,決定對視頻源統(tǒng)一轉碼,轉碼后的格式為mp4,兼容性穩(wěn)定且前后端改造工作較小
配置
maven添加java-all-deps引用,該引用內(nèi)置不同版本ffmpeg文件,為了避免打包后文件過大,排除不需要的平臺兼容支持
<dependency> <groupId>ws.schild</groupId> <artifactId>jave-all-deps</artifactId> <version>3.3.1</version> <exclusions> <!-- 排除windows 32位系統(tǒng) --> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-win32</artifactId> </exclusion> <!-- 排除linux 32位系統(tǒng) --> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux32</artifactId> </exclusion> <!-- 排除Mac系統(tǒng)--> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-osx64</artifactId> </exclusion> <!-- 排除osxm--> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-osxm1</artifactId> </exclusion> <!-- 排除arm--> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux-arm32</artifactId> </exclusion> <exclusion> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux-arm64</artifactId> </exclusion> </exclusions> </dependency>
轉碼
主要通過執(zhí)行ffmpeg轉換命令進行轉碼,指定編碼器,畫質(zhì),代碼通過流讀取執(zhí)行結果,阻塞命令以同步方式執(zhí)行完畢,執(zhí)行完畢后寫入finish.txt標識,便于前端輪詢視頻是否轉碼完畢,跳轉播放頁面
ffmpeg -i inputpath -c:v libx264 -crf 19 -strict experimental outputpath
ProcessWrapper ffmpeg = new DefaultFFMPEGLocator().createExecutor(); ffmpeg.addArgument("-i"); ffmpeg.addArgument(fileConvertInfo.getFilePath()); ffmpeg.addArgument("-c:v"); ffmpeg.addArgument("libx264"); ffmpeg.addArgument("-crf"); ffmpeg.addArgument("19"); ffmpeg.addArgument("-strict"); ffmpeg.addArgument("experimental"); ffmpeg.addArgument(fileConvertInfo.getFileDirPath() + "convert.mp4"); ffmpeg.execute(); try (BufferedReader br = new BufferedReader(new InputStreamReader(ffmpeg.getErrorStream()))) { blockFfmpeg(br); } File file = new File(fileConvertInfo.getFileDirPath() + "finish.txt"); file.createNewFile(); private static void blockFfmpeg(BufferedReader br) throws IOException { String line; // 該方法阻塞線程,直至合成成功 while ((line = br.readLine()) != null) { doNothing(line); } } private static void doNothing(String line) { System.out.println(line); }
經(jīng)過測試以下視頻格式支持轉碼mp4
.mp4;.asf;.avi;.dat;.f4v;.flv;.mkv;.mov;.mpg;.rmvb;.ts;.vob;.webm;.wmv;.vob
以上就是SpringBoot集成ffmpeg實現(xiàn)視頻轉碼播放示例詳解的詳細內(nèi)容,更多關于SpringBoot ffmpeg視頻轉碼的資料請關注腳本之家其它相關文章!
相關文章
淺談Java的虛擬機結構以及虛擬機內(nèi)存的優(yōu)化
這篇文章主要介紹了Java的虛擬機結構以及虛擬機內(nèi)存的優(yōu)化,講到了JVM的堆和??臻g及GC垃圾回收等重要知識,需要的朋友可以參考下2016-03-03在springboot3微項目中如何用idea批量創(chuàng)建單元測試邏輯
這篇文章主要介紹了在SpringBoot3項目中使用IntelliJIDEA批量創(chuàng)建單元測試包括準備工作(確保項目配置正確,添加測試依賴),使用IntelliJIDEA創(chuàng)建測試,感興趣的朋友一起看看吧2024-10-10springboot集成spring cache緩存示例代碼
本篇文章主要介紹了springboot集成spring cache示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳
這篇文章主要介紹了SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10