6

我正在尝试通过 linux 上的 exec 调用运行 ffmpeg。但是我必须在命令中使用引号(ffmpeg 需要它)。我一直在查看 processbuilder 和 exec 的 java 文档以及有关 stackoverflow 的问题,但我似乎找不到解决方案。

我需要跑步

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

任何帮助将不胜感激。

4

2 回答 2

6

做一个数组!

exec 可以接受一个字符串数组,这些字符串用作命令和参数数组(与命令数组相反)

像这样的东西...

String[] arguments = new String[] { "ffmpeg", 
"-i", 
"rtmp://127.0.0.1/vod/sample start=1500 stop=24000",
"-re",
...
};
于 2010-07-06T21:05:38.007 回答
1

听起来您需要在参数字符串中转义引号。这很简单,只需使用前面的反斜杠即可。

例如

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";
于 2010-07-06T21:45:21.747 回答