|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import logging |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +parser = argparse.ArgumentParser(description='Run ffprobe on one or more files with JSON output') |
| 10 | +parser.add_argument('filenames', nargs='+', help='Filename(s)') |
| 11 | + |
| 12 | + |
| 13 | +class ExecException(Exception): |
| 14 | + def __init__(self, stderr_output): |
| 15 | + super(ExecException, self).__init__('Execution error') |
| 16 | + self.stderr_output = stderr_output |
| 17 | + |
| 18 | + |
| 19 | +def run_ffprobe(filename): |
| 20 | + args = ['ffprobe', '-show_format', '-of', 'json', filename] |
| 21 | + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 22 | + out, err = p.communicate() |
| 23 | + if p.returncode != 0: |
| 24 | + raise ExecException(err) |
| 25 | + return json.loads(out) |
| 26 | + |
| 27 | + |
| 28 | +def main(filenames): |
| 29 | + failed = False |
| 30 | + results = {} |
| 31 | + for filename in sys.argv[1:]: |
| 32 | + try: |
| 33 | + results[filename] = run_ffprobe(filename) |
| 34 | + except ExecException as e: |
| 35 | + logging.error('Error processing {}:'.format(filename)) |
| 36 | + logging.error(e.stderr_output) |
| 37 | + failed = True |
| 38 | + except Exception as e: |
| 39 | + logging.exception('Error processing {}:'.format(filename)) |
| 40 | + failed = True |
| 41 | + if results != {}: |
| 42 | + print(json.dumps(results, indent=4, sort_keys=True)) |
| 43 | + if failed: |
| 44 | + logging.warning('One or more ffprobe calls failed. See above for detail.') |
| 45 | + sys.exit(int(failed)) |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + args = parser.parse_args() |
| 50 | + main(args.filenames) |
0 commit comments