前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot集成ffmpeg实现视频转码播放

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

作者头像
code2roc
发布2023-07-19 15:36:06
5400
发布2023-07-19 15:36:06
举报

背景

之前构建过文件预览服务,对于视频部分前端播放组件限制只能为mp4格式,为了支持更多视频格式决定对方案进行升级,由于视频格式较多,针对每一种格式定制选择播放器不太现实,决定对视频源统一转码,转码后的格式为mp4,兼容性稳定且前后端改造工作较小

配置

maven添加java-all-deps引用,该引用内置不同版本ffmpeg文件,为了避免打包后文件过大,排除不需要的平台兼容支持

代码语言:javascript
复制
        <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标识,便于前端轮询视频是否转码完毕,跳转播放页面

代码语言:javascript
复制
 ffmpeg -i inputpath -c:v libx264 -crf 19 -strict experimental outputpath
代码语言:javascript
复制
 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

代码语言:javascript
复制
.mp4;.asf;.avi;.dat;.f4v;.flv;.mkv;.mov;.mpg;.rmvb;.ts;.vob;.webm;.wmv;.vob
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 配置
  • 转码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档