Video Metadata Analysis Guide: Understanding Video Information with FFprobe
Deep dive into video metadata, master the usage of FFprobe tool, learn to analyze container, video stream, and audio stream information.
Read More βMaster the core techniques of high-quality GIF generation and say goodbye to blurry images and oversized files
GIF (Graphics Interchange Format) is an image format born in 1987. Despite its age, it remains widely used on the internet, especially in memes, animated demonstrations, and other scenarios. The GIF format has unique advantages but also significant technical limitations.
Due to GIF's 256-color limitation, direct conversion from MP4 video often results in color distortion and quality degradation. To achieve high-quality animated GIFs, special optimization algorithms are required.
FFmpeg provides an efficient GIF generation solution β the Two-pass Palette Method, which significantly improves GIF quality by first generating a palette and then using it for conversion.
The first pass uses the palettegen filter to analyze the color distribution of the entire video and generate an optimal 256-color palette. This palette is customized for specific video content and can best preserve the original video's color information.
ffmpeg -i input.mp4 -vf "palettegen" palette.png
This command outputs a palette.png file, which is the 256-color palette image generated specifically for the video.
The second pass uses the paletteuse filter, combined with the palette generated in the first pass, to convert the video to GIF format. Due to the use of a customized palette, the color reproduction of the converted GIF is much higher than direct conversion.
ffmpeg -i input.mp4 -i palette.png -lavfi "paletteuse" output.gif
The palettegen filter provides multiple parameters to adjust palette generation quality. Choosing appropriate parameters based on different video content can achieve the best balance between quality and file size.
Use the stats_mode parameter to select the palette statistics method:
ffmpeg -i input.mp4 -vf "palettegen=stats_mode=diff" palette.png
If the source video has a transparency channel (such as WebM format), transparency information can be preserved:
ffmpeg -i input.webm -vf "palettegen=reserve_transparent=1" palette.png
The paletteuse filter supports multiple dithering algorithms to further improve quality:
ffmpeg -i input.mp4 -i palette.png -lavfi "paletteuse=dither=sierra2_4a" output.gif
Common dithering algorithms include:
Frame rate is a key factor affecting GIF file size and smoothness. Videos typically have 24fps or 30fps, while GIFs generally don't need such high frame rates. Reducing the frame rate can significantly decrease file size.
The fps filter can precisely control the GIF frame rate:
ffmpeg -i input.mp4 -vf "fps=15, palettegen" palette.png
ffmpeg -i input.mp4 -i palette.png -lavfi "fps=15, paletteuse" output.gif
Common frame rate recommendations:
GIF dimensions have a huge impact on file size. Properly reducing GIF dimensions is one of the most effective ways to reduce file volume.
ffmpeg -i input.mp4 -vf "scale=480:-1, palettegen" palette.png
ffmpeg -i input.mp4 -i palette.png -lavfi "scale=480:-1, paletteuse" output.gif
In the scale filter, -1 means automatically calculating while maintaining the original aspect ratio. You can also specify both width and height:
ffmpeg -i input.mp4 -vf "scale=320:240, palettegen" palette.png
In addition to frame rate and dimensions, there are many other techniques to further reduce GIF file size.
Convert only the needed segment instead of the entire video:
ffmpeg -ss 00:00:05 -t 00:00:10 -i input.mp4 -vf "palettegen" palette.png
ffmpeg -ss 00:00:05 -t 00:00:10 -i input.mp4 -i palette.png -lavfi "paletteuse" output.gif
-ss specifies the start time, -t specifies the duration.
If color requirements are not high, you can reduce the number of colors in the palette:
ffmpeg -i input.mp4 -vf "palettegen=max_colors=128" palette.png
Below is a complete example that comprehensively applies multiple optimization methods:
ffmpeg -ss 2 -t 8 -i input.mp4 -vf "fps=12, scale=480:-1:flags=lanczos, palettegen=stats_mode=diff" palette.png
ffmpeg -ss 2 -t 8 -i input.mp4 -i palette.png -lavfi "fps=12, scale=480:-1:flags=lanczos, paletteuse=dither=bayer" output.gif
For convenience, the two-pass commands can be written as a script. Below is a complete high-quality GIF conversion command:
ffmpeg -i input.mp4 -vf "fps=15, scale=iw*0.5:-1:flags=lanczos, palettegen=stats_mode=full" palette.png -y
ffmpeg -i input.mp4 -i palette.png -lavfi "fps=15, scale=iw*0.5:-1:flags=lanczos, paletteuse=dither=sierra2_4a" output.gif -y
On Linux or macOS, you can use a simple Shell script for batch conversion:
for file in *.mp4; do
ffmpeg -i "$file" -vf "fps=12, scale=480:-1, palettegen" palette.png -y
ffmpeg -i "$file" -i palette.png -lavfi "fps=12, scale=480:-1, paletteuse" "${file%.mp4}.gif" -y
done
rm palette.png
Although MP4 to GIF conversion seems simple, achieving high-quality, small-sized animated GIFs requires mastering the two-pass algorithm and various optimization techniques. By properly setting parameters such as palette, frame rate, and dimensions, you can find the best balance between quality and file size.
If you don't want to memorize complex FFmpeg commands, you can use our MP4 to GIF Online Tool, simply upload your video in the browser and generate high-quality animated GIFs through simple parameter configuration.
More great articles and practical tools
Deep dive into video metadata, master the usage of FFprobe tool, learn to analyze container, video stream, and audio stream information.
Read More βCompare the advantages and disadvantages of four WebM to MP4 methods, master FFmpeg efficient conversion techniques and batch processing solutions.
Read More βCollection of the most practical FFmpeg commands, covering transcoding, editing, merging, audio extraction and other common operations.
Read More βUse our free MP4 to GIF online tool, no software installation required, generate high-quality animated GIFs directly in your browser
Try Now β‘