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

你说的曾经没有我的故事 提交于 2019-12-06 00:37:38

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/3190190/java-runtime-getruntime-exec-with-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!