我在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路径不同?

最佳答案

您可以使用

import os
print os.environ['PATH']


验证/验证ffprobe在您的python环境中。根据您的错误,可能没有。

10-04 17:04