tags:

views:

79

answers:

2

I am trying to run ffmpeg via the exec call on linux. However I have to use quotes in the command (ffmpeg requires it). I've been looking through the java doc for processbuilder and exec and questions on stackoverflow but I can't seem to find a solution.

I need to run

ffmpeg -i "rtmp://127.0.0.1/vod/sample start=1500 stop=24000" -re -vcodec copy -acodec copy -f flv rtmp://127.0.0.1/live/qltv

I need to insert quotes into the below argument string. Note simply adding single or double quotes preceded by a backslash doesn't work due to the nature of how processbuilder parses and runs the commands.

String argument = "ffmpeg -i rtmp://127.0.0.1/vod/"
                    + nextVideo.getFilename()
                    + " start=" + nextVideo.getStart()
                    + " stop=" + nextVideo.getStop()
                    + " -re -vcodec copy -acodec copy -f flv rtmp://127.0.0.1/live/qltv";

Any help would be greatly appreciated.

+3  A: 

Make an array!

exec can take an array of strings, which are used as an array of command & arguments (as opposed to an array of commands)

Something like this ...

String[] arguments = new String[] { "ffmpeg", 
"-i", 
"rtmp://127.0.0.1/vod/sample start=1500 stop=24000",
"start=xxx",
...
};
amir75
you can remove the characters "new String[]"; the braces will automatically produce a string array for you.
Jason S
This won't work as rtmp://127.0.0.1/vod/sample start=xxx stop=xxxhas to have quotes around it. Putting the argument in a string array doesn't help.
Slava Markeyev
Sorry, doesn't it help if you put escaped quotes, as follows: "\"rtmp://127.0.0.1/vod/sample start=1500 stop=24000\""
amir75
I'm on a Windows box right now, but I'll get it working tomorrow and let you know ..
amir75
In the meantime, you could always simplify the call by wrapping the call in a shell script (which expects args $1, $2, $3)
amir75
Thank you amir75. Doing it via the shell script as you suggested works. This doesn't solve the problem with exec and quotes but I'm happy. If you find a solution post back as I know others have had the same problem and might not be able to use shell scripts for whatever reason.
Slava Markeyev
A: 

It sounds like you need to escape quotes inside your argument string. This is simple enough to do with a preceding backslash.

E.g.

String containsQuote = "\"";

This will evaluate to a string containing just the quote character.

Or in your particular case:

String argument = "ffmpeg -i \"rtmp://127.0.0.1/vod/"
          + nextVideo.getFilename()
          + " start=" + nextVideo.getStart()
          + " stop=" + nextVideo.getStop() + "\""
          + " -re -vcodec copy -acodec copy -f flv rtmp://127.0.0.1/live/qltv";
Kris