Fundamentals 4 min read

How to Convert Video to GIF Using Python and MoviePy

This tutorial explains how to install the MoviePy library, write Python code to load a video file, and generate a GIF while controlling size through resolution scaling, frame rate reduction, sub‑clipping, and output dimensions, all with clear code examples and visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Convert Video to GIF Using Python and MoviePy

Many online services offer video‑to‑GIF conversion but often require payment or display ads; with a few lines of Python you can achieve the same result yourself.

1. Install the required library

pip install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simple

2. Basic conversion code

<code>from moviepy.editor import *
clip = VideoFileClip("movie.mp4")  # path to the source video
clip.write_gif("movie.gif")
</code>

The resulting GIF may be only a few seconds long but can be as large as 9 MB even after resolution scaling.

3. Reducing GIF size

Besides resizing, you can lower the frame rate (fps) to shrink the file:

<code>from moviepy.editor import *
clip = VideoFileClip("movie.mp4").resize((488,225))
clip.write_gif("movie.gif", fps=15)  # 15 frames per second
</code>

Setting the fps to 15 reduces the file to about 2 MB, roughly a four‑fold decrease, with little visual difference.

4. Converting a specific video segment

Use the subclip parameter to select a time range (e.g., 1–2 seconds) before conversion:

<code>from moviepy.editor import *
clip = VideoFileClip("movie.mp4").subclip(t_start=1, t_end=2).resize((488,225))
clip.write_gif("movie.gif", fps=15)
</code>

5. Specifying output resolution

The resize argument can define the final image size directly, either by pixel dimensions or scaling factor:

<code># Set output size to 600×400 pixels
clip = VideoFileClip("movie.mp4").resize((600,400))

# Scale the original video to 50 % of its size
clip = VideoFileClip("movie.mp4").resize(0.5)
</code>

These techniques allow you to control GIF quality and file size efficiently.

Disclaimer: This article is compiled from online sources; copyright belongs to the original author. Please contact us for removal or licensing requests.

video processingGIFMoviePy
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.