Skip to content

Instantly share code, notes, and snippets.

@glcoder
Created February 13, 2014 11:22
Show Gist options
  • Save glcoder/8973469 to your computer and use it in GitHub Desktop.
Save glcoder/8973469 to your computer and use it in GitHub Desktop.
Easy way to make an animated GIF from video file.
#!/usr/bin/env python
import os
import shutil
import sys
import getopt
# animated GIF maker
# typical usage: ./agif.py -i input.mp4 -w 640 -f 8 -t 00:00:08.50 -l 00:00:07.50 -d 10 -o output.gif
USAGE_HELP = """agif.py usage:
-h, --help - prints this usgae help
-i <name>, --input=<name> - input source video
-w <width>, --width=<width> - width of the resulting GIF
-f <fps>, --fps=<fps> - how much frames from 1 second will be captured
-t <sec>, --time=<sec> - capture offset
-l <sec>, --length=<sec> - capture length
-d <ms>, --delay=<ms> - delay between GIF frames
-o <name>, --output=<name> - resulting GIF name
"""
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:w:f:t:l:d:o:",
["help", "input", "width", "fps", "time", "length", "delay", "output"])
except getopt.GetoptError as e:
print e
print USAGE_HELP
exit(1)
input, output, delay = None, "output.gif", 20
command = "ffmpeg"
for opt, arg in opts:
if opt in ("-h", "--help"):
print USAGE_HELP
exit(0)
elif opt in ("-i", "--input"):
input = arg
command = command + " -i \"" + arg + "\""
elif opt in ("-w", "--width"):
if int(arg) > 0:
command = command + " -vf scale=" + arg + ":-1"
elif opt in ("-f", "--fps"):
if int(arg) > 0:
command = command + " -r " + arg
elif opt in ("-t", "--time"):
command = command + " -ss " + arg
elif opt in ("-l", "--length"):
command = command + " -t " + arg
elif opt in ("-d", "--delay"):
if int(arg) > 0:
delay = int(arg)
elif opt in ("-o", "--output"):
output = arg
if not input:
print USAGE_HELP
exit(1)
shutil.rmtree("agif-frames", True)
os.mkdir("agif-frames")
command = command + " agif-frames/frame-%08d.png"
os.system(command)
command = "convert -delay " + str(delay) + " -loop 0 agif-frames/frame-*.png -layers Optimize " + output
os.system(command)
shutil.rmtree("agif-frames", True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment