java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > javacv ffmpeg使用原生ffmpeg命令

javacv ffmpeg使用原生ffmpeg命令方式

作者:搬山境KL攻城狮

在使用javacv集成ffmpeg进行视频处理时,发现使用ffmpeg-6.0-1.5.9版本出现原生命令执行失败的问题,通过降级至ffmpeg-5.1.2-1.5.8版本,问题得到解决,此外,ffprobe可以用于获取视频属性,需确保视频片段属性一致性

javacv ffmpeg使用原生ffmpeg命令

测试过程中,

发现ffmpeg-6.0-1.5.9-linux-x86_64.jar 存在问题(ffmpeg原生命令执行失败)

降级到ffmpeg-5.1.2-1.5.8-linux-x86_64.jar版本正常

<javacv.version>1.5.8</javacv.version>

    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>${javacv.version}</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg</aifactId>
            <version>5.1.2-${javacv.version}</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>Windows</id>
            <activation>
                <os>
                    <!-- mvn enforcer:display-info -->
                    <family>windows</family>
                </os>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>ffmpeg</artifactId>
                    <version>5.1.2-${javacv.version}</version>
                    <classifier>windows-x86_64</classifier>
                </dependency>
            </dependencies>
        </profile>
        <profile>
           <id>Mac</id>
           <activation>
               <os>
                   <!-- mvn enforcer:display-info -->
                   <family>mac</family>
               </os>
           </activation>
           <dependencies>
               <dependency>
                   <groupId>org.bytedeco</groupId>
                   <artifactId>ffmpeg</artifactId>
                   <version>5.1.2-${javacv.version}</version>
                   <classifier>macosx-x86_64</classifier>
               </dependency>
           </dependencies>
       </profile>
    </profiles>

1. ffmpeg

org.bytedeco.ffmpeg.ffmpeg
org.bytedeco.ffmpeg.ffprobe
   String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
   System.out.println("ffmpeg path=>" + ffmpeg);
   ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-version");
   pb.inheritIO().start().waitFor();
        // 输入视频文件路径
        List<String> inputFiles = Arrays.asList("video1.mp4", "video2.mp4");
        // 输出视频文件路径
        String outputFile = "output.mp4";
        // 拼接模板文件
        String modelFile = "modelFile.txt";
        // 创建拼接模板文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(modelFile))) {
            for (String inputFile : inputFiles) {
                writer.write("file '" + inputFile + "'");
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
        ProcessBuilder pb = new ProcessBuilder(ffmpeg,
                "-f", "concat",
                "-safe", "0",
                "-i", modelFile,
                "-c", "copy",
                outputFile);
        pb.inheritIO().start().waitFor();

2. ffprobe

    public static void main(String[] args) throws IOException, InterruptedException {
        String ffprobe = Loader.load(org.bytedeco.ffmpeg.ffprobe.class);
        System.out.println("ffprobe path=>" + ffprobe);
        ProcessBuilder pb = new ProcessBuilder(ffprobe, "-version");
        pb.inheritIO().start().waitFor();
    }

更多用法:help、大模型

ffprobe -h topic
        String ffprobe = Loader.load(org.bytedeco.ffmpeg.ffprobe.class);
        ProcessBuilder pb = new ProcessBuilder(ffprobe,
                "-v", "error",
                "-print_format", "json",
                "-show_streams",
                "video1.mp4");
        pb.inheritIO().start().waitFor();

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文