Convert Video to GIF with Python and MoviePy
This tutorial demonstrates how to use Python's MoviePy library to convert videos into GIFs, covering installation, basic conversion, size reduction via resizing and frame‑rate adjustment, extracting sub‑clips, and specifying output resolution with clear code examples.
This guide shows how to transform a video file into an animated GIF using the Python library moviepy . It begins by installing the required package from a Chinese mirror:
pip install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simpleAfter installation, a simple conversion can be performed with the following script:
from moviepy.editor import *
clip = VideoFileClip("movie.mp4")
clip.write_gif("movie.gif")For large GIFs, the file size can be reduced by resizing the video and lowering the frame rate. Example code:
from moviepy.editor import *
clip = VideoFileClip("movie.mp4").resize((488, 225))
clip.write_gif("movie.gif", fps=15) # set to 15 frames per secondFurther size reduction is achieved by extracting only a portion of the source video using the subclip method and then applying the same resizing and frame‑rate settings:
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)The resize parameter accepts either a pixel tuple or a scaling factor. Examples:
clip = VideoFileClip("movie.mp4").resize((600, 400)) # set output size to 600×400
clip = VideoFileClip("movie.mp4").resize(0.5) # scale video to 50%These techniques allow you to generate GIFs with acceptable visual quality while keeping file sizes manageable.
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.