【问题标题】:Using ffprobe '-show_format_entry' option使用 ffprobe '-show_format_entry' 选项
【发布时间】:2012-07-04 07:12:12
【问题描述】:

几天来,我一直在寻找如何将“-show_format_entry”合并到我的 Python 脚本中,该脚本使用 ffprobe 从目录中的所有音频和视频文件中提取元数据。 但是,我不希望格式返回的所有内容。

我当前的脚本:

    #! usr/bin/python
import os, sys, subprocess, shlex, re, fnmatch
from subprocess import call

def probe_file(filename):
    cmnd = ['ffprobe', '-show_format', ,'-pretty', '-loglevel' filename]
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err =  p.communicate()
    print out

mp3box=[]
for root, dirs, files in os.walk('/home/username/Music'):
  for fname in files:
    name,ext=os.path.splitext(fname)
    if fname.lower().endswith('.mp3'):
        mp3box.append(fname)
        probe_file(fname) 

输出类似于:

[FORMAT]
filename=test.mp3
nb_streams=1
format_name=mp3
format_long_name=MPEG audio layer 2/3
start_time=0:00:00.000000
duration=0:03:32.943917
size=2.437 Mibyte
bit_rate=95.999 Kbit/s
TAG:album=compliments of grimriper2u@yahoo.com
TAG:artist=Charley Barnet, V=Trudy Richard
TAG:disc=sound changed and copyright for public Domain. not for resale.
TAG:genre=Jazz
TAG:TLEN=000000212342
TAG:title= Ill Wind 
TAG:date=1949
[/FORMAT]

[FORMAT]
filename=barnet.mp3
nb_streams=1
format_name=mp3
format_long_name=MPEG audio layer 2/3
start_time=0:00:00.000000
duration=0:03:32.943917
size=2.437 Mibyte
bit_rate=95.999 Kbit/s
TAG:album=compliments of grimriper2u@yahoo.com
TAG:artist=Charley Barnet, V=Trudy Richard
TAG:disc=sound changed and copyright for public Domain. not for resale.
TAG:genre=Jazz
TAG:TLEN=000000212342
TAG:title= Ill Wind 
TAG:date=1949
[/FORMAT]

我想要的是能够使用通用 ffprobe 选项“-show_format_entry”并指定 '-show_format_entry filename', '-show_format_entry duration', '-show_format_entry size' 仅获取输出中的文件名、持续时间和大小。

我还尝试在 cmnd 中的 'filename' 之后使用 grep|duration 来隔离输出中的这些值,但它不起作用。另外,如果可以的话,我想去掉输出中的 [FORMAT][/FORMAT] 标签,但这并不是完全必要的。 非常感谢任何反馈。

【问题讨论】:

    标签: python ffprobe


    【解决方案1】:

    这对我在 Windows 7 上使用 FFProbe 1.1.3 有效:

    ffprobe -show_entries format=filename,size,duration -i d:\demos\demo1.mp3 2>NUL
    

    FFProbe 文档 http://ffmpeg.org/ffprobe.html 声明 show_format_entry 选项已被弃用,而改为使用 show_entries。

    【讨论】:

      【解决方案2】:

      要格式化输出,请使用-of 选项。其中有几个是可用的,INI 风格、平面、JSON。详情请见http://www.ffmpeg.org/ffprobe.html#Writers

      $ ffprobe -show_entries format=filename,size,duration -of json -i DSC_2309.MOV 2>/dev/null
      {
          "format": {
              "filename": "DSC_2309.MOV",
              "duration": "14.139125",
              "size": "19488502",
              "tags": {
      
              }
          }
      }
      
      $ ffprobe -show_entries format=filename,size,duration -of flat -i DSC_2309.MOV 2>/dev/null
      format.filename="DSC_2309.MOV"
      format.duration="14.139125"
      format.size="19488502"
      
      $ ffprobe -show_entries format=filename,size,duration -of ini -i DSC_2309.MOV 2>/dev/null
      # ffprobe output
      
      [format]
      filename=DSC_2309.MOV
      duration=14.139125
      size=19488502
      
      [format.tags]
      

      【讨论】:

        【解决方案3】:

        这可能不是最优雅的解决方案,但您可以在打印之前过滤线条。而不是

        print out
        

        你可以有类似的东西

        for line in out.split('\n'):
            line = line.strip()
            if (line.startswith('filename=') or
                line.startswith('duration=') or
                line.startswith('size=')):
                print line
        

        【讨论】:

        • 非常感谢 Morphyn!我非常感谢您提出的解决方案。我肯定会搞砸的。我想尝试使用 ffprobe 的内置选项 (show_format_entry),因为从阅读文档来看,它的输出似乎与您的解决方案的输出相似。但是当我将它添加到 cmnd 并运行脚本时,它只会在屏幕上打印 3 或 4 个空白行。谢谢你的帮助!!!
        猜你喜欢
        • 1970-01-01
        • 2012-12-17
        • 2020-12-02
        • 2018-06-24
        • 2015-03-03
        • 2017-06-03
        • 2015-09-24
        • 2012-04-11
        • 2018-11-21
        相关资源
        最近更新 更多