【问题标题】:How can I get Python to find ffprobe?如何让 Python 找到 ffprobe?
【发布时间】:2017-05-08 04:43:52
【问题描述】:

我在我的 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"

标签: python ffmpeg path subprocess pythonpath


【解决方案1】:

你可以使用

import os
print os.environ['PATH']

验证/验证 ffprobe 是否在您的 python 环境中。根据你的错误,应该不会。

【讨论】:

  • 是的,当我从终端运行 'echo $PATH' 时,您的代码输出中没有 ffmpeg 的路径。那我该如何更新python路径呢?
  • 临时解决问题,将ffprobe路径添加到python环境中,即添加os.environ["PATH"]+=":/the_path/of/ffprobe/dir ".但是,您应该考虑为什么 python 路径与您的 shell 路径不一致。
  • @Burns:System PATH 和 Python PYTHONPATH 完全没有任何关系。您需要更改 PATH 以便您的命令 (ffmpeg) 可以从任何目录运行。谷歌“如何更改 PATH”为您的特定外壳。
猜你喜欢
  • 2019-07-25
  • 2018-12-15
  • 2014-01-05
  • 2021-04-07
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
相关资源
最近更新 更多