Skip to content

Instantly share code, notes, and snippets.

@scari
Created January 8, 2014 12:12
Show Gist options
  • Save scari/8315943 to your computer and use it in GitHub Desktop.
Save scari/8315943 to your computer and use it in GitHub Desktop.
get evenly parted image from a video
#!/usr/bin/env python
import sys, os, re
from subprocess import *
# get_evenly.py videofile #of_thumbnails - 1
try:
fvideo = sys.argv[1]
frames = float(sys.argv[2])
except:
sys.stderr.write("Failed to parse parameters.\n")
sys.exit(1)
output = Popen(["avconv", "-i", fvideo], stderr=PIPE).communicate()
devnull = open('/dev/null', 'w')
regex = re.compile("Duration: (.*?)\.")
duration = regex.search(output[1]).groups()[0]
seconds = reduce(lambda x, y: x*60+y, map(int, duration.split(":")))
rate = frames/seconds
print "Duration = %s (%i seconds)" % (duration, seconds)
print "Capturing one frame every %.1f seconds" % (1 / rate)
call(['avconv', '-i', fvideo, '-f', 'image2', '-r', str(rate), '-vsync', 'vfr', '%03d.jpg'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment