How to Use FFmpeg CLI Wrapper for Java

Jul 20, 2024 | Programming

Welcome to your stylish guide to the FFmpeg CLI Wrapper for Java developed by Andrew Brampton. This tool allows Java developers to effortlessly harness the power of FFmpeg, a legendary multimedia framework, using a fluent interface. Whether you’re encoding videos, extracting media information, or tracking progress during encoding, this wrapper is designed for easy and effective use.

Installation

To get started, ensure you have Java 8 or above. You can easily install the dependency using Maven by adding the following lines to your pom.xml:

<dependency>
    <groupId>net.bramp.ffmpeg</groupId>
    <artifactId>ffmpeg</artifactId>
    <version>0.8.0</version>
</dependency>

Usage Guide

Video Encoding

To encode a video, picture the process as ordering coffee from a café. You specify your order (input) and the desired beverage (output format and settings). Here’s how you can encode videos:

java
FFmpeg ffmpeg = new FFmpeg(pathtoffmpeg);
FFprobe ffprobe = new FFprobe(pathtoffprobe);
FFmpegBuilder builder = new FFmpegBuilder()
    .setInput("input.mp4") // Your video file
    .overrideOutputFiles(true) // Replace existing files
    .addOutput("output.mp4") // Destination file
    .setFormat("mp4") // File format
    .setTargetSize(250_000) // Target size in KB
    .disableSubtitle() // No subtitles
    .setAudioChannels(1) // Mono audio
    .setAudioCodec("aac") // Audio codec
    .setAudioSampleRate(48_000) // Audio sample rate
    .setAudioBitRate(32768) // Audio bit rate
    .setVideoCodec("libx264") // Video codec
    .setVideoFrameRate(24, 1) // Frame rate
    .setVideoResolution(640, 480) // Video resolution
    .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Use experimental specs
    .done();
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run(); // One-pass encode
// Or for a two-pass encode
executor.createTwoPassJob(builder).run();

In this scenario, the coffee order is broken down into various attributes such as size, type (codec), and additional specifications (like video frame rate). You make all your choices upfront, and the barista (FFmpeg) prepares your drink (output) to perfection!

Getting Media Information

To grab media information – think of it like checking the details on a menu before placing an order:

java
FFprobe ffprobe = new FFprobe(pathtoffprobe);
FFmpegProbeResult probeResult = ffprobe.probe("input.mp4");
FFmpegFormat format = probeResult.getFormat();
System.out.format("%nFile: %s ; Format: %s ; Duration: %.3fs",
    format.filename, format.format_long_name, format.duration);
FFmpegStream stream = probeResult.getStreams().get(0);
System.out.format("%nCodec: %s ; Width: %dpx ; Height: %dpx",
    stream.codec_long_name, stream.width, stream.height);

Monitoring Progress During Encoding

If you want to monitor the progress – akin to watching your coffee being made – you can set up a progress listener:

java
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
FFmpegProbeResult in = ffprobe.probe("input.flv");
FFmpegBuilder builder = new FFmpegBuilder()
    .setInput(in) // Your video file
    .addOutput("output.mp4") // Destination file
    .done();
FFmpegJob job = executor.createJob(builder, new ProgressListener() {
    final double duration_ns = in.getFormat().duration * TimeUnit.SECONDS.toNanos(1);
    @Override
    public void progress(Progress progress) {
        double percentage = progress.out_time_ns / duration_ns;
        System.out.println(String.format(
            "[%.0f%%] status:%s frame:%d time:%s ms fps:%.0f speed:%.2fx",
            percentage * 100,
            progress.status,
            progress.frame,
            FFmpegUtils.toTimecode(progress.out_time_ns, TimeUnit.NANOSECONDS),
            progress.fps.doubleValue(),
            progress.speed
        ));
    }
});
job.run();

Troubleshooting

When using the FFmpeg CLI Wrapper, you might encounter a few hiccups. Here are some troubleshooting tips:

  • Missing Dependencies: Ensure that your Maven configuration is properly set up to include the FFmpeg dependency.
  • Incompatible Video Formats: Check if the input video format is supported by FFmpeg.
  • Encoding Issues: If the output is not what you expect, review your encoding settings to make sure parameters are correctly defined.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox