使用 java-ffmpeg 包装器,还是简单地使用 java 运行时来执行 ffmpeg?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1281353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:45:59  来源:igfitidea点击:

use java-ffmpeg wrapper, or simply use java runtime to execute ffmpeg?

javaffmpegruntime.exec

提问by Beier

I'm pretty new to Java, need to write a program that listen to video conversion instructions and convert the video once an new instruction arrives (instructions is stored in Amazon SQS, but it's irrelevant to my question)

我对 Java 很陌生,需要编写一个程序来监听视频转换指令并在新指令到达时转换视频(指令存储在 Amazon SQS 中,但这与我的问题无关)

I'm facing a choice, either use Java RunTime to exec 'ffmpeg' conversion (like from command line), or I can use a ffmpeg wrapper written inJava http://fmj-sf.net/ffmpeg-java/getting_started.php

我面临着一个选择,要么使用 Java RunTime 来执行 'ffmpeg' 转换(比如从命令行),或者我可以使用用 Java 编写的 ffmpeg 包装器http://fmj-sf.net/ffmpeg-java/getting_started.php

I'd much prefer using Java Runtime to exec ffmpeg directly, and avoid using java-ffmpeg wrapper as I have to learn the library. so my question is are there any benefits using java-ffmpeg wrapper over exec ffmpeg directly using Runtime? I don't need ffmpeg to play videos, just convert videos

我更喜欢使用 Java 运行时直接执行 ffmpeg,并避免使用 java-ffmpeg 包装器,因为我必须学习该库。所以我的问题是直接使用运行时使用 java-ffmpeg 包装器而不是 exec ffmpeg 有什么好处吗?我不需要 ffmpeg 来播放视频,只需转换视频

Thanks

谢谢

采纳答案by Peter Thomas

If I'm not mistaken, the "ffmpeg wrapper" project you linked to is out of date and not maintained. ffmpeg is a very active project, lot's of changes and releases all the time.

如果我没记错的话,您链接到的“ffmpeg 包装器”项目已过时且未维护。ffmpeg 是一个非常活跃的项目,一直有很多变化和发布。

You should look at the Xuggler project, this provides a Java API for what you want to do, and they have tight integration with ffmpeg.

您应该查看 Xuggler 项目,它为您想要做的事情提供了一个 Java API,并且它们与 ffmpeg 紧密集成。

http://www.xuggle.com/xuggler/

http://www.xuggle.com/xuggler/

Should you choose to go down the Runtime.exec() path, this Red5 thread should be useful:

如果您选择使用 Runtime.exec() 路径,这个 Red5 线程应该很有用:

http://www.nabble.com/java-call-ffmpeg-ts15886850.html

http://www.nabble.com/java-call-ffmpeg-ts15886850.html

回答by Michael Borgwardt

The benefits of using the wrapper would be mainly that it offers more and fine-grained functionality not accessible via the command line (not relevant to you) and makes error handling and status control easier - you'll have to parse the command line tool's standard output and error streams.

使用包装器的好处主要是它提供了无法通过命令行访问的更多和细粒度的功能(与您无关)并使错误处理和状态控制更容易 - 您必须解析命令行工具的标准输出和错误流。

回答by Art Clarke

Also, as of Xuggler 3.3, Xuggler is LGPL meaning you don't need a commercial license.

此外,从 Xuggler 3.3 开始,Xuggler 是 LGPL,这意味着您不需要商业许可证。

回答by mateuscb

I too am looking for something to wrap FFMPEG in Java. While searching, I found this: https://github.com/bramp/ffmpeg-cli-wrapper.

我也在寻找用 Java 包装 FFMPEG 的东西。在搜索时,我发现了这个:https: //github.com/bramp/ffmpeg-cli-wrapper

As of today, it seems to have been modified a month ago. So, hopefully it will stick around for a while.

截至今天,它似乎已经在一个月前进行了修改。所以,希望它会坚持一段时间。

