SpringBoot集成ffmpeg實現視頻轉碼播放

背景

之前構建過文件預覽服務,對於視頻部分前端播放組件限制只能爲mp4格式,爲了支持更多視頻格式決定對方案進行升級,由於視頻格式較多,針對每一種格式定製選擇播放器不太現實,決定對視頻源統一轉碼,轉碼後的格式爲mp4,兼容性穩定且前後端改造工作較小

配置

maven添加java-all-deps引用,該引用內置不同版本ffmpeg文件,爲了避免打包後文件過大,排除不需要的平臺兼容支持

        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>3.3.1</version>
            <exclusions>
                <!--  排除windows 32位系統      -->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-win32</artifactId>
                </exclusion>
                <!--  排除linux 32位系統      -->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-linux32</artifactId>
                </exclusion>
                <!-- 排除Mac系統-->
                <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>

轉碼

主要通過執行ffmpeg轉換命令進行轉碼,指定編碼器,畫質,代碼通過流讀取執行結果,阻塞命令以同步方式執行完畢,執行完畢後寫入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);
    }

經過測試以下視頻格式支持轉碼mp4

.mp4;.asf;.avi;.dat;.f4v;.flv;.mkv;.mov;.mpg;.rmvb;.ts;.vob;.webm;.wmv;.vob
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章