2010-07-06 191 views
5

我想通过在Linux上的exec调用运行ffmpeg。但是我必须在命令中使用引号(ffmpeg需要它)。我一直在寻找processbuilder和exec的java文档和stackoverflow上的问题,但我似乎无法找到解决方案。Java Runtime.getRuntime()。exec()用引号

我需要运行

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 

我需要插入到报价以下参数字符串。注意,由于processbuilder解析和运行命令的性质,只需在反斜杠之前添加单引号或双引号就不起作用。

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

任何帮助将不胜感激。

回答

6

制作一个数组!

EXEC可以采取串,其被用作命令&参数数组(相对于命令的数组)

像这样的阵列...

String[] arguments = new String[] { "ffmpeg", 
"-i", 
"rtmp://127.0.0.1/vod/sample start=1500 stop=24000", 
"-re", 
... 
}; 
+0

您可以删除字符 “新的String []”;大括号会自动为你产生一个字符串数组。 – 2010-07-06 21:08:11

+1

这不会起作用 rtmp://127.0.0.1/vod/sample start = xxx stop = xxx 必须有引号。将参数放在字符串数组中并没有帮助。 – 2010-07-06 21:31:31

+0

对不起,如果您放置转义引号,它没有帮助,如下所示:“\”rtmp://127.0.0.1/vod/sample start = 1500 stop = 24000 \“” – laher 2010-07-06 21:57:18

1

听起来就像你需要在你的参数字符串中转义引号一样。这很简单,可以使用前面的反斜杠。

E.g.

String containsQuote = "\""; 

这将评估为只包含引号字符的字符串。

或者你的具体情况:

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"; 
相关问题