CGJU
CGJU Tool Net
June 18, 2026 Β· 5 min read

GIF Format Features

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.

Core Features of GIF Format

  • Animation Support: Can contain multiple frames of images to create animation effects
  • Lossless Compression: Uses LZW compression algorithm with no quality loss
  • Transparent Background: Supports single-frame transparent color
  • Good Compatibility: Supported by almost all browsers and devices
  • 256 Color Limit: Each frame can have a maximum of 256 colors
  • Larger File Size: GIF files are typically larger compared to modern video formats

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.

Two-Pass Algorithm Principle

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.

First Pass: Generate Palette (palettegen)

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.

Second Pass: Convert with Palette (paletteuse)

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

Advantages of Two-Pass Algorithm

  • Better Color Reproduction: Customized palette for video content ensures more accurate colors
  • Less Color Banding: Effectively reduces color banding and graininess in gradient areas
  • Higher Quality: Significant quality improvement compared to direct conversion
  • Controllable Quality: Palette quality can be adjusted through parameters

Palette Quality Optimization

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.

Adjust Palette Statistics Mode

Use the stats_mode parameter to select the palette statistics method:

  • full: Statistics all pixels (default), best quality but slowest speed
  • diff: Only statistics inter-frame difference pixels, suitable for videos with less motion
  • single: Only statistics each frame's pixels individually
ffmpeg -i input.mp4 -vf "palettegen=stats_mode=diff" palette.png

Preserve Transparency Channel

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

Use Highest Quality Mode

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:

  • sierra2_4a: Default value, good balance between speed and quality
  • floyd_steinberg: Classic Floyd-Steinberg dithering, better quality but slightly slower
  • bayer: Bayer ordered dithering, smaller file size
  • none: No dithering, suitable for images with distinct color blocks

Frame Rate Control

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.

Use fps Filter to Adjust Frame Rate

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:

  • 10-15fps: Suitable for most scenarios, balance between smoothness and file size
  • 8-10fps: Text demonstrations, tutorial animations, smaller file size
  • 15-24fps: High-quality animation, pursuing smooth effects
  • 5-8fps: Minimalist animation, pursuing smallest file

Resizing

GIF dimensions have a huge impact on file size. Properly reducing GIF dimensions is one of the most effective ways to reduce file volume.

Use scale Filter to Adjust Size

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

Common GIF Size Reference

  • Memes: 200-400 pixels width
  • Tutorial Demonstrations: 600-800 pixels width
  • Web Display: 480-640 pixels width
  • Social Media: 500-700 pixels width

File Size Reduction

In addition to frame rate and dimensions, there are many other techniques to further reduce GIF file size.

Crop Video Segment

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.

Reduce Color Count

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

Comprehensive Optimization Example

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

Practical Commands

One-Click Conversion Script

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

Batch Conversion

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

Summary

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.

Related Recommendations

More great articles and practical tools

Want to Convert GIFs Quickly?

Use our free MP4 to GIF online tool, no software installation required, generate high-quality animated GIFs directly in your browser

Try Now ➑