python如何调用ffprobe_使用Python获取FFProbe信息

I've been attempting to figure this out for forever now (I'm new to programming) and I can't figure it out.

I'm attempting to build a script that will test the file, and give me output from which I can get information like "Audio Format" that I can then put into the filename. However, I can't even get the script to return any file info. I've hit a wall at inserting an input file...

So at this point I just need help getting it to spit out info based on the argvs I've thrown in. Hopefully I'll be able to figure out how to parse the audio info from that.

My attempt that seems to be close:

#!/usr/bin/python

import os, sys, subprocess, shlex, re

from subprocess import call

def probe_file(filename):

p = subprocess.Popen(['/opt/local/bin/ffprobe', '-show_format', '-pretty', '-loglevel quiet', -i filename], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

print filename

print p.communicate()

[probe_file (f) for f in os.listdir('.') if not f.startswith('.')]

解决方案

Few problems in your code

args list to Popen has last argument as -i filename which is a syntax error use '-i '+filename instead

shell=True is usually not needed and is unnecessary burden.

Other than that it seems to be working, are you not seeing output after fixing #1 ?

Edit: Looks like you are having problem with ffprobe commandline, so I installed it and changes you require are

My ffprobe (ffprobe 0.7.3-4:0.7.3-0ubuntu0.11.10.1) doesn't seems to accept -i flag, input file is just passed as last argument.

you need to pass -logelevel and option of logleve quiet as separate arguments i.e. [..., '-loglevel', 'quiet',..]

So after these changes here is a sample script

#!/usr/bin/python

import os, sys, subprocess, shlex, re

from subprocess import call

def probe_file(filename):

cmnd = ['ffprobe', '-show_format', '-pretty', '-loglevel', 'quiet', filename]

p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

print filename

out, err = p.communicate()

print "==========output=========="

print out

if err:

print "========= error ========"

print err

probe_file('drop.avi')

And I see the correct output:

==========output==========

[FORMAT]

filename=drop.avi

nb_streams=1

format_name=avi

format_long_name=AVI format

start_time=0:00:00.000000

duration=0:00:06.066667

size=660.000 Kibyte

bit_rate=891.217 Kbit/s

[/FORMAT]

========= error ========

ffprobe version 0.7.3-4:0.7.3-0ubuntu0.11.10.1, Copyright (c) 2007-2011 the Libav developers

built on Jan 4 2012 16:08:51 with gcc 4.6.1

configuration: --extra-version='4:0.7.3-0ubuntu0.11.10.1' --arch=amd64 --prefix=/usr --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --enable-libvpx --enable-runtime-cpudetect --enable-vaapi --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static

libavutil 51. 7. 0 / 51. 7. 0

libavcodec 53. 6. 0 / 53. 6. 0

libavformat 53. 3. 0 / 53. 3. 0

libavdevice 53. 0. 0 / 53. 0. 0

libavfilter 2. 4. 0 / 2. 4. 0

libswscale 2. 0. 0 / 2. 0. 0

libpostproc 52. 0. 0 / 52. 0. 0

Unsupported codec with id 114 for input stream 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值