A sample from their docs:

他们的文档中的一个示例:

FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg");
FFprobe ffprobe = new FFprobe("/path/to/ffprobe");

FFmpegBuilder builder = new FFmpegBuilder()
    .setInput(in)
    .overrideOutputFiles(true)
    .addOutput("output.mp4")
        .setFormat("mp4")
        .setTargetSize(250000)

        .disableSubtitle()

        .setAudioChannels(1)
        .setAudioCodec("libfdk_aac")
        .setAudioRate(48000)
        .setAudioBitrate(32768)

        .setVideoCodec("libx264")
        .setVideoFramerate(Fraction.getFraction(24, 1))
        .setVideoResolution(640, 480)

        .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL)
        .done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createTwoPassJob(builder).run();

回答by Stefan Endrullis

There are a lot of Java libraries providing FFMPEG wrappers. However, most of these libraries are unfortunately outdated and use old FFMPEG versions which lack some important codecs (e.g. Xuggler, humble video, JavaAV, JavaAVC, and jave). So be careful when using those projects!

有很多 Java 库提供 FFMPEG 包装器。然而,不幸的是,这些库中的大多数已经过时,并且使用旧的 FFMPEG 版本,这些版本缺少一些重要的编解码器(例如 Xuggler、谦虚的视频、JavaAV、JavaAVC 和 jave)。所以在使用这些项目时要小心!

However, there is one FFMPEG wrapper that is still actively developedand supports FFMPEG 4:

但是,有一个 FFMPEG 包装器仍在积极开发并支持FFMPEG 4

Alternatively you can use a wrapper for the command line interface of FFMPEG, such as ffmpeg-cli-wrapper. Then it's in your hand to update ffmpeg manually without having to wait for a new release of the wrapper library.

或者,您可以为 FFMPEG 的命令行界面使用包装器,例如ffmpeg-cli-wrapper。然后,您可以手动更新 ffmpeg,而无需等待包装库的新版本。

回答by cyril

i try several ways, the most simple for me is ffmpeg-cli-wrapper, because it use default ffmpeg or specific one, moreover you can configure lot of configuration like scale, bitrate... get duration... JAVE from sauronsoftware make lot of error, XUGGLE and other i found it too complicated

我尝试了几种方法,对我来说最简单的是ffmpeg-cli-wrapper,因为它使用默认的ffmpeg或特定的一种,而且您可以配置很多配置,例如比例,比特率...获取持续时间...来自sauronsoftware的JAVE make lot错误,XUGGLE 和其他我觉得太复杂了

回答by Denis Kokorin

I wrote my own Java ffmpeg command line wrapper: Jaffree. It works with both ffprobe and ffmpeg and supports programmatic video production and consumption. Also it has in my opinion more convenient fluid API.

我编写了自己的 Java ffmpeg 命令行包装器:Jaffree。它适用于 ffprobe 和 ffmpeg,并支持程序化视频制作和消费。在我看来,它还有更方便的流体 API。

Here is an ffprobe usage example:

这是一个 ffprobe 使用示例:

FFprobeResult result = FFprobe.atPath(BIN)
        .setInputPath(VIDEO_MP4)
        .setShowStreams(true)
        .setShowError(true)
        .execute();

if (result.getError() != null) {
    //TODO handle ffprobe error message
    return;
}

for (Stream stream : probe.getStreams().getStream()) {
    //TODO analyze stream data
}

ProgressListener listener = new ProgressListener() {
    @Override
    public void onProgress(FFmpegProgress progress) {
        //TODO handle progress data
    }
};

And this is for ffmpeg:

这是 ffmpeg 的:

FFmpegResult result = FFmpeg.atPath(BIN)
        .addInput(Input.fromPath(VIDEO_MP4))
        .addOutput(Output.toPath(outputPath)
                .addCodec(null, "copy")
        )
        .setProgressListener(listener)
        .execute();