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.
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/simple2. Basic conversion code
from moviepy.editor import *
clip = VideoFileClip("movie.mp4") # source video
clip.write_gif("movie.gif")3. 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 fpsSetting 15 fps reduces the file to about 2 MB, roughly a four‑fold decrease, with little visual difference.
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)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
