javaffmpegprocessbuilderruntime.exec

How to run FFmpeg command in Java (Windows)


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


Solution

  • 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);