如何获取Python找到ffprobe? [英] How can I get Python to find ffprobe?
问题描述
我试图使用ffprobe使用以下代码获取视频文件的宽度和高度:
import subprocess
import shlex
import json
#函数查找分辨率输入视频文件
def findVideoResolution(pathToInputVideo):
cmd =ffprobe -v quiet -print_format json -show_streams
args = shlex.split(cmd)
args.append (pathToInputVideo)
#运行ffprobe进程,将stdout解码为utf-8&转换为JSON
ffprobeOutput = subprocess.check_output(args).decode('utf-8')
ffprobeOutput = json.loads(ffprobeOutput)
#查找高度和宽度
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,因为我没有写这段代码,我真的不知道它是如何工作的。
它出现以下错误:
追溯(最近的调用最后):
文件/ Users / tomburrows / Dropbox / Moviepy测试/ get_dimensions.py,第21行,< module>
h,w = findVideoResolution(/ Users / tomburrows / Documents / qfpics / user1 / order1 / movie.mov)
文件/ Users / tomburrows / Dropbox / Moviepy测试/ get_dimensions.py 12,在findVideoResolution
ffprobeOutput = subprocess.check_output(args).decode('utf-8')
文件/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ subprocess.py,第626行,check_output
** kwargs).stdout
文件/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py,行693,在运行
与Popen(* popenargs,** kwargs)作为进程:
文件/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py ,第947行,__init__
restore_signals,start_new_session)
文件/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py,第1551行,在_execute_child
raise child_exception_type(errno_num,err_msg)
FileNotFoundError:[Errno 2]没有这样的文件或目录:'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环境中。根据你的错误,可能不会。
I have ffmpeg and ffprobe installed on my mac (macOS Sierra), and I have added their path to PATH. I can run them from terminal.
I am trying to use ffprobe to get the width and height of a video file using the following code:
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)
I am sorry I cannot provide a MCVE, as I didn't write this code, and I don't really know how it works.
It gives the following error:
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'
If python is not reading from the PATH file, how can I specify where ffprobe is?
EDIT:
It appears the python path is not aligned with my shell path.
Using os.environ["PATH"]+=":/the_path/of/ffprobe/dir"
at the beginning of each program allows me to use ffprobe, but why might my python path not be the same as my shell path?
You may use
import os
print os.environ['PATH']
to verify/validate that ffprobe is in your python environment. According to the error you have, it is likely not.
这篇关于如何获取Python找到ffprobe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!