Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Runtime.getRuntime().exec() with quotes

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.

like image 857
Slava Markeyev Avatar asked Jul 06 '10 20:07

Slava Markeyev


2 Answers

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",
"-re",
...
};
like image 200
laher Avatar answered Sep 19 '22 12:09

laher


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";
like image 31
Kris Avatar answered Sep 21 '22 12:09

Kris