Java Runtime.getRuntime().exec() 带引号

问题描述 投票:0回答:3

我正在尝试通过 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";

任何帮助将不胜感激。

java linux exec runtime.exec
3个回答
6
投票

制作一个数组!

exec 可以采用字符串数组,这些字符串用作命令和参数数组(而不是命令数组)

类似这样的事情...

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

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

0
投票

我打赌这些建议都不适合您,因为正如您所指定的,“rtmp://127.0.0.1/vod/sample start=1500 stop=24000”必须在其周围加上引号,并将参数传递给带有引号的 exec因为 "\"rtmp://127.0.0.1/vod/sample start=1500 stop=24000\"" 不会起作用,因为 exec 会忽略它们,即使在 String[] 数组内也是如此。不幸的是,我认为没有解决方案

© www.soinside.com 2019 - 2024. All rights reserved.