From f02d42712d3d3b76aa135847f7f56363a11c3082 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 24 Dec 2014 23:18:29 +0100 Subject: [PATCH] dont throw exception for invalid files --- ox/file.py | 113 +++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/ox/file.py b/ox/file.py index 2b0bd2d..9ef0fcd 100644 --- a/ox/file.py +++ b/ox/file.py @@ -219,63 +219,66 @@ def ffprobe(filename): 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', {}) - language = None - for t in tags: - if t == 'language': - language = tags[t] - else: - info['metadata'][t] = tags[t] - if s.get('codec_type') in ('audio', 'video'): - stream = {} - if language and language != 'und': - stream['language'] = language - 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', + if not 'format' in ffinfo: + info['error'] = 'badfile' + else: + 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', {}) + language = None + for t in tags: + if t == 'language': + language = tags[t] + else: + info['metadata'][t] = tags[t] + if s.get('codec_type') in ('audio', 'video'): + stream = {} + if language and language != 'und': + stream['language'] = language + 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', - 'sample_aspect_ratio': 'pixel_aspect_ratio', - }.get(key, key)] = fix_value(key, s[key]) - info[s['codec_type']].append(stream) - else: - pass - #print s - for v in info['video']: - k = 'display_aspect_ratio' - if not k in v and 'width' in v \ - or (k in v and v[k] == '0:1'): - v[k] = '%d:%d' % (v['width'], v['height']) - v['pixel_aspect_ratio'] = '1:1' + 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', + 'sample_aspect_ratio': 'pixel_aspect_ratio', + }.get(key, key)] = fix_value(key, s[key]) + info[s['codec_type']].append(stream) + else: + pass + #print s + for v in info['video']: + k = 'display_aspect_ratio' + if not k in v and 'width' in v \ + or (k in v and v[k] == '0:1'): + v[k] = '%d:%d' % (v['width'], v['height']) + v['pixel_aspect_ratio'] = '1:1' info['oshash'] = oshash(filename) info['path'] = filename return info