From d6be4b20f796818ce93e42589217435c49695ae7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 11 Jun 2012 12:11:06 +0200 Subject: [PATCH] add ffprobe wrapper --- ox/file.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/ox/file.py b/ox/file.py index 5c95b43..58a9157 100644 --- a/ox/file.py +++ b/ox/file.py @@ -13,6 +13,12 @@ from ox.utils import json __all__ = ['sha1sum', 'oshash', 'avinfo', 'makedirs'] +def cmd(program): + local = os.path.expanduser('~/.ox/bin/%s' % program) + if os.path.exists(local): + program = local + return program + def sha1sum(filename): sha1 = hashlib.sha1() file=open(filename) @@ -62,10 +68,7 @@ def oshash(filename): def avinfo(filename): if os.path.getsize(filename): - ffmpeg2theora = 'ffmpeg2theora' - local = os.path.expanduser('~/.ox/bin/ffmpeg2theora') - if os.path.exists(local): - ffmpeg2theora = local + ffmpeg2theora = cmd('ffmpeg2theora') p = subprocess.Popen([ffmpeg2theora], stdout=subprocess.PIPE, stderr=subprocess.PIPE) info, error = p.communicate() version = info.split('\n')[0].split(' - ')[0].split(' ')[-1] @@ -90,6 +93,83 @@ def avinfo(filename): return {'path': filename, 'size': 0} +def ffprobe(filename): + p = subprocess.Popen([ + cmd('ffprobe'), + '-show_format', + '-show_streams', + '-print_format', + 'json', + '-i', filename + + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + info, error = p.communicate() + ffinfo = json.loads(info) + + def fix_value(key, value): + if key == 'r_frame_rate': + value = value.replace('/', ':') + elif key == 'bit_rate': + value = float(value) / 1000 + elif key == 'duration': + value = float(value) + elif key == 'size': + value = int(value) + return value + + info = {} + for key in ('duration', 'size', 'bit_rate'): + info[{ + 'bit_rate': 'bitrate' + }.get(key, key)] = fix_value(key, ffinfo['format'][key]) + info['audio'] = [] + info['video'] = [] + info['metadata'] = ffinfo['format'].get('tags', {}) + for s in ffinfo['streams']: + tags = s.pop('tags', {}) + for t in tags: + info['metadata'][t] = tags[t] + if s.get('codec_type') in ('audio', 'video'): + stream = {} + keys = [ + 'codec_name', + 'width', + 'height', + 'bit_rate', + 'index', + 'display_aspect_ratio', + 'sample_rate', + 'channels', + ] + if s['codec_type'] == 'video': + keys += [ + 'sample_aspect_ratio', + 'r_frame_rate', + 'pix_fmt', + ] + + for key in keys: + if key in s: + stream[{ + 'codec_name': 'codec', + 'bit_rate': 'bitrate', + 'index': 'id', + 'r_frame_rate': 'framerate', + 'sample_rate': 'samplerate', + 'pix_fmt': 'pixel_format', + }.get(key, key)] = fix_value(key, s[key]) + info[s['codec_type']].append(stream) + else: + pass + #print s + for v in info['video']: + if not 'display_aspect_ratio' in v and 'width' in v: + v['display_aspect_ratio'] = '%d:%d' % (v['width'], v['height']) + v['pixel_aspect_ratio'] = '1:1' + info['oshash'] = ox.oshash(filename) + info['path'] = os.path.basename(filename) + return info + def makedirs(path): if not os.path.exists(path): try: