我试图在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
使用没有像“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
这是代码
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)
顺便说一句,我在MT7688上运行此命令。
谢谢
您必须拆分选项:
command = [ffm_path, '-i', ffm_input, '-r', ffm_rate, '-f', ffm_format, '-vframes', ffm_nb_frames, ffm_filename]
ffm_input
,ffm_rate
,ffm_format
应仅包含以下值:
ffm_input = self.path
ffm_rate = '5'
ffm_nd_frames = str(nb_frames)
ffm_format = 'v412' if platform.system() == 'Linux' else 'image2'
当你传递一个列表时,没有解析,所以-r 5
被作为一个参数,但程序希望你提供两个单独的参数-r
,然后是5
。
基本上,如果将它们作为单个元素放在列表中,就像在命令行中引用它们一样:
$ echo "-n hello"
-n hello
$ echo -n hello
hello$
在第一个例子中,echo
看到单个参数-n hello
。由于它不匹配任何选项,它只是打印它。在第二种情况下,echo
看到两个参数-n
和hello
,第一个是抑制行尾的有效选项,你可以看到提示是在hello
之后打印而不是在它自己的行上。