Python: 将复杂 (ffmpeg) 参数传递给 Popen
Python: Passing complex (ffmpeg) arguments to Popen
这个 ffmpeg Popen 调用有效:
command = ['ffmpeg', '-y',
'-i', filename,
'-filter_complex', 'showwavespic',
'-colorkey', 'red',
'-frames:v', '1',
'-s', '800:30',
'-vsync', '2',
'/tmp/waveform.png']
process = sp.Popen( command, stdin=sp.PIPE, stderr=sp.PIPE)
process.wait()
但我需要使用 'compand, showwavespic' and this comma seems to be blocking the execution. I also need to pass all sorts of strange characters,例如列,以及您可以在 CLI 调用中找到的所有内容。
如何传递复杂参数?
这些只是普通的 Python 字符串。字符串值直接传递给 FFmpeg,shell.
没有任何解释
所以当您看到这样的命令行示例时,
ffmpeg -i input -filter_complex "showwavespic=s=640x120" -frames:v 1 output.png
首先,由于示例传递给 shell,我们必须 "undo" shell 引用。
ffmpeg
-i
input
-filter_complex
showwavespic=s=640x120
-frames:v
1
output.png
然后,我们将其放入Python列表中。
command = [
'ffmpeg',
'-i',
'input',
'-filter_complex',
'showwavespic=s=640x120',
'-frames:v',
'1',
'output.png',
]
如您所见,逗号、空格和大多数其他字符没有任何区别对待,因此您无需执行任何操作来引用它们。主要的特殊字符有\
和'
必须加引号,控制字符也必须加引号,NUL字符根本不能用
更复杂的例子
在shell中:
ffmpeg -i in.mp4 -ac 2 -filter_complex:a '[0:a]aresample=8000,asplit[l][r]' \
-map '[l]' -c:a pcm_s16le -f data /tmp/plot-waveform-ac1 \
-map '[r]' -c:a pcm_s16le -f data /tmp/plot-waveform-ac2
在Python中:
command = [
'ffmpeg',
'-i', 'in.mp4',
'-ac', '2',
'-filter_complex:a', '[0:a]aresample=8000,asplit[l][r]',
'-map', '[l]',
'-c:a', 'pcm_s16le',
'-f', 'data',
'/tmp/plot-waveform-ac1',
'-map', '[r]',
'-c:a', 'pcm_s16le',
'-f', 'data',
'/tmp/plot-waveform-ac2',
]
如您所见,非常简单。 Python 只是有点冗长,但更规则。
这个 ffmpeg Popen 调用有效:
command = ['ffmpeg', '-y',
'-i', filename,
'-filter_complex', 'showwavespic',
'-colorkey', 'red',
'-frames:v', '1',
'-s', '800:30',
'-vsync', '2',
'/tmp/waveform.png']
process = sp.Popen( command, stdin=sp.PIPE, stderr=sp.PIPE)
process.wait()
但我需要使用 'compand, showwavespic' and this comma seems to be blocking the execution. I also need to pass all sorts of strange characters,例如列,以及您可以在 CLI 调用中找到的所有内容。
如何传递复杂参数?
这些只是普通的 Python 字符串。字符串值直接传递给 FFmpeg,shell.
没有任何解释所以当您看到这样的命令行示例时,
ffmpeg -i input -filter_complex "showwavespic=s=640x120" -frames:v 1 output.png
首先,由于示例传递给 shell,我们必须 "undo" shell 引用。
ffmpeg
-i
input
-filter_complex
showwavespic=s=640x120
-frames:v
1
output.png
然后,我们将其放入Python列表中。
command = [
'ffmpeg',
'-i',
'input',
'-filter_complex',
'showwavespic=s=640x120',
'-frames:v',
'1',
'output.png',
]
如您所见,逗号、空格和大多数其他字符没有任何区别对待,因此您无需执行任何操作来引用它们。主要的特殊字符有\
和'
必须加引号,控制字符也必须加引号,NUL字符根本不能用
更复杂的例子
在shell中:
ffmpeg -i in.mp4 -ac 2 -filter_complex:a '[0:a]aresample=8000,asplit[l][r]' \
-map '[l]' -c:a pcm_s16le -f data /tmp/plot-waveform-ac1 \
-map '[r]' -c:a pcm_s16le -f data /tmp/plot-waveform-ac2
在Python中:
command = [
'ffmpeg',
'-i', 'in.mp4',
'-ac', '2',
'-filter_complex:a', '[0:a]aresample=8000,asplit[l][r]',
'-map', '[l]',
'-c:a', 'pcm_s16le',
'-f', 'data',
'/tmp/plot-waveform-ac1',
'-map', '[r]',
'-c:a', 'pcm_s16le',
'-f', 'data',
'/tmp/plot-waveform-ac2',
]
如您所见,非常简单。 Python 只是有点冗长,但更规则。