SpringBoot integrates ffmpeg to implement video transcoding and playback

background

We have built a file preview service before, but the front-end playback component of the video part is limited to mp4 format. In order to support more video formats, we decided to upgrade the solution. Since there are many video formats, it is not practical to customize the player for each format. , decided to uniformly transcode the video source. The transcoded format is mp4. The compatibility is stable and the front-end and back-end modification work is small.

Configuration

Maven adds a java-all-deps reference, which has built-in different versions of ffmpeg files. In order to avoid excessively large files after packaging, unnecessary platform compatibility support is excluded.

        <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>

Transcode

Transcoding is mainly performed by executing the ffmpeg conversion command, specifying the encoder, image quality, and the code reads the execution results through the stream. The blocking command is executed in a synchronous manner. After the execution is completed, the finish.txt mark is written to facilitate the front-end polling of whether the video is converted. After coding, jump to the playback page

 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);
    }

After testing, the following video formats support transcoding mp4

.mp4;.asf;.avi;.dat;.f4v;.flv;.mkv;.mov;.mpg;.rmvb;.ts;.vob;.webm;.wmv;.vob

Guess you like

Origin blog.csdn.net/m0_64355285/article/details/131724131