Convert MP4 to GIF with FFmpeg

Cut the clip with -ss and -t, lower the frame rate and width, and build a palette from the clip itself with palettegen and paletteuse in one command. Every GIF below is the real file these commands wrote from the same 3 seconds of video.

Best quality Re-encode

The default choice. A palette made from your clip, error-diffusion dithering, no colour cast.

ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" out.gif

480x205, 30 frames, loops forever. A 256-colour palette built from this clip.

Frames
30 at 480x205
Colours
256 from the clip
Run time
799 ms
Output
2.6 MB
Smaller Re-encode

The file has a size limit. Fewer colours and ordered dithering compress better.

ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen=max_colors=64[p];[b][p]paletteuse=dither=bayer" out.gif

Same size and frame rate, 64 colours, 35% smaller than the 256-colour GIF. A fine crosshatch shows in flat areas.

Frames
30 at 480x205
Colours
64
Run time
516 ms
Output
1.7 MB

Proof

The same 3 seconds, three ways

Each GIF is 480x205 at 10 fps. Only the palette and dithering change.
palettegen + paletteuse Clean colour and skin
Animated GIF made with palettegen and paletteuse: a hand reaching for the baby dragon on a rooftop, clean colours
2.6 MB The palette comes from these 30 frames, so skin, stone and sky get the colours they need.
fps=10,scale=480:-1 only Crosshatch and colour cast
Animated GIF made without a palette: the same shot covered in a coloured crosshatch
1.4 MB Without palettegen FFmpeg maps every frame to one fixed palette. Smaller, but grainy and off-colour.
max_colors=64, dither=bayer Smaller, faint pattern
Animated GIF with 64 colours and Bayer dithering: the same shot with a faint regular pattern in the skin
1.7 MB A quarter of the colours. Bayer dithering leaves a regular pattern, which compresses better than noise.
frame 15 at 2x
Frame 15 of four GIFs at twice the size: no palette top left, palettegen top right, Bayer bottom left, no dithering bottom right
4 GIFs Top left no palette, top right palettegen with default dithering, bottom left dither=bayer:bayer_scale=3, bottom right dither=none.

Footage: Sintel, © Blender Foundation, CC BY 3.0.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/mp4-to-gif/in.mp4",
},
params: {
command: "ffmpeg -ss 78 -t 3 -i in.mp4 -vf \"fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse\" out.gif",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/mp4-to-gif/in.mp4"
},
"params": {
"command": "ffmpeg -ss 78 -t 3 -i in.mp4 -vf \"fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse\" out.gif"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Make a smaller GIF, 320 wide

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" out.gif

    320x137, 1.2 MB, 45% of the 480-wide file. Width is the biggest size lever you have.

    512 ms run 1.2 MB 3.00 s long

  2. Make a smoother GIF, 20 fps

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=20,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" out.gif

    60 frames, 5.2 MB. Twice the frames gave 1.96 times the file.

    1.2 s run 5.2 MB 3.00 s long

  3. Make the GIF play once, no loop

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop -1 out.gif

    The same frames without the looping block, 19 bytes smaller. Without that block a GIF plays once.

    781 ms run 2.6 MB 3.00 s long

  4. Turn dithering off

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse=dither=none" out.gif

    2.5 MB, 7% smaller than the default dithering, with no dither noise. Bottom right in the zoom above.

    595 ms run 2.5 MB 3.00 s long

  5. Use ordered (Bayer) dithering

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse=dither=bayer:bayer_scale=3" out.gif

    2.5 MB. A regular pattern instead of noise, 5% smaller than the default dithering.

    598 ms run 2.5 MB 3.00 s long

  6. Give every frame its own palette

    Re-encode
    ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen=stats_mode=single[p];[b][p]paletteuse=new=1" out.gif

    2.6 MB, 2% smaller than one shared palette on this single shot.

    1.8 s run 2.6 MB 3.00 s long

Gotchas

Where it goes wrong

Why does my GIF look grainy or off-colour?

You converted without a palette. GIF holds 256 colours per frame, and without palettegen FFmpeg uses one fixed set for every video:

ffmpeg -ss 78 -t 3 -i in.mp4 -vf "fps=10,scale=480:-1" out.gif

1.4 MB, with a coloured crosshatch over skin and stone and a cast on the bright sky. Compare the first two GIFs above.

The palette version came out 1.9 times bigger, 2.6 MB, because its smoother dithering compresses worse. That is the price of the better picture. Cut the size with width, frame rate or max_colors, not by dropping the palette.

Why is my GIF so big?

GIF has no motion prediction. Each frame is a paletted picture squeezed with LZW. Converting with no options keeps the full size and frame rate:

ffmpeg -ss 78 -t 3 -i in.mp4 out.gif

22.2 MB for 3 seconds: 72 frames at 1280x546.

The same 3 seconds as an H.264 MP4 at 480 wide was 206 KB. The 480-wide palette GIF is 12.8 times that. What moved the size on this clip:

  • Width: 640 wide 4.6 MB, 480 wide 2.6 MB, 320 wide 1.2 MB.
  • Frame rate: 20 fps made the file 1.96 times the 10 fps one.
  • Colours: max_colors=64 took it to 1.8 MB, and 1.7 MB with dither=bayer.
  • Not much: stats_mode=diff with diff_mode=rectangle made no difference here, 2.6 MB.

How do I make a GIF loop, or play only once?

FFmpeg loops GIFs forever by default. It writes a NETSCAPE2.0 block into the file, which is what browsers read. -loop -1 leaves the block out, so the GIF plays once and stops on its last frame.

-loop is an output option, so put it after the filters, right before the output name.

Related: Trim a video, Extract frames, Resize a video, Change frame rate.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.