6

I executed the below FFmpeg terminal command in command prompt successfully. But I am unable to execute this command in my Java program. I can execute all other FFmpeg commands which doesn't have double quotation marks in my Java program. Here I have problem only with double quotation marks("...").

ffmpeg -i "concat:C:\\journalism\\videos\\vid1.ts|C:\\journalism\\videos\\vid2.ts" -c copy C:\\journalism\\videos\\output.mp4

I can execute above in command prompt successfully. But I tried as below in my Java code:

Runtime.getRuntime().exec("C:\\ffmpeg\\bin\\ffmpeg -i 'concat:C:\\journalism\\videos\\vid1.ts|C:\\journalism\\videos\\vid2.ts' -c copy C:\\journalism\\videos\\output.mp4");

Even I tried by replaced the double quotation marks with single quotation marks. But did not succeed.

Can anyone please help me to get out of this issue?

Thanks in advance

4 Answers 4

7

I found answer for myself. Instead of using String object, I used String array as below, then the command executed successfully.

String[] cmd={"C:\\ffmpeg\\bin\\ffmpeg","-i", "concat:C:\\journalism\\videos\\vid1.ts|C:\\journalism\\videos\\vid2.ts", "-c", "copy", "C:\\journalism\\videos\\output.mp4"};
Runtime.getRuntime().exec(cmd);
Sign up to request clarification or add additional context in comments.

Comments

2

My program is running, i just use as below. Hope it help you :

String cmd = "ffmpeg -i http://117.103.224.78/videoinput/Video1.mp4 -s 1920x1080 -c:a copy D:\\tmp\\Video2.mp4";

Process p = Runtime.getRuntime().exec(cmd);

Comments

0

I was trying to do the same, this code did the trick.

String exeLocation = "\"C:\\Program Files\\ffmpeg\\bin\\ffmpeg\"";
String extractFileName = "test.mp4";
String extractFullPath = "C:\\ac3" + "\\" + extractFileName;
String dest = "C:\\\\ac3\\\\output.mp4";

String cmd = exeLocation + " -i " + extractFullPath + " -c copy " + dest ;
Runtime.getRuntime().exec(cmd);

Comments

0

To see usage in a real-world application, see this project on GitHub.

It is possible to use FFmpeg and FFprobe published in org.bytedeco.javacv library.

First, add the dependency for your desired operating system (see here for supported OSes):

// 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")

Executing 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 executeFFmpeg() throws IOException {
        var ffmpegProcess = new ProcessBuilder()
                .command(
                        ffmpegPath,
                        // OR any options you would pass to FFmpeg CLI
                        "-i", "/absolute/path/to/video.mp4"
                )
                .start();
        try (var reader = new BufferedReader(new InputStreamReader(ffmpegProcess.getErrorStream()))) {
            reader
                    .lines()
                    .forEach(System.out::println);
        }
    }
}

Executing FFmpeg in Kotlin:

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

class Test() {
    private val ffmpegPath = Loader.load(ffmpeg::class.java)
    fun executeFFmpeg() {
        ProcessBuilder()
            .command(ffmpegPath, "-i", "/absolute/path/to/video.mp4")
            .runCatching { start() }
            .onFailure { println("Starting the FFmpeg process failed") }
            .getOrNull()
            ?.errorStream
            ?.reader()
            ?.useLines { lines -> lines.forEach(::println) }
    }
}

1 Comment

kotlin version is hardly readable

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.