SpringBoot集成ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)碼播放示例詳解
背景
之前構(gòu)建過文件預(yù)覽服務(wù),對于視頻部分前端播放組件限制只能為mp4格式,為了支持更多視頻格式?jīng)Q定對方案進(jìn)行升級,由于視頻格式較多,針對每一種格式定制選擇播放器不太現(xiàn)實(shí),決定對視頻源統(tǒng)一轉(zhuǎn)碼,轉(zhuǎn)碼后的格式為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>轉(zhuǎn)碼
主要通過執(zhí)行ffmpeg轉(zhuǎn)換命令進(jìn)行轉(zhuǎn)碼,指定編碼器,畫質(zhì),代碼通過流讀取執(zhí)行結(jié)果,阻塞命令以同步方式執(zhí)行完畢,執(zhí)行完畢后寫入finish.txt標(biāo)識,便于前端輪詢視頻是否轉(zhuǎn)碼完畢,跳轉(zhuǎn)播放頁面
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)過測試以下視頻格式支持轉(zhuǎn)碼mp4
.mp4;.asf;.avi;.dat;.f4v;.flv;.mkv;.mov;.mpg;.rmvb;.ts;.vob;.webm;.wmv;.vob
以上就是SpringBoot集成ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)碼播放示例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot ffmpeg視頻轉(zhuǎn)碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談Java的虛擬機(jī)結(jié)構(gòu)以及虛擬機(jī)內(nèi)存的優(yōu)化
這篇文章主要介紹了Java的虛擬機(jī)結(jié)構(gòu)以及虛擬機(jī)內(nèi)存的優(yōu)化,講到了JVM的堆和??臻g及GC垃圾回收等重要知識,需要的朋友可以參考下2016-03-03
在springboot3微項(xiàng)目中如何用idea批量創(chuàng)建單元測試邏輯
這篇文章主要介紹了在SpringBoot3項(xiàng)目中使用IntelliJIDEA批量創(chuàng)建單元測試包括準(zhǔn)備工作(確保項(xiàng)目配置正確,添加測試依賴),使用IntelliJIDEA創(chuàng)建測試,感興趣的朋友一起看看吧2024-10-10
springboot集成spring cache緩存示例代碼
本篇文章主要介紹了springboot集成spring cache示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳
這篇文章主要介紹了SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

