Created
October 24, 2014 06:44
-
-
Save meoow/5fb269ea081d1ea1c5c8 to your computer and use it in GitHub Desktop.
Get basic information of media file from FFmpeg's command line output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import subprocess as subp | |
import sys | |
import re | |
bitratePtn = re.compile(r'^\s*Duration:.+, (bitrate: (\d+) kb/s).*$') | |
videoPtn = re.compile(r'^\s*Stream #\d+:\d+.*?: Video: ([^,]+).*$') | |
audioPtn = re.compile(r'^\s*Stream #\d+:\d+.*?: Audio: ([^,]+).*$') | |
resolutionPtn = re.compile(r'^.*?(\d{1,4}x\d{1,4})[, ].*$') | |
hzPtn = re.compile(r'^.*?(\d+ Hz).*$') | |
sbitratePtn = re.compile(r'^.*?(\d+ kb/s).*$') | |
fpsPtn = re.compile(r'^.*?(\d+(\.\d+)?) fps,.*$') | |
def mediainfo(filename): | |
bitrate = '' | |
vbitrate = '' | |
abitrate = '' | |
resolution = '' | |
vcodec = '' | |
acodec = '' | |
hz = '' | |
fps = '' | |
cmd = subp.Popen(['ffmpeg', '-hide_banner','-i', filename], stdout = subp.PIPE, stderr = subp.PIPE) | |
for l in cmd.stderr: | |
l = l.strip() | |
if not bitrate: | |
a = bitratePtn.match(l) | |
if a: | |
bitrate = a.group(2) | |
continue | |
if not vcodec: | |
a = videoPtn.match(l) | |
if a: | |
vcodec = a.group(1).split()[0] | |
try: | |
resolution = resolutionPtn.match(l).group(1).split('x') | |
except AttributeError: | |
pass | |
try: | |
vbitrate = sbitratePtn.match(l).group(1).split()[0] | |
except AttributeError: | |
pass | |
try: | |
fps = fpsPtn.match(l).group(1).split()[0] | |
except AttributeError: | |
pass | |
continue | |
if not acodec: | |
a = audioPtn.match(l) | |
if a: | |
acodec = a.group(1).split()[0] | |
try: | |
hz = hzPtn.match(l).group(1).split()[0] | |
except AttributeError: | |
pass | |
try: | |
abitrate = sbitratePtn.match(l).group(1).split()[0] | |
except AttributeError: | |
pass | |
continue | |
cmd.wait() | |
return {'bitrate':bitrate, "vcodec":vcodec, 'resolution':resolution, | |
'vbitrate':vbitrate, 'fps':fps, | |
"acodec":acodec, "sampling":hz, 'abitrate':abitrate} | |
if __name__ == '__main__': | |
filename = sys.argv[1] | |
print mediainfo(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment