1

I have problems executing this ffmpeg command in my java code:

ffmpeg -i sample.mp4 -i ad.mp4 -filter_complex "[0:v]trim=0:15,setpts=PTS-STARTPTS[v0]; [1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; [0:v]trim=20:30,setpts=PTS-STARTPTS[v2]; [v0][v1][v2]concat=n=3:v=1:a=0[out]" -map "[out]" output.mp4

I use this command:

String command= "ffmpeg -i "+dir+"sample.mp4 -i "+dir+"ad.mp4 -filter_complex [0:v]trim=0:15,setpts=PTS-STARTPTS[v0]; [1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; [0:v]trim=20:30,setpts=PTS-STARTPTS[v2]; [v0][v1][v2]concat=n=3:v=1:a=0[out] -map [out] output.mp4";
final ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", command);
System.out.println(command);
final Process p = pb.start();

I simply remove the ". But still no chance.

I also used the getRuntime() method, but that also doesn't work for me.

String c1=" -i "+dir+"sample.mp4 "+"-i "+dir+"ad.mp4 -fi‌​lter_complex [0:v]‌​trim=0:15,setpts=PTS‌​-STARTPTS[v0]; [1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; [0:v]trim=20:30,setpts=PTS-STARTPTS[v2]; [v0][v1][v2]concat=n=3:v=1:a=0[out] -map [out] "+dir+"output.‌​mp4";
RunCommand("ffmpeg"+c1);

Using this method:

private static void RunCommand(String command) throws InterruptedException {
    try {
        // Execute command
        Process proc = Runtime.getRuntime().exec(command);
        System.out.println(proc.exitValue());

        // Get output stream to write from it
        // Read the output

        BufferedReader reader =  
                new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = "";
        while((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
            //              System.out.println(ads.get(0));
        }
        proc.waitFor();  

    } catch (IOException e) {
    }
}

This one doesn't work also, and printing the exit value shows this:

Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
    at java.lang.UNIXProcess.exitValue(UNIXProcess.java:423)
    at parser.Parser.RunCommand(Parser.java:106)
    at parser.Parser.commandGenerator2(Parser.java:79)
    at parser.Parser.main(Parser.java:44)

If I move the proc.waitFor(); before printing the exit value, it is 1.

What is the problem? Why it doesn't run in Java code?

Mary
  • 393
  • 1
  • 4
  • 17
  • You'll probably run it easier with `Runtime.exec()` due to the complex arguments. – Kayaman Aug 07 '17 at 20:04
  • Why a re you using /bin/sh? You can run `ffmpeg` directly, and use a ProcessBuilder constructor that avoid the parsing of the command parameters by the shell: `new ProcessBuilder("ffmpeg","-i","sample.mp4","-i","ad.mp4","-filter_complex","[0:v]trim=0:15,setpts=PTS-STARTPTS[v0]; [1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; [0:v]trim=20:30,setpts=PTS-STARTPTS[v2]; [v0][v1][v2]concat=n=3:v=1:a=0[out]","-map","[out]","output.mp4"` – xenoid Aug 07 '17 at 23:03
  • @xenoid this doesn't run for me. I call `.start()` and nothing happens. – Mary Aug 07 '17 at 23:15
  • @Kayaman using `Runtime` doesn't do anything for me. – Mary Aug 07 '17 at 23:15
  • Well, maybe you need to read up on how to use `ProcessBuilder`. So far you're saying "it no work", with zero explanation. "Doesn't do anything for me" is not a way to describe what problems you're having. – Kayaman Aug 08 '17 at 05:57
  • Either it returns a process or throws an exception. If there is a process it has an `exitValue()` (after a `waitFor()`). So what do you get? And did you check the stderr contents of the process, in case `ffmpeg` is complaining about something? – xenoid Aug 08 '17 at 09:08
  • It is 1. I updated my question with details. – Mary Aug 08 '17 at 22:50

0 Answers0