Working with GIFs: Convert Video to GIF and Optimize for the Web

Tue Sep 10 2024

Learn how to create high-quality GIFs from videos, optimize file size with palettes, and control speed, loops, and dimensions.

GIFs remain popular for short clips, memes, and reactions on the web. FFmpeg gives you fine control over quality, file size, and playback. This guide shows you how to convert videos to GIF format and optimize them for sharing.

Why Use FFmpeg for GIFs?

Most video editors export bloated GIFs. FFmpeg lets you optimize the color palette, adjust dimensions, and control frame rates—resulting in smaller files that look better. The trick is a two-pass process that generates a custom palette for your specific video content.

Basic Video to GIF Conversion

# Simple conversion (may be large) ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" output.gif # Better quality with custom palette (two-pass) ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png -lavfi "fps=10,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

The two-pass approach generates a palette that matches your content, avoiding the generic 256-color web palette and giving you better visual quality.

Control Speed and Duration

# Trim to 3 seconds starting at 5s ffmpeg -ss 00:00:05 -t 3 -i input.mp4 -vf "fps=15,scale=360:-1:flags=lanczos,palettegen" palette.png ffmpeg -ss 00:00:05 -t 3 -i input.mp4 -i palette.png \ -lavfi "fps=15,scale=360:-1:flags=lanczos[x];[x][1:v]paletteuse" clip.gif # Speed up 2x (double framerate perception, halve frame count) ffmpeg -i input.mp4 -vf "setpts=0.5*PTS,fps=20,scale=400:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png \ -lavfi "setpts=0.5*PTS,fps=20,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" fast.gif # Slow down to 0.5x ffmpeg -i input.mp4 -vf "setpts=2*PTS,fps=10,scale=400:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png \ -lavfi "setpts=2*PTS,fps=10,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" slow.gif

Size Optimization Tips

# Lower FPS for smaller files (8-12 fps is often enough) ffmpeg -i input.mp4 -vf "fps=8,scale=320:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png \ -lavfi "fps=8,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" small.gif # Aggressive dithering for even smaller size (trade quality for size) ffmpeg -i input.mp4 -vf "fps=10,scale=360:-1:flags=lanczos,palettegen=stats_mode=diff" palette.png ffmpeg -i input.mp4 -i palette.png \ -lavfi "fps=10,scale=360:-1:flags=lanczos[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=3" optimized.gif

Advanced: Looping and Cropping

# Create a GIF that loops only 3 times (default is infinite) ffmpeg -i input.mp4 -vf "fps=10,scale=400:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png -loop 3 \ -lavfi "fps=10,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" limited.gif # Crop and convert (center 500x500 square) ffmpeg -i input.mp4 -vf "crop=500:500,fps=12,scale=300:-1:flags=lanczos,palettegen" palette.png ffmpeg -i input.mp4 -i palette.png \ -lavfi "crop=500:500,fps=12,scale=300:-1:flags=lanczos[x];[x][1:v]paletteuse" cropped.gif

Pitfalls

  • Skipping the palette step results in poor color quality and often larger files.
  • High frame rates (30+ fps) bloat GIF files quickly; 10-15 fps is usually plenty.
  • Lanczos scaling gives better quality than the default bicubic—always worth using.
  • Setting -loop 0 makes infinite loops (default), while -loop N loops N times.

When Not to Use GIF

For videos longer than 5-10 seconds or when you need smooth motion, consider modern formats:

# WebM is better for web (VP9 codec, smaller size, better quality) ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 40 -an output.webm # MP4 with aggressive compression ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast -an -movflags +faststart output.mp4

Most browsers auto-play muted video now, so MP4 or WebM often delivers better results at smaller sizes than GIF.

With these techniques, you can create web-ready GIFs that balance quality and file size for sharing on social media, documentation, or your website.