如何将ffprobe元数据作为变量在python中解析
当我运行如何将ffprobe元数据作为变量在python中解析,python,ffmpeg,terminal,subprocess,ffprobe,Python,Ffmpeg,Terminal,Subprocess,Ffprobe,当我运行ffprobe时,我得到的标准元数据如下: ffprobe version 4.3.1 Copyright (c) 2007-2020 the FFmpeg developers built with Apple clang version 11.0.3 (clang-1103.0.32.62) configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --e
ffprobe
时,我得到的标准元数据如下:
ffprobe version 4.3.1 Copyright (c) 2007-2020 the FFmpeg developers
built with Apple clang version 11.0.3 (clang-1103.0.32.62)
configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-librsvg --enable-libtheora --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex --enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype --enable-libfribidi --disable-libjack --disable-libopencore-amrnb --disable-libopencore-amrwb --disable-libxcb --disable-libxcb-shm --disable-libxcb-xfixes --disable-indev=jack --enable-opencl --disable-outdev=xv --enable-audiotoolbox --enable-videotoolbox --enable-sdl2 --disable-securetransport --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --enable-libdav1d --arch=x86_64 --enable-x86asm --enable-libx265 --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '../directConversion/200mbs105_55.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Duration: 00:01:12.43, start: 0.000000, bitrate: 213963 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], 213828 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : Core Media Video
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : Core Media Audio
我想从python中获取这些信息,这样我就可以解析这个输出并在代码中使用其中的一些内容。然而,我不能让它工作
我尝试了以下方法:
data = subprocess.call(f'ffprobe {video}', shell=True)
data = subprocess.check_output(f'ffprobe {vid}', shell=True)
data = subprocess.Popen(f'ffprobe {vid}', shell=True)
data = subprocess. Popen(f'ffprobe {vid}', stdout=subprocess.PIPE ).communicate()[0]
data = run(f'ffprobe {vid}', capture_output=True).stdout
如果我包括
shell=True
,则在终端中打印正确的信息,但我会得到数据的空字符串。对于没有shell=True
的命令,我得到file not found错误
,它会中断。我该怎么办?我从SO那里得到了所有这些解决方案,它们似乎对其他人有效。您可以使用shlex.split
或将FFprobe的参数放入列表中
在Windows操作系统中,您可以使用sp.run(f'ffprobe{video}'
…
在Linux和Mac中,Python尝试使用空格作为文件名执行命令
例如:'ffprobe vid.mp4'
被视为单个可执行命令(文件名带空格)
您可以使用参数列表:
sp.run(['ffprobe',f'{video}']
或者使用shlex.split
将shell命令拆分为列表:
sp.run(shlex.split(f'ffprobe{video}'))
对于Python中的FFprobe输出的简单解析:
- 使用json
参数的-执行ffprobe,并以json格式获取输出
使用json.loads
将输出字符串转换为字典
以下是将FFprobe的输出读入字典的代码示例:
import subprocess as sp
import shlex
import json
video = 'vid.mkv'
# Execute ffprobe (to show streams), and get the output in JSON format
data = sp.run(shlex.split(f'ffprobe -loglevel error -show_streams -of json {video}'), capture_output=True).stdout
# Same - using a list of arguments:
# data = sp.run(['ffprobe', '-loglevel', 'error', '-show_streams', '-of', 'json', f'{video}'], capture_output=True).stdout
# Convert data from JSON string to dictionary
d = json.loads(data)
print(d) # Print the dictionary for testing
注:
- 我使用了
-show\u streams
参数作为示例。
如果缺少(或太多)信息,请查找相关的参数集
- 该示例假定ffprobe位于执行路径中
ffprobe
在您的shell路径中,但不在Python上下文的路径中。作为一种临时解决方案,您可以使用完整路径。Linux中的默认路径是/usr/bin/ffprobe
。如果不存在,请在shell中使用哪个ffprobe
。注意:为了便于解析,您可以要求ffprobe以JSON格式返回数据,然后t将字符串转换为Python字典。当我运行哪个ffprobe
时,我会得到/opt/local/bin/ffprobe
。我应该如何处理这些信息?还有,我应该如何按照您的建议获得JSON?将我的命令更改为f'/opt/local/bin/ffprobe{vid}
结果没有任何变化您使用的是Mac还是Linux?我想我无法帮助您找到路径。对于JSON格式,请使用ffprobe-print\u format JSON
。然后使用dict=JSON.load(data)
。我想我知道问题:您应该使用shlex.split
:data=run(shlex.split(f'ffprobe{vid}-print\u format JSON')),capture_output=True)。标准输出