Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用ffprobe'-显示"格式"条目';选项_Python_Ffprobe - Fatal编程技术网

Python 使用ffprobe'-显示"格式"条目';选项

Python 使用ffprobe'-显示"格式"条目';选项,python,ffprobe,Python,Ffprobe,几天来,我一直在研究如何将“-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_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]标记,但这并不是完全必要的。
非常感谢您的反馈

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

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

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

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

FFProbe文档声明不推荐使用“显示格式”条目选项,若要使用“显示格式”条目,请使用“代码”选项的“代码”来格式化输出。其中有几个是可用的,INI样式,平面,JSON。有关详细信息,请参阅

$ 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]

谢谢你,莫芬!我真的很感激你提出的解决方案。我肯定会搞砸的。我想尝试使用ffprobe的内置选项(show_format_entry),因为从阅读文档来看,它的输出与您的解决方案的输出类似。但当我将它添加到cmnd并运行脚本时,它只会在屏幕上打印3或4个空行。谢谢你的帮助!!!