带有引号的Java Runtime.getRuntime()。exec()

时间:2022-02-05 23:08:03

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 * but I can't seem to find a solution.

我试图通过Linux上的exec调用运行ffmpeg。但是我必须在命令中使用引号(ffmpeg需要它)。我一直在浏览java doc for processbuilder和exec以及*上的问题,但我似乎无法找到解决方案。

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.

我需要在下面的参数字符串中插入引号。注意,由于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";

Any help would be greatly appreciated.

任何帮助将不胜感激。

2 个解决方案

#1


6  

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)

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

Something like this ...

这样的东西......

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

#2


1  

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

#1


6  

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)

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

Something like this ...

这样的东西......

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

#2


1  

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