Java Process Builder runs ffmpeg commands very slowly

Viewed 30

I'm trying to run ffmpeg in Java using ProcessBuilder. I'm on Windows. It works fine. But not sure why it's much slower than when I just run the same command in command prompt or PowerShell.

Why is it? Is there any ways to increase the speed?

processBuilder.command("C:\\Windows\\System32\\cmd.exe", "/c","ffmpeg.exe", "-y", "-i", video,"-vf","scale=720:-1","out.mp4");
    processBuilder.redirectErrorStream(true);
    try {
                process = processBuilder.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                
                String line="";
                while ((line = reader.readLine()) != null) {
                        System.out.println(line);
        }
        } catch (Exception e) {
            System.err.println("Error in processBuilder. ");
        }
1 Answers

You have two starts, delete the first or the redirect will not work:

process = processBuilder.start();

If your sub-process is quite verbose the problem may simply be System.out.println() as multiple line output to some Windows cmd / terminals can be exceptionally slow. You can verify if this is the case by commenting out the print, or capture to File based output before start:

processBuilder.redirectOutput(new File("stdout.log"));

Don't forget to add status check at the end and cross check rc with ffmpeg documentation:

int rc = process.waitFor();
Related