子进程调用无效参数或未找到选项

Nic*_*ckB 0 python subprocess ffmpeg

我试图在 linux 上使用 subprocess.call() 调用 ffmpeg 命令,但我无法正确获取参数。之前,我使用了 os.system 并且它有效,但不推荐这种方法。

使用诸如“-i”之类的带破折号的参数会出现此错误

Unrecognized option 'i "rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream"'.
Error splitting the argument list: Option not found
Run Code Online (Sandbox Code Playgroud)

使用像“i”这样没有破折号的参数会出现这个错误

[NULL @ 0x7680a8b0] Unable to find a suitable output format for 'i rtsp://192.168.0.253:554/user=admin&password=&channel=0&stream=0.sdp?real_stream'
i rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream: Invalid argument
Run Code Online (Sandbox Code Playgroud)

这是代码

class IPCamera(Camera):
"""
    IP Camera implementation
"""
def __init__(self,
             path='\"rtsp://192.168.0.253:554/'
                  'user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream\"'):

    """
        Constructor
    """
    self.path = path

def __ffmpeg(self, nb_frames=1, filename='capture%003.jpg'):
    """
    """

    ffm_input = "-i " + self.path
    ffm_rate = "-r 5"
    ffm_nb_frames = "-vframes " + str(nb_frames)
    ffm_filename = filename

    if platform.system() == 'Linux':
        ffm_path = 'ffmpeg'
        ffm_format = '-f v4l2'

    else:
        ffm_path = 'C:/Program Files/iSpy/ffmpeg.exe'
        ffm_format = '-f image2'

    command = [ffm_path, ffm_input, ffm_rate, ffm_format, ffm_nb_frames, ffm_filename]
    subprocess.call(command)

    print(command)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我在 MT7688 上运行这个命令。

谢谢

Gia*_*tta 5

您必须拆分选项:

command = [ffm_path, '-i', ffm_input, '-r', ffm_rate, '-f', ffm_format, '-vframes',  ffm_nb_frames, ffm_filename]
Run Code Online (Sandbox Code Playgroud)

ffm_inputffm_rateffm_format应该只包含值:

ffm_input = self.path
ffm_rate = '5'
ffm_nd_frames = str(nb_frames)
ffm_format = 'v412' if platform.system() == 'Linux' else 'image2'
Run Code Online (Sandbox Code Playgroud)

当您传递一个列表时,不会进行任何解析,因此-r 5将其视为单个参数,但程序希望您提供两个单独的参数,-r后跟5.


基本上,如果您将它们作为单个元素放在列表中,就好像您在命令行中引用它们一样:

$ echo "-n hello"
-n hello
$ echo -n hello
hello$
Run Code Online (Sandbox Code Playgroud)

在第一个示例中echo看到一个参数-n hello。由于它不匹配任何选项,它只是打印它。在第二种情况下echo看到两个参数-nand hello,第一个是抑制行尾的有效选项,正如您所看到的,提示是在后面打印的,hello而不是在它自己的行上。