Generate FFmpeg Color Palette for High Quality GIFs

Converting videos to GIFs often results in blocky gradients and poor color quality because the GIF format is limited to a maximum of 256 colors. This article explains how to use FFmpeg to generate a custom color palette from your source video and apply it during conversion, allowing you to bypass the default color limitations and create highly optimized, visually stunning GIFs.

Why You Need a Color Palette

By default, FFmpeg uses a generic, web-safe palette of 256 colors to generate GIFs. Because this palette is not tailored to your specific video, it results in severe color banding and dithering artifacts.

To solve this, FFmpeg uses a two-phase approach: 1. palettegen: Analyzes the video to identify the 256 most dominant and visually important colors, saving them as a small PNG image. 2. paletteuse: Applies this customized PNG color palette to render the final GIF with maximum color accuracy.


You can generate the palette and convert the video in a single command using FFmpeg’s filter_complex. This method is faster and avoids creating a temporary palette image file on your hard drive.

Run the following command in your terminal:

ffmpeg -i input.mp4 -filter_complex "fps=15,scale=480:-1:flags=lanczos[x];[x]split[x1][x2];[x1]palettegen[p];[x2][p]paletteuse" output.gif

Command Breakdown:


Method 2: The Two-Step Command

If you want to manually inspect, edit, or reuse the palette image, you can split the process into two distinct steps.

Step 1: Generate the Palette Image

Run this command to create a palette.png file:

ffmpeg -i input.mp4 -vf "fps=15,scale=480:-1:flags=lanczos,palettegen" -y palette.png

Step 2: Convert to GIF Using the Palette

Run this command to merge the video and the palette 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.gif

Advanced Optimization Tweaks

You can further refine the GIF quality and file size by appending parameters to the palettegen and paletteuse filters:

1. Optimize for Color Transits (Dithering)

By default, paletteuse applies Floyd-Steinberg dithering to smooth out color gradients. If your GIF file size is too large, you can disable dithering or change the dithering mode: * Disable Dithering: Add dither=none to paletteuse (e.g., paletteuse=dither=none). This significantly reduces file size but may introduce color banding. * Bayer Dithering: Add dither=bayer:bayer_scale=5. This produces a cross-hatch pattern that balances size and quality.

2. Handle Video Cuts and Scene Changes

If your video contains drastic scene changes with completely different color schemes, a single palette might not look good across the entire GIF. You can force FFmpeg to generate a new palette for every single frame by adding stats_mode=single to palettegen and new=1 to paletteuse:

ffmpeg -i input.mp4 -filter_complex "fps=15,scale=480:-1:flags=lanczos[x];[x]split[x1][x2];[x1]palettegen=stats_mode=single[p];[x2][p]paletteuse=new=1" output.gif

Note: While this provides the absolute highest color accuracy, it will significantly increase the final GIF file size.