HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Trying to get output of ffprobe into variable

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ffprobeintotryingoutputgetvariable

Problem

I am trying to grab the ffprobe values from the video file into a variable that I can compare against others or move the value into a database. The question I have; Is there a better way of doing it than below?

I don't like the multiple if/elif/line.startswith statements and I am not sure of split is the best way of getting the ffprobe values?

```
#!/usr/bin/python
import os, sys, subprocess, shlex, re, fnmatch
from subprocess import call

videoDrop_dir="/mnt/VoigtKampff/Temp/_Jonatha/test_drop"

for r,d,f in os.walk(videoDrop_dir):
for files in f:
print "Files: %s" % files
if files.startswith(('._', '.')):
print "This file: %s is not valid" % files
elif files.endswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
fpath = os.path.join(r, files)
def probe_file(fpath):
cmnd = ['ffprobe', '-show_format', '-show_streams', '-pretty', '-loglevel', 'quiet', fpath]
p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print files
out, err = p.communicate()
print "===============================OUTPUT START: %s ===============================" % files
print out
for line in out.split('\n'):
line = line.strip()
if line.startswith('codec_name='):
s = line
codec_name = s.split('codec_name=', 1)
print "Codec is: %s" % codec_name[1]
codec_1 = codec_name[1]
elif line.startswith('codec_type='):
s = line
codec_type = s.split('codec_type=', 1)
print "Codec type is: %s" % codec_type[1]
codec_type1 = codec_type[1]
elif line.startswith('codec_long_name=', 1):
s = line
codec_long_name = s.split('codec_long_name=', 1)

Solution

#!/usr/bin/python
import os, sys, subprocess, shlex, re, fnmatch
from subprocess import call 

videoDrop_dir="/mnt/VoigtKampff/Temp/_Jonatha/test_drop"


Python convention is to ALL_CAPS for global constants

for r,d,f in os.walk(videoDrop_dir):


I suggest not using single letter variable names here, it just obfuscates the meanings.

for files in f:


files is a single file, not plural, it should be file or filename.

print "Files: %s" % files
        if files.startswith(('._', '.')):


Since ._ starts with . there is no reason to check both.

print "This file: %s is not valid" % files
        elif files.endswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
            fpath = os.path.join(r, files)
            def probe_file(fpath):


Why do you define a function inline and then just call it? Either get rid of the function or move the function outside of the loop and call it.

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


Don't needlessly abbreivate, call it command

p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                print files
                out, err = p.communicate()
                print "===============================OUTPUT START: %s ===============================" % files
                print out


See subprocess.check_output. It calls the command returns the string so you can simplify this last but of code.

for line in out.split('\n'):
                line = line.strip()
                if line.startswith('codec_name='):
                    s = line 
                    codec_name = s.split('codec_name=', 1)
                    print "Codec is: %s" % codec_name[1]
                    codec_1 = codec_name[1]


Rather then repeating this bunches of times, do something like

decoder_configuration = {}
for line in out.splitlines():
    if '=' in line:
        key, value = line.split('=')
        decoder_configuration[key] = value

Then all of the pieces of data will be held in decoder_configuration. You can do what you want with it from there.

            print "===============================OUTPUT FINISH: %s ===============================" % files
            if err: 
                print "===============================ERROR: %s ===============================" % files
                print err
        probe_file(fpath)
    else:
        if not files.endswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
            print "This file: %s is not a valid video file" % files

Code Snippets

#!/usr/bin/python
import os, sys, subprocess, shlex, re, fnmatch
from subprocess import call 

videoDrop_dir="/mnt/VoigtKampff/Temp/_Jonatha/test_drop"
for r,d,f in os.walk(videoDrop_dir):
for files in f:
print "Files: %s" % files
        if files.startswith(('._', '.')):
print "This file: %s is not valid" % files
        elif files.endswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
            fpath = os.path.join(r, files)
            def probe_file(fpath):

Context

StackExchange Code Review Q#23277, answer score: 3

Revisions (0)

No revisions yet.