关于ffmpeg:SpringBoot集成ffmpeg实现视频转码播放

10次阅读

共计 2046 个字符,预计需要花费 6 分钟才能阅读完成。

背景

之前构建过文件预览服务,对于视频局部前端播放组件限度只能为 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

正文完
 0