【发布时间】:2021-07-22 21:06:53
【问题描述】:
当我运行ffprobe <video> 时,我得到如下标准元数据:
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,则正确的信息会打印在终端中,但我会得到data 的空字符串。对于没有shell=True 的命令,我得到file not found error 并且它会中断。我该怎么办?我从 SO 那里得到了所有这些解决方案,它们似乎对其他人有用。
【问题讨论】:
-
ffprobe在您的 shell 路径中,但不在 Python 上下文的路径中。作为临时解决方案,您可以使用完整路径。 Linux 中的默认路径是/usr/bin/ffprobe。如果不存在,请在您的 shell 中使用which ffprobe。注意:为了便于解析,您可以要求FFprobe以JSON格式返回数据,并将字符串转换为Python字典。 -
当我运行
which ffprobe时,我得到/opt/local/bin/ffprobe。我应该如何处理这些信息?另外,我如何获得您推荐的 JSON? -
将我的命令更改为
f'/opt/local/bin/ffprobe {vid}不会导致任何变化 -
您使用的是 Mac 还是 Linux?我不认为我可以帮助你的路径。对于 JSON 格式,请使用
ffprobe -print_format json。然后使用dict = json.loads(data)。 -
我想我知道问题所在:你应该使用
shlex.split:data = run(shlex.split(f'ffprobe {vid} -print_format json'), capture_output=True).stdout
标签: python ffmpeg terminal subprocess ffprobe