Skip to content

Instantly share code, notes, and snippets.

@onlyyoujack
Forked from Zulko/makegifs.py
Last active September 7, 2017 22:53
Show Gist options
  • Save onlyyoujack/8915ba6cde3d86e0a264 to your computer and use it in GitHub Desktop.
Save onlyyoujack/8915ba6cde3d86e0a264 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Taken from http://zulko.github.io/blog/2015/02/01/extracting-perfectly-looping-gifs-from-videos-with-python-and-moviepy/
import os
import sys
import moviepy.editor as mp
from moviepy.video.tools.cuts import FramesMatches
if len(sys.argv) != 2:
print "makegifs.py <video file>"
sys.exit(1)
clip = mp.VideoFileClip(sys.argv[1])
name = os.path.splitext(os.path.basename(sys.argv[1]))[0]
# Comment after first use
scenes = FramesMatches.from_clip(clip.resize(width=120), 5, 3) # I increase 2 to 3 to get more gifs
scenes.save("matches")
# load the scenes from a previous time to avoid recomputing them
scenes.load("matches")
"""
That's the hard part that you must tweak. Not that the whole method is
very sloppy so maybe you can just redefine/customize this function if
you have the time
The last number "0.5" says that you want at least 0.5 second between the start of each clip.
If you really want to avoid having multiple times the same scene with just a few different frames,
increase this to e.g. 1.5
"""
selected_scenes = scenes.select_scenes(2, 0.5, 4, 0.5)
os.mkdir(name) # <- well done, otherwise you get a Seg Fault (this is going to be fixed)
selected_scenes.write_gifs(clip.resize(width=270), name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment