【问题标题】:How to get ffprobe metadata as variable to parse in python如何获取 ffprobe 元数据作为变量以在 python 中解析
【发布时间】: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


【解决方案1】:

您可以使用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 输出:

  • 使用-of 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_streams 参数为例。
    如果缺少(或过多)信息,请查找相关的参数集。
  • 该示例假定 ffprobe 在执行路径中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多