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文件,為了避免打包后文件過大,排除不需要的平臺(tái)兼容支持
<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)識(shí),便于前端輪詢視頻是否轉(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)文章
SpringSecurity rememberme功能實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringSecurity rememberme功能實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
springboot使用Scheduling實(shí)現(xiàn)動(dòng)態(tài)增刪啟停定時(shí)任務(wù)教程
這篇文章主要介紹了springboot使用Scheduling實(shí)現(xiàn)動(dòng)態(tài)增刪啟停定時(shí)任務(wù)教程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
SpringCloud Zuul網(wǎng)關(guān)功能實(shí)現(xiàn)解析
這篇文章主要介紹了SpringCloud Zuul網(wǎng)關(guān)功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot加入Guava Cache實(shí)現(xiàn)本地緩存代碼實(shí)例
這篇文章主要介紹了SpringBoot加入Guava Cache實(shí)現(xiàn)本地緩存代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例
這篇文章主要介紹了Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例,灰度發(fā)布是指在黑與白之間,能夠平滑過渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時(shí)候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度,需要的朋友可以參考下2023-08-08
Java Swing中JDialog實(shí)現(xiàn)用戶登陸UI示例
這篇文章主要介紹了Java Swing中JDialog實(shí)現(xiàn)用戶登陸UI功能,結(jié)合完整實(shí)例形式分析了Swing使用JDialog實(shí)現(xiàn)用戶登陸UI界面窗口功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
基于Spring Data Jest的Elasticsearch數(shù)據(jù)統(tǒng)計(jì)示例
本篇文章主要介紹了基于Spring Data Jest的Elasticsearch數(shù)據(jù)統(tǒng)計(jì)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02

