开发者

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

开发者 https://www.devze.com 2023-01-06 10:10 出处:网络
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 question

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 inser开发者_运维技巧t 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.


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",
...
};


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";
0

精彩评论

暂无评论...
验证码 换一张
取 消