Last active
April 3, 2016 14:11
-
-
Save dogrdon/c551d74a252b4b615170 to your computer and use it in GitHub Desktop.
Scrappy mockup of a script to generate a thumbnail preview for a video found online...no need to download. Run me like: `python thumbnailgen.py -i http://example.com/video.f4v`
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 python | |
from subprocess import PIPE, Popen | |
import argparse | |
import os | |
''' | |
Give me a url to a video file and I will return a thumbnail preview | |
Run me like: `python thumbnailgen.py -i https://catalog.archives.gov/OpaAPI/media/13729/content/arcmedia/mopix/107/107-1130.wmv?download=true` | |
***NOTES*** | |
- Requires `ffmpeg` and `imagemagick` to be installed on local | |
- `ffmpeg` should be compiled `--with-openssl` so that it can access videos over https (https://trac.ffmpeg.org/wiki/CompilationGuide) | |
- if script seems to run silently without creating output, it's most likely that ffmpeg or imagemagick are not congfigured properly in someway - run commands in makethumbs() and makenpreview() directly in command line to see if any errors pop up` | |
''' | |
parser = argparse.ArgumentParser(description='Command line tool to make a thumbnail montage preview image for a digital video with a url') | |
parser.add_argument('-i', '--inputfile', help='Url to video you want to turn into thumbnail preview. Is required', required=True) | |
args = vars(parser.parse_args()) | |
__input__ = args['inputfile'] | |
__path__ = './tmp_v' | |
FFMPEG_BIN = 'ffmpeg' | |
MONTAGE_BIN = 'montage' | |
def process(command): | |
p = Popen(command, stdout=PIPE, stderr=PIPE, bufsize=10**8) | |
output = p.stdout.read().decode("utf-8") | |
error = p.stderr.read().decode("utf-8") | |
return (output, error) | |
def makethumbs(i): | |
command = [ | |
FFMPEG_BIN, | |
'-v', 'error', | |
'-threads', '1', | |
'-i', i, | |
'-vf', 'fps=1/15', | |
'./tmp_v/img_%03d.jpg' | |
] | |
return process(command) | |
def makepreview(d): | |
imgs = os.path.join(d, "img_*.jpg") | |
command = [ | |
MONTAGE_BIN, | |
'-geometry', '+4+4', | |
imgs, './output.png' | |
] | |
return process(command) | |
def main(i, p): | |
makethumbs(i) | |
makepreview(p) | |
if __name__ == '__main__': | |
main(__input__, __path__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment