Skip to content

Instantly share code, notes, and snippets.

@motoishmz
Created October 21, 2013 23:50
Show Gist options
  • Save motoishmz/7092877 to your computer and use it in GitHub Desktop.
Save motoishmz/7092877 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
DEPENDENCIES:
$ brew install ffmpeg
$ brew install imagemagick
$ python ./mov2gif.py input.mov output.gif 15
'''
import sys
import os
import tempfile
import shutil
import subprocess
if len(sys.argv) <= 2:
print 'usage: ', sys.argv[0], '[INPUT_MOV_FILENAME]', '[OUTPUT_GIF_FILENAME]', '[FPS]'
sys.exit(-1)
assert(os.path.exists(sys.argv[1]))
INPUT_MOV_FILENAME = sys.argv[1]
OUTPUT_GIF_FILENAME = sys.argv[2]
try:
FPS = int(sys.argv[3])
except Exception, e:
FPS = 10
temp = tempfile.mkdtemp()
cmd = 'ffmpeg -loglevel quiet -i %(INPUT_MOV_FILENAME)s -r %(FPS)i %(TEMP)s' % {
"INPUT_MOV_FILENAME": INPUT_MOV_FILENAME,
"FPS": FPS,
"TEMP": os.path.join(temp, '%5d.png')
}
subprocess.check_call(cmd, shell=True)
cmd = 'convert -delay 1x%(FPS)i %(TEMP)s %(OUTPUT_GIF_FILENAME)s' % {
"FPS": FPS,
"TEMP": os.path.join(temp, '*.png'),
'OUTPUT_GIF_FILENAME': OUTPUT_GIF_FILENAME
}
subprocess.check_call(cmd, shell=True)
shutil.rmtree(temp)
cmd = 'open -R %(OUTPUT_GIF_FILENAME)s' % {
"OUTPUT_GIF_FILENAME": OUTPUT_GIF_FILENAME
}
subprocess.check_call(cmd, shell=True)
print 'done'
@danvk
Copy link

danvk commented Sep 22, 2014

Here's a version which supports file name with spaces:
https://gist.github.com/danvk/ea7a9e2cd8cde9c92a5e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment