How to Create High-Quality GIFs with FFmpeg
Standard GIF generation in FFmpeg often results in grainy,
low-quality images because the GIF format is limited to a maximum of 256
colors. This article explains how to overcome this limitation by using
FFmpeg’s palettegen and paletteuse filters. By
utilizing this two-step process, you can generate a custom color palette
tailored specifically to your input video, resulting in crisp,
professional, and high-quality GIFs.
Understanding the Two-Step Process
The standard FFmpeg conversion uses a generic, static color palette, which leads to poor color representation and color banding. To solve this, FFmpeg offers two specific filters:
palettegen: Analyzes the video frames and generates a highly optimized custom 256-color palette (saved as a PNG image).paletteuse: Applies this custom palette to the video during the GIF conversion, rendering the final output with accurate colors and smooth gradients.
Method 1: The One-Command Approach (Recommended)
You do not need to output a temporary palette file to your hard drive. You can link both filters together in a single FFmpeg command using a filtergraph. This is the fastest and most efficient way to generate high-quality GIFs.
Run the following command in your terminal:
ffmpeg -i input.mp4 -filter_complex "fps=15,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gifCommand Breakdown:
-i input.mp4: Specifies the source video.-filter_complex: Calls the complex filtergraph to chain multiple operations.fps=15: Sets the frame rate of the GIF to 15 frames per second (reducing file size while maintaining smooth motion).scale=480:-1: Resizes the width to 480 pixels and automatically scales the height to maintain the original aspect ratio.flags=lanczos: Uses the high-quality Lanczos scaling algorithm.split[s0][s1]: Duplicates the video stream into two identical outputs labeleds0ands1.[s0]palettegen[p]: Takes the first stream (s0) and generates a custom palette namedp.[s1][p]paletteuse: Takes the second stream (s1) and the generated palette (p) to render the final GIF.
Method 2: The Two-Step Approach
If you prefer to inspect, edit, or reuse the generated palette file, you can run the process in two separate steps.
Step 1: Generate the Palette
Run this command to analyze the video and output a
palette.png image:
ffmpeg -i input.mp4 -vf "fps=15,scale=480:-1:flags=lanczos,palettegen" -y palette.pngStep 2: Apply the Palette to the GIF
Run this command to merge the original video and the palette file into the final GIF:
ffmpeg -i input.mp4 -i palette.png -filter_complex "fps=15,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gifAdvanced Palette Tweaks
You can further customize how colors are processed by adding options to the filters.
Adjusting Dithering
Dithering helps smooth out color gradients, but it can increase the
GIF’s file size. The paletteuse filter allows you to change
the dithering mode.
- Floyd-Steinberg (Default): Best for complex scenes with gradients.
- Bayer: Good for retro pixel art and creates smaller file sizes.
- None: Disables dithering entirely, resulting in very sharp color blocks and the smallest file sizes.
To change the dither mode to Bayer, modify the filter like this:
[s1][p]paletteuse=dither=bayer:bayer_scale=3To disable dithering completely:
[s1][p]paletteuse=dither=none