6

I'm using the following command to convert sequence of images to a video:

ffmpeg -r 1 -i sample%d.png -s 320x240 -aspect 4:3 output.flv

This works fine for me!

Now I'm trying to use the above command to run through Java code.

How can I run the FFmpeg command using Runtime.getRuntime() from my Java code?

2 Answers 2

8
Runtime.getRuntime().exec("ffmpeg -r 1 -i sample%d.png -s 320x240 -aspect 4:3 output.flv");
Sign up to request clarification or add additional context in comments.

Comments

0
See this open-source project for a real-world desktop app example.

It's possible to use org.bytedeco wrappers for FFmpeg and FFplay.

Add the dependency for the core artifact and also for your target operating system (supported OSes can be seen here):

// The main artifact
implementation("org.bytedeco:ffmpeg:7.1-1.5.11")
// The Jar artifact with FFmpeg and FFprobe executables for the desired OS
implementation("org.bytedeco:ffmpeg:7.1-1.5.11:windows-x86_64-gpl")

Then, to execute FFmpeg in Java:

import org.bytedeco.ffmpeg.ffmpeg;
import org.bytedeco.javacpp.Loader;

public class Test {
    private final String ffmpegPath = Loader.load(ffmpeg.class);
    public void exampleFFmpeg() throws IOException {
        var ffmpegProcess = new ProcessBuilder()
                .command(
                        ffmpegPath,
                        // OR any options you would pass to FFmpeg CLI
                        "-i", "/absolute/path/to/a/video.mp4"
                )
                .start();
    }
}

To execute FFmpeg in Kotlin:

import org.bytedeco.ffmpeg.ffmpeg
import org.bytedeco.javacpp.Loader

class Test() {
    private val ffmpegPath = Loader.load(ffmpeg::class.java)
    fun exampleFFmpeg() {
        ProcessBuilder()
            .command(ffmpegPath, "-i", "/absolute/path/to/video.mp4")
            .runCatching { start() }
            .onFailure { println("Starting the FFmpeg process failed") }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.