Fundamentals 4 min read

How to Convert Videos to GIFs with Python and MoviePy in Minutes

This guide shows how to convert videos to GIFs using Python's moviepy library, covering installation, basic conversion code, size reduction techniques like resizing and frame rate adjustment, and extracting video subclips, with practical code examples and visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Convert Videos to GIFs with Python and MoviePy in Minutes

Introduction

Many websites offer video‑to‑GIF conversion but charge fees or display ads. With a few lines of Python you can perform the conversion yourself.

Tutorial

1. Install moviepy

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

2. Basic conversion code

from moviepy.editor import *
clip = VideoFileClip("movie.mp4")  # source video
clip.write_gif("movie.gif")

3. Conversion result

GIF conversion result
GIF conversion result

The GIF is only a few seconds long but its size can reach 9 MB even after resolution scaling.

4. Reducing GIF size

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

from moviepy.editor import *
clip = VideoFileClip("movie.mp4").resize((488,225))
clip.write_gif("movie.gif", fps=15)  # 15 fps

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

Reduced GIF size
Reduced GIF size

5. Converting a specific video segment

Use the subclip parameter to select a time range.

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)

6. Specifying output resolution

The resize argument accepts either a (width, height) tuple in pixels or a scaling factor.

# 600×400 output
clip = VideoFileClip("movie.mp4").resize((600,400))

# 50 % scaling
clip = VideoFileClip("movie.mp4").resize(0.5)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Image ProcessingGIFvideo conversionmoviepy
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

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.