使用 CMD 错误输出子进程 ffmpeg/ffprobe

With CMD wrong output subprocess with ffmpeg/ffprobe

我在 cmd 中 运行 时遇到 ffmpeg 问题 我有正确的输出 "ffprobe passionfruit.mp4 -show_streams"

但是当我对子进程使用相同的方法时:

command = 'ffprobe "C:/Users/NMASLORZ/Downloads/passionfruit.mp4" -show_streams'
p = subprocess.Popen(command, universal_newlines=True, shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
text = p.stdout.read()
retcode = p.wait()
print (text)

我有这个输出: “'ffprobe' 未被识别为内部命令或外部命令、可执行程序或批处理文件。” 我尝试了每一个合成器,甚至在一个列表中我仍然有相同的输出

那是因为您的 Windows 终端读取系统的 PATH 或以其他方式定义了 ffprobe 的路径。当您 运行 Popenshell=True 时,它 运行 通过 shell 执行可能会或可能不会(您的情况)访问该路径。

可能的解决方案:

  1. 提供 ffprobe 的完整路径(最简单):
command = 'C:\whatever\ffprobe.exe "C:/Users/NMASLORZ/Downloads/passionfruit.mp4" -show_streams'
  1. 使用shell=False。可能不起作用,取决于您的系统。
command = ('ffprobe', 'C:/Users/NMASLORZ/Downloads/passionfruit.mp4', '-show_streams')
p = subprocess.Popen(command, universal_newlines=True, shell=False,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  1. 通过 env 变量添加 PATH 并在该 PATH 中添加 ffprobe.

If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly to Popen.

查看 docs 以获得进一步的指导。