如何让Python找到ffprobe?

3
我在我的Mac(macOS Sierra)上安装了ffmpegffprobe,并将它们的路径添加到了PATH中。我可以从终端运行它们。
我正在尝试使用以下代码通过ffprobe获取视频文件的宽度和高度:
import subprocess
import shlex
import json


# function to find the resolution of the input video file
def findVideoResolution(pathToInputVideo):
    cmd = "ffprobe -v quiet -print_format json -show_streams"
    args = shlex.split(cmd)
    args.append(pathToInputVideo)
    # run the ffprobe process, decode stdout into utf-8 & convert to JSON
    ffprobeOutput = subprocess.check_output(args).decode('utf-8')
    ffprobeOutput = json.loads(ffprobeOutput)

    # find height and width
    height = ffprobeOutput['streams'][0]['height']
    width = ffprobeOutput['streams'][0]['width']

    return height, width

h, w = findVideoResolution("/Users/tomburrows/Documents/qfpics/user1/order1/movie.mov")
print(h, w)

抱歉,我不能提供一个 MCVE,因为这段代码不是我写的,而且我也不是很清楚它是如何工作的。
它会显示以下错误:
Traceback (most recent call last):
  File "/Users/tomburrows/Dropbox/Moviepy Tests/get_dimensions.py", line 21, in <module>
    h, w = findVideoResolution("/Users/tomburrows/Documents/qfpics/user1/order1/movie.mov")
  File "/Users/tomburrows/Dropbox/Moviepy Tests/get_dimensions.py", line 12, in findVideoResolution
    ffprobeOutput = subprocess.check_output(args).decode('utf-8')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 626, in check_output
    **kwargs).stdout
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 693, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'

如果Python没有从PATH文件中读取,我该如何指定ffprobe所在的位置?
编辑: 看起来Python路径与我的Shell路径不一致。 在每个程序的开头使用os.environ["PATH"]+=":/the_path/of/ffprobe/dir"可以让我使用ffprobe,但为什么我的Python路径可能与Shell路径不同?

为了验证确实是 $PATH 相关的问题,为什么不直接给出完整路径,而不是依赖于 shell 环境来执行呢?例如,使用 cmd = "/full/path/to/ffprobe -v quiet -print_format json -show_streams" - boardrider
1个回答

2

您可以使用

import os
print os.environ['PATH']

验证/确认ffprobe是否在您的Python环境中。根据您遇到的错误,它很可能不在其中。


是的,您的代码输出中没有出现在终端运行'echo $PATH'时显示的ffmpeg路径。那么我该如何更新Python路径呢? - Tom Burrows
为了暂时解决这个问题,将ffprobe路径添加到Python环境中,即添加*os.environ["PATH"]+=":/the_path/of/ffprobe/dir"*。 但是,您应该考虑为什么Python路径与您的shell路径不对齐。 - Burns
@Burns:系统路径和Python PYTHONPATH 完全没有关系。你需要更改 PATH,以便可以从任何目录运行你的命令(ffmpeg)。请在 Google 上搜索“如何更改 PATH”以获取你特定 shell 的方法。 - Vlad K.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接