1. Install the tools

sudo apt update
sudo apt install ffmpeg gifsicle imagemagick
  • ffmpeg – best quality + most control
  • gifsicle – excellent for optimizing / shrinking existing GIFs
  • ImageMagick – simple one-liners from images or short clips

2. High-quality GIF from a video (recommended method)

The best results come from a two-pass palette approach:

# Extract a short clip and generate an optimized palette
ffmpeg -ss 00:00:05 -t 4 -i input.mp4 \
  -vf "fps=12,scale=480:-1:flags=lanczos,palettegen" palette.png

# Create the GIF using that palette
ffmpeg -ss 00:00:05 -t 4 -i input.mp4 -i palette.png \
  -filter_complex "fps=12,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse" \
  output.gif

Useful options you can change:

OptionExampleEffect
Start time-ss 00:01:23Seek to that point
Duration-t 3 or -t 00:00:03Length of the GIF
Frame ratefps=10 / fps=15 / fps=24Lower = smaller file
Widthscale=320:-1 / scale=640:-1Height auto-calculated
Scaling qualityflags=lanczosBest quality (recommended)
Ditheringpaletteuse=dither=bayer:bayer_scale=3Different dither styles

One-liner version of the same thing:

ffmpeg -ss 00:00:05 -t 4 -i input.mp4 \
  -vf "fps=12,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
  output.gif

3. Quick & simple GIF (lower quality)

ffmpeg -ss 00:00:05 -t 3 -i input.mp4 -vf "fps=10,scale=400:-1" output.gif

4. From a sequence of images

# Numbered images: frame_001.png, frame_002.png …
ffmpeg -framerate 12 -i frame_%03d.png -vf "scale=480:-1:flags=lanczos" output.gif

# Or with ImageMagick
convert -delay 8 -loop 0 frame_*.png output.gif

5. Optimize / shrink an existing GIF

# Aggressive optimization
gifsicle -O3 --lossy=80 input.gif -o optimized.gif

# Resize + optimize
gifsicle -O3 --resize 320x_ input.gif -o small.gif

# Reduce colors
gifsicle -O3 --colors 128 input.gif -o reduced.gif

6. Other useful variations

Loop forever (default) or play once:

# Already loops by default. To play only once:
ffmpeg ... -loop 1 output.gif   # (actually use -loop 0 for infinite)

Crop a region first:

ffmpeg -i input.mp4 -vf "crop=640:360:100:50,fps=12,scale=480:-1:flags=lanczos,..." ...

Add text overlay:

ffmpeg -i input.mp4 -vf "drawtext=text='Hello':fontsize=24:fontcolor=white:x=10:y=10,fps=12,..." ...

Quick recipe summary

GoalCommand style
Best quality from videoTwo-pass palette (fps + scale + palettegen/use)
Fast & simpleSingle ffmpeg with fps + scale
From image sequenceffmpeg -framerate … -i frame_%03d.png or convert
Make file much smallergifsicle -O3 --lossy=80
Control sizeChange scale=WIDTH:-1 and fps=

By davs

I exists and eat, a lot

Leave a Reply