So, I am building a program that converts .flv files to other formats. For that I'm using ffmpeg which does its job perfectly when executing it via command line. For example:
ffmpeg -i C:\test.flv -acodec libmp3lame -y C:\test.mp3
This example works like a charm - there isn't a single problem when executing the command.
BUT when I try to execute the same command from within a Java class a problem occurs. I do this in a try-catch block:
System.out.println("Start");
Process p = Runtime.getRuntime().exec("cmd /c ffmpeg -i C:\test.flv -acodec libmp3lame -y C:\test.mp3");
System.out.println("End");
The console prints "Start". It starts converting and it doesn't finish.
Can somebody help me?
Well, the problem was solved in a very unexpected way. I just had to read the output that the execution generates. And, voila, the file was converted.
InputStream in = p.getErrorStream();
int c;
while ((c = in.read()) != -1)
{
System.out.print((char)c);
}
in.close();
Two problems:
cmd /c ffmpeg -i C:\test.flv -acodec libmp3lame -y C:\test.mp3
is not a command. (You probably end up getting an IOException
which causes the "End"
to be suppressed.)
cmd
is the command you want to execute, and the rest of the string are arguments. Use ProcessBuilder
or Runtime.exec(String[] cmdarray)
You need to wait for the process to finish. Call
p.waitFor();
after starting the process. Here's a link for the documentation of Process.waitFor
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With