Published:

Updated:

Create a GIF from video with FFmpeg using a two-pass palettegen and paletteuse workflow. Trim a clip, choose FPS and width, and control looping and file size.

Create a High-Quality GIF from Video with FFmpeg

For a short, good-looking GIF, select a small part of the video, resize it, and generate a palette from those exact frames. GIF is limited to 256 colors; a custom palette can improve gradients and color selection, but cannot preserve every color from a video.

To adjust options in a form, use the MP4 to GIF tool. For a compact single-command alternative, see Convert MP4 to GIF with FFmpeg or the MP4 to GIF recipe. This guide covers the complete two-pass workflow.

Two-Pass Method: Generate and Apply a Palette

This example uses the three seconds starting at 00:00:05, 12 frames per second, and a width of 480 pixels. Replace input.mp4 with a source video at least eight seconds long. Use a new output filename if palette.png or clip.gif already exists.

First, generate a palette from the selected frames:

ffmpeg -ss 00:00:05 -t 3 -i "input.mp4" -vf "fps=12,scale=480:-1:flags=lanczos,palettegen" -frames:v 1 -update 1 "palette.png"

Then apply that palette to the same segment with the same FPS and scale:

ffmpeg -ss 00:00:05 -t 3 -i "input.mp4" -i "palette.png" -filter_complex "[0:v:0]fps=12,scale=480:-1:flags=lanczos[v];[v][1:v:0]paletteuse=dither=sierra2_4a[out]" -map "[out]" -an -loop 0 "clip.gif"
  • palettegen examines the processed frames and produces one palette image. -frames:v 1 -update 1 writes a single PNG rather than an image sequence.
  • paletteuse applies that image as the color palette. Its dithering distributes color error to make color transitions less abrupt; it can add visible texture and increase size.
  • flags=lanczos selects a sharp scaling algorithm. It does not restore detail from a low-resolution source.
  • [v], [1:v:0] and [out] connect the resized video, palette input and final output. -map "[out]" selects the filtered result.
  • -an omits audio; GIF has no audio track. -loop 0 requests indefinite playback.

Generate a new palette whenever you change the clip, frame rate or dimensions. A palette made from unrelated footage can give poor colors. The FFmpeg palette filters document the available palette and dithering options.

Select the Clip with -ss and -t

-ss is the starting time in the source; -t is the amount of source time to read. They appear before the video input in both commands above. With decoding and re-encoding, FFmpeg seeks and discards frames before the requested start using its default accurate-seek behavior.

Desired segment Settings in both passes
First 3 seconds -ss 0 -t 3
5 to 8 seconds -ss 00:00:05 -t 3
1 minute to 1 minute 4 seconds -ss 00:01:00 -t 4

Apply those options only to the video input, not to palette.png. Do not change just one pass: both passes need the same selected frames. Check that the source is longer than the requested start time; a start past the end leaves no frames to encode.

Optimize GIF Size: Duration, FPS, Scale and Dithering

Reduce duration first, then dimensions and frame rate. These are starting settings to compare on your own source, not fixed output-size guarantees:

Use Duration FPS Width Tradeoff
Small reaction or UI preview 2–3 seconds 8 320 px Less motion detail, fewer pixels
Balanced product preview 3–5 seconds 12 480 px A practical starting point
Larger demonstration 3–5 seconds 15 640 px More detail and smoother motion, usually more bytes

A smaller two-pass example uses 8 fps, a 320-pixel width and ordered Bayer dithering:

ffmpeg -ss 00:00:05 -t 3 -i "input.mp4" -vf "fps=8,scale=320:-1:flags=lanczos,palettegen=max_colors=128" -frames:v 1 -update 1 "palette-small.png"
ffmpeg -ss 00:00:05 -t 3 -i "input.mp4" -i "palette-small.png" -filter_complex "[0:v:0]fps=8,scale=320:-1:flags=lanczos[v];[v][1:v:0]paletteuse=dither=bayer:bayer_scale=5[out]" -map "[out]" -an -loop 0 "clip-small.gif"

scale=320:-1 keeps the aspect ratio. Lower FPS reduces the number of frames; lower width reduces pixels per frame. max_colors=128 limits the colors used, although the palette image still contains 256 entries. Ordered dithering may compress better on some clips, but its pattern can be visible; compare the result rather than assuming it is always smaller or better.

Busy backgrounds, grain and rapidly changing frames can still produce large GIFs. A GIF can exceed the size of its MP4 source even at a lower resolution. Avoid upscaling: increasing width beyond the source adds pixels without recovering detail.

Looping: What -loop 0 Does

-loop 0 makes the GIF repeat indefinitely. It controls playback metadata; it does not reduce the number of encoded frames, shorten the clip or act as a file-size compression setting. The frames are stored once and replayed by the viewer.

To request a single playback, use the same second-pass command with -loop -1:

ffmpeg -ss 00:00:05 -t 3 -i "input.mp4" -i "palette.png" -filter_complex "[0:v:0]fps=12,scale=480:-1:flags=lanczos[v];[v][1:v:0]paletteuse=dither=sierra2_4a[out]" -map "[out]" -an -loop -1 "clip-once.gif"

Viewer behavior can vary. See the FFmpeg GIF muxer options for loop and final-frame delay settings.

Common Problems

The colors look wrong or flat. Regenerate the palette from the exact segment you encode. Keep the same FPS and dimensions in both passes. Try the full palette and sierra2_4a dithering instead of the smaller Bayer variant; the 256-color limit still applies.

The palette is missing or the GIF command cannot open it. Let the first pass finish, run the second pass from the same directory, and quote paths containing spaces. If palette generation produced no image, check the start time and duration before running the second command.

The GIF is still too large. Shorten -t, reduce both passes to 320 pixels wide or lower FPS, and compare dithering options. -loop 0 will not solve a file-size problem.

The output duration is slightly different. GIF frame delays use hundredths of a second; rates such as 12 or 15 fps do not divide that clock evenly. Small timing differences can occur. Compare the animation with the intended clip before publishing.

Related GIF Pages

Related tool

MP4 to GIF with FFmpeg

Create high-quality GIF commands from MP4 using palette generation.

Open the command generator