External command does not execute completely - Java
Asked Answered
N

4

5

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?

Nutriment answered 26/3, 2012 at 12:12 Comment(2)
the \t in C:\test is going to be passed as C:<tab>est, by the way... you need escape the backslash.Sharronsharyl
ffmpeg is surely not a DOS command. It's a command-line program.Nikkinikkie
N
5

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();
Nutriment answered 18/4, 2012 at 7:36 Comment(0)
E
3

Two problems:

  1. 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)

  2. 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.

Eelworm answered 26/3, 2012 at 12:14 Comment(1)
You're right, though I suspect something may be amiss elsewhere. At present the program shouldn't wait for the process and so End should be printed to the console almost immediatelyConsequent
B
0

Have you tried control String ", something like :

cmd "ffmpeg" -i "C:\test.flv" -acodec "libmp3lame" -y "C:\test.mp3"

or

cmd "ffmpeg -i C:\test.flv -acodec libmp3lame -y C:\test.mp3"
Bromeosin answered 26/3, 2012 at 12:21 Comment(0)
D
0

First of all, this is not a DOS command. It is a console command line, from any flavour of Microsoft Windows.

You can do it using p.waitFor() as @aioobe explains or you can use a wrapper Java API for FFMPEG like Xuggler, it would be easy to use, and it has LGPL.

Durware answered 26/3, 2012 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.