Skip to content

Instantly share code, notes, and snippets.

@Zulko
Forked from luser/makegifs.py
Last active December 27, 2022 18:16
Show Gist options
  • Save Zulko/c2c5c207bc64dd84085f to your computer and use it in GitHub Desktop.
Save Zulko/c2c5c207bc64dd84085f to your computer and use it in GitHub Desktop.
Just indications on what you could do, @luser
#!/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)
@gphg
Copy link

gphg commented Jul 9, 2022

I tried to change

selected_scenes.write_gifs(clip.resize(width=270), name)

Into

selected_scenes.write_videofile(clip, name)

An error happen:

Traceback (most recent call last):
  File "C:\Users\pnesi\Videos\Output\makegifs.py", line 35, in <module>
    selected_scenes.write_videofile(clip, name) # save gif as it, without resize
AttributeError: 'FramesMatches' object has no attribute 'write_videofile'

Expectation: write the scenes as video (muted is ok) instead gif.

What should I do?

Dec, 2022 update:
I managed to accomplish my objection. The customization can be found on fork by me.

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