使用ffprobe“show_format_entry”选项

2024-06-01 00:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

几天来,我一直在寻找如何将'-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来隔离输出中的那些值,但它不起作用。另外,如果可以的话,我想去掉ouut中的[FORMAT][/FORMAT]标记,但这并不是完全必要的。 任何反馈都非常感谢。


Tags: name脚本formatforostagshowfilename
3条回答

要格式化输出,请使用-of选项。其中有几个是可用的,INI样式,flat,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]

这对我在Windows7上使用FFProbe 1.1.3很有效:

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

FFProbe dochttp://ffmpeg.org/ffprobe.html声明不推荐使用show_format_entry选项,而是使用show_entries。

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

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

相关问题 更多 >