Python 尝试通过子进程发送命令以使用ffmpeg进行操作

Python 尝试通过子进程发送命令以使用ffmpeg进行操作,python,shell,command-line,ffmpeg,subprocess,Python,Shell,Command Line,Ffmpeg,Subprocess,我试图构建一个脚本,通过Python3中的ffmpeg转换视频文件。 通过Windows PowerShell,我通过以下命令成功获得了所需的结果: ffmpeg -i test.webm -c:v libx264 converted.mp4 但是,如果我尝试通过以下代码在python中重复相同的操作: import subprocess from os import getcwd print(getcwd()) # current directory subprocess.call(["f

我试图构建一个脚本,通过Python3中的ffmpeg转换视频文件。 通过Windows PowerShell,我通过以下命令成功获得了所需的结果:

ffmpeg -i test.webm -c:v libx264 converted.mp4
但是,如果我尝试通过以下代码在python中重复相同的操作:

import subprocess
from os import getcwd

print(getcwd()) # current directory
subprocess.call(["ffmpeg", " -f test.webm -c libx264 converted.mp4"])
我得到以下错误:

Output #0, mp4, to ' -f test.webm -c libx264 converted.mp4':
Output file #0 does not contain any stream
我在文件所在的正确文件夹中。您是否有更好的方法通过Python在shell中执行命令?最好在不同的平台上工作。

尝试以下方法:

import shlex

cmd = shlex.split("ffmpeg -f test.webm -c libx264 converted.mp4")
subprocess.call(cmd)
您需要将每个参数作为列表中的单个元素传递,这就是argv的工作方式,或者让shell进行拆分:

subprocess.call("ffmpeg -f test.webm -c libx264 converted.mp4", shell=True)

明亮的必须将参数作为列表直接传递才能使其正常工作。您的代码块被解释为列表列表,并返回一个错误。这是最终版本:import subprocess import shlex args=shlex.splitffmpeg-i test.webm-c:v libx264 converted.mp4-y subprocess.callargs