[英]Subprocess call invalid argument or option not found
我正在嘗試在 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
之后打印的,而不是在它自己的行上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.