Requirements
Required for creating GIFs:
- -
ffmpeg — video to GIF conversion
Optional:
- -
gifsicle — post-optimization (reduces size 30-50%) - INLINECODE2 — for Giphy search API
- INLINECODE3 — for Tenor search API
Where to Find GIFs
| Site | Best for | API |
|---|
| Giphy | General, trending | Yes (key required) |
| Tenor |
Messaging apps | Yes (key required) |
|
Imgur | Viral/community | Yes |
|
Reddit r/gifs | Niche, unique | No |
Creating GIFs with FFmpeg
Always use palettegen (without it, colors look washed out):
ffmpeg -ss 0 -t 5 -i input.mp4 \
-filter_complex "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" \
output.gif
| Setting | Value | Why |
|---|
| fps | 8-12 | Higher = much larger file |
| scale |
320-480 | 1080p GIFs are massive |
| lanczos | Always | Best scaling quality |
Post-Optimization
If gifsicle is available:
CODEBLOCK1
Reduces size 30-50% with minimal quality loss.
Video Alternative
For web, use video instead of large GIFs (80-90% smaller):
CODEBLOCK2
Accessibility
- - WCAG 2.2.2: Loops >5s need pause control
- prefers-reduced-motion: Show static image instead
- Alt text: Describe the action ("Cat jumping off table")
- Three flashes: Nothing >3 flashes/second (seizure risk)
Common Mistakes
- - No
palettegen in FFmpeg — colors look terrible - FPS >15 — file size explodes for no visual benefit
- No lazy loading on web — blocks page load
- Using huge GIF where video would work — 10x larger
API Quick Reference
Giphy search:
CODEBLOCK3
Tenor search:
CODEBLOCK4
要求
创建GIF所需:
可选:
- - gifsicle — 后期优化(可减小30-50%体积)
- GIPHYAPIKEY — 用于Giphy搜索API
- TENORAPIKEY — 用于Tenor搜索API
哪里找GIF
| 网站 | 最适合 | API |
|---|
| Giphy | 通用、热门 | 是(需要密钥) |
| Tenor |
即时通讯应用 | 是(需要密钥) |
|
Imgur | 病毒式传播/社区 | 是 |
|
Reddit r/gifs | 小众、独特 | 否 |
使用FFmpeg创建GIF
务必使用palettegen(否则颜色会显得暗淡):
bash
ffmpeg -ss 0 -t 5 -i input.mp4 \
-filter_complex fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse \
output.gif
| 设置 | 值 | 原因 |
|---|
| fps | 8-12 | 更高=文件大得多 |
| scale |
320-480 | 1080p的GIF体积巨大 |
| lanczos | 始终 | 最佳缩放质量 |
后期优化
如果安装了gifsicle:
bash
gifsicle -O3 --lossy=80 --colors 128 input.gif -o output.gif
可在质量损失极小的情况下减小30-50%体积。
视频替代方案
对于网页,使用视频替代大型GIF(体积小80-90%):
html
无障碍访问
- - WCAG 2.2.2: 循环>5秒需要暂停控制
- prefers-reduced-motion: 显示静态图片替代
- 替代文本: 描述动作(猫从桌子上跳下来)
- 三次闪烁: 不超过3次闪烁/秒(癫痫风险)
常见错误
- - FFmpeg中未使用palettegen — 颜色效果极差
- FPS >15 — 文件体积暴增却无明显视觉提升
- 网页未使用懒加载 — 阻塞页面加载
- 本可用视频却使用巨大GIF — 体积大10倍
API快速参考
Giphy搜索:
bash
curl https://api.giphy.com/v1/gifs/search?apikey=$GIPHYAPI_KEY&q=thumbs+up&limit=10
Tenor搜索:
bash
curl https://tenor.googleapis.com/v2/search?key=$TENORAPIKEY&q=thumbs+up&limit=10