3

I've been searching for several days on how to incorporate the '-show_format_entry' into my Python script that uses ffprobe to extract the metadata from all the audio and visual files in a directory. However, I don't want everything that format gives back.

My current script:

    #! 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) 

The output is something like:

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

What I want, is to be able to use the generic ffprobe option, '-show_format_entry' and specify '-show_format_entry filename', '-show_format_entry duration', '-show_format_entry size' to get only the filename, duration and size in the output.

I've also tried grep|duration after 'filename' in the cmnd to isolate those values in the output but it doesn't work. Also, if I could, I would like to get rid of the [FORMAT][/FORMAT] tags in the ouput but that is not totally necessary. Any feedback is greatly appreciated.

user1499768
  • 31
  • 1
  • 3

3 Answers3

3

To format the output, use the -of option. Several of them are available, INI style, flat, JSON. See http://www.ffmpeg.org/ffprobe.html#Writers for details.

$ 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]
slv
  • 666
  • 6
  • 12
2

This worked for me using FFProbe 1.1.3 on Windows 7:

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

The FFProbe doc http://ffmpeg.org/ffprobe.html states the show_format_entry option is deprecated, and to use show_entries instead.

anon
  • 21
  • 2
1

This is probably not the most elegant solution, but you could filter the lines before printing them. Instead of

print out

you can have something like

for line in out.split('\n'):
    line = line.strip()
    if (line.startswith('filename=') or
        line.startswith('duration=') or
        line.startswith('size=')):
        print line
Martin Maillard
  • 2,751
  • 19
  • 24
  • Thank you so Morphyn! I really appreciate your proposed solution. I'm definitely going to mess around with that. I wanted to try and use the built-in option (show_format_entry) for ffprobe since it does seem, from reading the documentation, that its output would be similar to the output of your solution. But when I add it to the cmnd and run the script it just prints 3 or 4 blank lines to screen. Thank you for your help!!! – user1499768 Jul 05 '12 at 01:10