|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import unicode_literals, print_function |
| 3 | +import argparse |
| 4 | +import ffmpeg |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser(description='Extract video thumbnail') |
| 9 | +parser.add_argument('in_filename', help='Input filename') |
| 10 | +parser.add_argument('out_filename', help='Output filename') |
| 11 | +parser.add_argument( |
| 12 | + '--time', type=int, default=0.1, help='Time offset') |
| 13 | +parser.add_argument( |
| 14 | + '--width', type=int, default=120, |
| 15 | + help='Width of output thumbnail (height automatically determined by aspect ratio)') |
| 16 | + |
| 17 | + |
| 18 | +def generate_thumbnail(in_filename, out_filename, time, width): |
| 19 | + try: |
| 20 | + ( |
| 21 | + ffmpeg |
| 22 | + .input(in_filename, ss=time) |
| 23 | + .filter_('scale', width, -1) |
| 24 | + .output(out_filename, vframes=1, format='image2', vcodec='mjpeg') |
| 25 | + .run(capture_stdout=True, capture_stderr=True, overwrite_output=True) |
| 26 | + ) |
| 27 | + except ffmpeg.Error as e: |
| 28 | + print(e.stderr.decode(), file=sys.stderr) |
| 29 | + |
| 30 | + |
| 31 | +if __name__ == '__main__': |
| 32 | + args = parser.parse_args() |
| 33 | + generate_thumbnail(args.in_filename, args.out_filename, args.time, args.width) |
0 commit comments