Animated GIF: Create, Optimize and Convert with FFmpeg and Gifsicle
GIF is the most universal animation format on the web. While animated WebP and APNG offer better quality and compression, GIF remains the standard for universal compatibility.
GIF Format Limitations
- Maximum 256 colors per frame (8 bits per pixel)
- 1-bit transparency: opaque or transparent, no semi-transparency
- No audio — animation/video only
- Large files compared to MP4 or animated WebP
- No native support in some offline players
Create GIF from Video with FFmpeg
Basic Conversion
# Video -> GIF
ffmpeg -i video.mp4 animation.gif
# With FPS and dimensions control
ffmpeg -i video.mp4 -vf "fps=10,scale=480:-1" animation.gif
Optimized Palette Method (High Quality)
# Pass 1: generate optimal palette
ffmpeg -i video.mp4 \
-vf "fps=12,scale=600:-1:flags=lanczos,palettegen=stats_mode=diff" \
palette.png
# Pass 2: create GIF using the palette
ffmpeg -i video.mp4 -i palette.png \
-filter_complex "fps=12,scale=600:-1:flags=lanczos[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=5" \
animation_hd.gif
Extract a Clip from Video
# From second 5 to second 10
ffmpeg -ss 00:00:05 -t 5 -i video.mp4 \
-vf "fps=10,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
clip.gif
# From 1:30 to 1:45
ffmpeg -ss 00:01:30 -to 00:01:45 -i video.mp4 \
-vf "fps=12,scale=640:-1:flags=lanczos" \
clip_640.gif
From Image Sequence
ffmpeg -framerate 10 -i frame_%03d.png animation.gif
ffmpeg -framerate 12 -i frame_%04d.jpg -loop 0 animation_loop.gif
Optimize GIF with Gifsicle
Gifsicle reduces GIF size by 30-70% without visible quality loss.
# Install
sudo apt-get install gifsicle # Ubuntu
brew install gifsicle # macOS
# Optimize
gifsicle -O3 --lossy=80 -i animation.gif -o animation_opt.gif
# --lossy=N: 30-100, higher = smaller file
# --colors 128: reduce palette to 128 colors
# Change speed (delay in hundredths of a second)
gifsicle --delay 10 animation.gif -o animation_10fps.gif
# Loop control
gifsicle --loopcount=0 animation.gif -o loop.gif
gifsicle --no-loopcount animation.gif -o once.gif
# Extract frame info
gifsicle --info animation.gif
gifsicle animation.gif '#0' > first_frame.gif
Create Animated GIF with Python
from PIL import Image
import os
def create_gif(frames_folder, output_path, duration=100, loop=0):
exts = {'.png', '.jpg', '.jpeg', '.bmp', '.webp'}
files = sorted([
os.path.join(frames_folder, f) for f in os.listdir(frames_folder)
if os.path.splitext(f)[1].lower() in exts
])
frames = [Image.open(f).convert('RGBA') for f in files]
frames[0].save(
output_path, format='GIF', save_all=True,
append_images=frames[1:], duration=duration,
loop=loop, optimize=True,
)
print(f"GIF: {output_path} ({len(frames)} frames, {duration}ms/frame)")
create_gif("frames/", "animation.gif", duration=80)
Loading Spinner
from PIL import Image, ImageDraw
def create_spinner(output="spinner.gif", size=100, n_frames=16):
frames = []
for i in range(n_frames):
img = Image.new('RGBA', (size, size), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
center = size // 2
radius = size // 2 - 10
angle = (360 / n_frames) * i
draw.arc(
[(center-radius, center-radius), (center+radius, center+radius)],
start=angle, end=angle+270,
fill=(59, 130, 246, 255), width=8
)
frames.append(img)
frames[0].save(
output, save_all=True, append_images=frames[1:],
duration=60, loop=0, disposal=2,
)
create_spinner()
Convert GIF to Other Formats
# GIF -> Animated WebP (~50% smaller)
ffmpeg -i animation.gif -c:v libwebp -loop 0 -quality 80 animation.webp
# GIF -> MP4 (5-10x smaller)
ffmpeg -i animation.gif \
-c:v libx264 -pix_fmt yuv420p -movflags faststart \
-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" \
animation.mp4
GIF vs Modern Alternatives
| Format | Compression | Transparency | Compatibility |
|---|---|---|---|
| GIF | Poor (256 colors) | 1-bit | Universal |
| Animated WebP | Excellent | Real alpha | Chrome/Firefox/Safari 14+ |
| APNG | Good | Real alpha | All except IE11 |
| MP4/WebM | Excellent | No | Universal |
Recommendation: animated WebP with GIF fallback for modern web. Pure GIF for maximum compatibility.
Convert Online
To convert videos to GIF or GIF to other formats without installing software, KaijuConverter handles it directly in your browser.
Related conversions
Most teams that read this guide convert images in one of these directions: