C.W.K.
Stream
Lesson 04 of 07 · published

GIF Creation — High-Quality, Small File

~10 min · gif, palette, share

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Why GIFs are tricky

GIF supports only 256 colors per palette. Default GIF encoding picks colors naively and produces ugly banding. The palettegen + paletteuse two-step generates an optimal palette per clip and applies it — the difference is dramatic.

Code

High-quality GIF (the right way)·bash
# Step 1 — generate optimal palette
ffmpeg -i in.mp4 -vf "fps=15,scale=480:-1:flags=lanczos,palettegen=stats_mode=diff" \
  -y palette.png

# Step 2 — encode using that palette
ffmpeg -i in.mp4 -i palette.png \
  -lavfi "fps=15,scale=480:-1:flags=lanczos[v];[v][1:v]paletteuse=dither=bayer:bayer_scale=5" \
  -y out.gif

# Or do both in one command
ffmpeg -i in.mp4 \
  -vf "fps=15,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse=dither=bayer:bayer_scale=5" \
  -y one_pass.gif
Trim + GIF in one shot·bash
# 5-second clip starting at 00:01:30
ffmpeg -ss 00:01:30 -t 5 -i in.mp4 \
  -vf "fps=15,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
  -y meme.gif

External links

Exercise

Take a 5-second clip. Make three GIFs: (a) default ffmpeg -i in.mp4 out.gif, (b) the palettegen+paletteuse two-pass, (c) the same with bayer_scale=3. Compare file sizes and quality. Then convert the same clip to a small WebP loop and to an MP4 — when would you actually ship the GIF instead of one of those?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.