Originally published at ffmpeg-micro.com.
Converting an MP4 to a GIF with FFmpeg sounds simple until you actually try it. The default output is either a blurry mess or a 50MB file that nobody wants to load. The trick is knowing which FFmpeg flags control quality and file size independently.
The Basic Command (and Why It Looks Bad)
The most basic FFmpeg conversion looks like this:
ffmpeg -i input.mp4 output.gif
This technically works. But the result will look terrible.
GIFs are limited to 256 colors per frame. FFmpeg's default color selection doesn't pick the right 256 to keep, so you get banding, dithering artifacts, and washed-out colors. The file will also be massive because there's no compression optimization happening. A 10-second 1080p clip can easily produce a 100MB+ GIF.
High-Quality GIFs with Palette Generation
The fix is FFmpeg's palettegen and paletteuse filters. This two-pass approach first analyzes your video to build an optimal 256-color palette, then converts using that palette instead of guessing.
# Pass 1: Generate the optimal color palette
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,palettegen" palette.png
# Pass 2: Convert using that palette
ffmpeg -i input.mp4 -i palette.png -filter_complex "fps=10,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif
The difference is dramatic. Colors stay vibrant, gradients are smooth, and dithering artifacts mostly disappear.
What each flag does:
-
fps=10reduces the frame rate from the source (usually 24-30fps) to 10fps, cutting file size significantly -
scale=480:-1resizes to 480px wide while maintaining aspect ratio -
flags=lanczosuses Lanczos resampling for sharper downscaling -
palettegenanalyzes all frames and builds the best 256-color palette -
paletteuseapplies that palette during conversion
You can also do this in a single command using split to avoid the intermediate palette file:
ffmpeg -i input.mp4 -filter_complex "[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" output.gif
Same quality, one command, no temp files.
Controlling File Size
GIF files get big fast. Three things control the size: duration, resolution, and frame rate.
Trim to a specific clip with -ss (start time) and -t (duration):
ffmpeg -ss 00:00:05 -t 3 -i input.mp4 -filter_complex "[0:v] fps=10,scale=320:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" output.gif
This extracts 3 seconds starting at the 5-second mark.
Sizing reference:
| Resolution | FPS | 5s clip | 10s clip |
|---|---|---|---|
| 320px wide | 10 | ~1-3MB | ~2-6MB |
| 480px wide | 10 | ~3-6MB | ~5-12MB |
| 640px wide | 15 | ~8-15MB | ~15-30MB |
| 1080px wide | 24 | ~30-80MB | ~60-150MB |
For web use, 480px wide at 10fps is the sweet spot. Anything over 640px is usually overkill for a GIF.
Loop Control
GIFs loop forever by default. Control this with the -loop output option:
# Play 3 times then stop
ffmpeg -i input.mp4 -filter_complex "[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" -loop 3 output.gif
# Play once, no loop
ffmpeg -i input.mp4 -filter_complex "[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" -loop -1 output.gif
-loop 0 is infinite (the default). -loop -1 plays once. -loop N plays N+1 times total.
Common Pitfalls
The 256-color limit hits hardest on gradients and transitions. If your video has rapid color changes between scenes, you'll see color flickering. Adding dither=bayer:bayer_scale=5 to the paletteuse filter smooths this out: paletteuse=dither=bayer:bayer_scale=5.
GIF transparency is binary. No semi-transparent pixels. If you're converting video with an alpha channel, expect hard edges instead of smooth blending.
Audio gets silently dropped. GIFs don't support audio and FFmpeg won't warn you about it. If you need sound, stick with MP4 or WebM.
Skipping palette generation is the single biggest mistake. The quality difference between a naive conversion and the palette method is night and day. Always use palettegen + paletteuse unless file size truly doesn't matter.
Convert MP4 to GIF with an API
If you'd rather skip the FFmpeg installation and two-pass workflow, FFmpeg Micro converts videos to GIF with a single API call:
curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"inputs": [{"url": "https://example.com/video.mp4"}],
"outputFormat": "gif",
"options": [
{"option": "-vf", "argument": "fps=10,scale=480:-1:flags=lanczos"}
]
}'
You get back a job ID, poll for completion, and download the result. No infrastructure to manage, no FFmpeg to install, no server resources to worry about when processing volume spikes.
Trim the source video before conversion with input-level options:
curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"inputs": [{
"url": "https://example.com/video.mp4",
"options": [
{"option": "-ss", "argument": "5"},
{"option": "-t", "argument": "3"}
]
}],
"outputFormat": "gif",
"options": [
{"option": "-vf", "argument": "fps=10,scale=480:-1:flags=lanczos"}
]
}'
FFmpeg Micro handles the infrastructure and auto-scales with your workload. Pay per minute of video processed, with a free tier to start. Create your free account to get an API key.
For a deeper dive on FFmpeg encoding fundamentals, check out the Learn FFmpeg course.
Frequently Asked Questions
What's the best resolution for a web GIF?
480px wide at 10fps works for most use cases. Short clips stay under 5MB, which loads quickly on any connection. Drop to 320px if the GIF is for chat or messaging apps where bandwidth matters more than detail.
Why does my FFmpeg GIF look grainy or washed out?
You're probably running ffmpeg -i input.mp4 output.gif without palette generation. FFmpeg's default color quantization produces poor results with only 256 colors to work with. The two-pass palettegen and paletteuse method described above fixes this completely.
Can I convert a GIF back to MP4 with FFmpeg?
Yes. Run ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p output.mp4. The MP4 will be much smaller because H.264 compression is far more efficient than GIF's LZW compression. Platforms like Twitter and Imgur convert uploaded GIFs to MP4 behind the scenes for this exact reason.
Is animated WebP better than GIF?
For most cases, yes. Animated WebP supports 24-bit color (16.7 million colors vs GIF's 256), produces smaller files at equivalent quality, and handles transparency with proper alpha blending. GIF's advantage is universal compatibility: it works in email clients, legacy browsers, and systems where WebP isn't supported.
How do I make a GIF from just part of a video?
Use -ss for the start time and -t for duration before the input: ffmpeg -ss 00:00:30 -t 5 -i input.mp4 output.gif. This grabs 5 seconds starting at the 30-second mark. Combine it with the palette method for the best quality.
Top comments (0)