How to Create a GIF from a Video in Python Using MoviePy ?

Introduction

Creating a GIF from a video clip is a useful and fun way to extract short, looping animations for presentations, social media, or documentation. Python’s moviepy library makes this process easy with just a few lines of code.

What is MoviePy?

MoviePy is a Python module for video editing. It can read and write video/audio files in various formats and perform tasks like:

  • Cutting video clips
  • Concatenating and editing clips
  • Adding effects and text
  • Exporting clips as videos or GIFs

It uses FFMPEG under the hood for handling video encoding and decoding.

Installing and Verifying MoviePy

Before you can use MoviePy, install it with:

1
pip install moviepy

Check the Installed Version

To confirm the installation and check the version you're using:

1
pip show moviepy

Example Output

1
2
3
4
5
Name: moviepy
Version: 2.1.1
Summary: Video editing with Python
Author: Zulko 2024
License: MIT License

Why Version Matters

The version of MoviePy you're using is important because function names have changed in recent versions:

Functionality Old Version (≤1.0.x) New Version (≥2.x)
Resize video resize() resized()
Trim video segment subclip() subclipped()

Example: Creating a GIF

Here’s a working Python code snippet that takes a video file, trims it, resizes it, and saves it as a GIF:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from moviepy import *

# Load the video
clip = VideoFileClip("input.mov")

# Trim the video from 2s to 3s and resize to 40% of original
clip = clip.resized(0.4).subclipped(2.0,3.0)

# Export the clip as a GIF
clip.write_gif("output.gif", fps=10)

Output

1
MoviePy - Building file output.gif with imageio.

Now, output.gif should be saved in your current working directory.

Explanation of Each Step:

  1. VideoFileClip("filename")

Loads the video file into a MoviePy VideoFileClip object. You can use formats like .mp4, .mov, etc.

  1. subclipped(start_time, end_time)

Extracts a portion of the video. Time is given in seconds. This is useful for trimming only the part you want in the GIF.

Example: clip.subclipped(2.0, 3.0) gets the one-second segment from 2s to 3s.

  1. clip.resized(0.4)

Reduces the video size to 40% of its original resolution. This is important for GIFs to reduce file size.

  1. write_gif("output.gif", fps=10)

Writes the resulting video segment as a .gif file. Lower FPS (e.g., 10) gives smaller files; higher FPS means smoother playback but larger size.

Note on Performance: Watch Out for Long Processing Times

If your script seems to hang or take a very long time to generate the GIF, don't worry — you're not stuck.

This usually happens when:

  • The original video has a high resolution (e.g., 1080p or 4K)
  • The GIF covers multiple seconds of video
  • You haven’t resized the video before exporting

Solution

To speed things up, reduce the size of the clip using .resized(scale_factor) with a smaller number. For example:

1
clip = clip.resized(0.4)  # 40% of the original size

Try values like 0.4, 0.3, or even 0.2 if the video is large.

If Nothing Seems to Happen...

If the terminal/output appears frozen or no GIF is being generated:

  • It's most likely processing, especially for large clips.
  • Hit Ctrl+C to stop it if needed.
  • Then, try a shorter clip or a smaller resize factor.

This can significantly reduce both memory usage and processing time.

References

Links Site
How to convert video to gif with Python blog.techchee.com
MoviePy zulko.github.io