简体   繁体   English

如何获取 ffprobe 元数据作为变量在 python 中解析

[英]How to get ffprobe metadata as variable to parse in python

When I run ffprobe <video> , I get the standard metadata as below:当我运行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

I want to get this information from python so I can parse this output and use some of it in my code.我想从 python 获取此信息,以便我可以解析此 output 并在我的代码中使用其中的一些。 However, I can't get this to work.但是,我无法让它工作。

I have tried the following:我尝试了以下方法:

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

If I include shell=True , then the proper information prints in the terminal but I get an empty string for data .如果我包含shell=True ,那么终端中会打印正确的信息,但我会得到一个空字符串data For commands without shell=True I get file not found error and it breaks.对于没有shell=True的命令,我得到file not found error并且它中断了。 What should I do?我应该怎么办? I got all of these solutions from SO and they seem to work for others.我从 SO 那里得到了所有这些解决方案,它们似乎对其他人有用。

You may use shlex.split or put the arguments to FFprobe in a list.您可以使用shlex.split或将 arguments 到 FFprobe 放在一个列表中。

In Windows OS, you can use sp.run(f'ffprobe {video}' ...在 Windows 操作系统中,您可以使用sp.run(f'ffprobe {video}' ...
In Linux and Mac, Python tries to execute the command as file name with spaces .在 Linux 和 Mac 中,Python 尝试以带空格的文件名执行命令。
For example: 'ffprobe vid.mp4' is considered a single executable command (file name with space).例如: 'ffprobe vid.mp4'被视为单个可执行命令(带空格的文件名)。

You may use an arguments list:您可以使用 arguments 列表:
sp.run(['ffprobe', f'{video}'] ... sp.run(['ffprobe', f'{video}'] ...

Or use shlex.split for splitting the shell command to a list:或者使用shlex.split将 shell 命令拆分为一个列表:
sp.run(shlex.split(f'ffprobe {video}')) ... sp.run(shlex.split(f'ffprobe {video}')) ...


For simple parsing FFprobe output in Python:对于Python中的FFprobe output的简单解析:

  • Execute ffprobe with -of json argument, and get the output in JSON format.使用-of json参数执行ffprobe,得到JSON格式的output。
  • Convert the output string to dictionary using json.loads .使用json.loads将 output 字符串转换为字典。

Here is a code sample that reads the output of FFprobe into a dictionary:这是一个将FFprobe的output读入字典的代码示例:

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

Notes:笔记:

  • I used -show_streams argument as an example.我以-show_streams参数为例。
    In case there is missing (or too much) information, look for the relevant set of arguments.如果缺少(或过多)信息,请查找相关的 arguments 集。
  • The example assumes that ffprobe is in the execution path.该示例假定 ffprobe 在执行路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM