使用ffprobe的“-show_format_entry”选项

3

我已经搜索了好几天,想要将“-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]标记,但这并非完全必要。任何反馈都将不胜感激。
3个回答

3

要格式化输出,请使用-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]

2

在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选项。

1

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

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),因为从文档中看来,它的输出结果与你的解决方案相似。但是当我将其添加到命令中并运行脚本时,它只会在屏幕上打印3或4个空白行。非常感谢你的帮助!!! - user1499768

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接