Convert Video to GIF with FFmpeg
Convert video to GIF with FFmpeg: a good-quality one-line command with a palette, how to keep the file small, and the same job as one API call.
To convert a video to a GIF with FFmpeg, set a frame rate and a width, and make a palette from the clip: ffmpeg -ss 10 -t 5 -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop 0 output.gif. The palette step is what makes the colours look right.
Below is the command with each part explained, how to keep the file small, the same job as one Xora API call, and the common errors. For other common commands, see the FFmpeg cheat sheet.
The local command
ffmpeg -ss 10 -t 5 -i input.mp4 \ -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" \ -loop 0 output.gif| Part | What it does |
|---|---|
-ss 10 -t 5 | Take 5 seconds from the 10-second mark. A GIF of a full video is very large. |
fps=10 | 10 frames per second. 10 to 15 is enough for most GIFs. |
scale=480:-1:flags=lanczos | 480 pixels wide, height to match. Lanczos scaling keeps edges sharp. |
split[a][b];[a]palettegen[p];[b][p]paletteuse | Make a 256-colour palette from the clip, then use it for the GIF. |
-loop 0 | Loop forever. Use -loop -1 for no loop. |
A quick GIF without a palette
ffmpeg -ss 10 -t 5 -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" -loop 0 quick.gifThis is shorter, but the colours are often grainy, more so for skin, skies and gradients.
The same job with the Xora API
There is no GIF recipe. Send the same arguments in mode: "ffmpeg":
{ "mode": "ffmpeg", "input_files": { "in_1": "https://example.com/demo.mp4" }, "output_files": { "out_1": "demo_clip.gif" }, "ffmpeg": { "args": [ "-ss", "10", "-t", "5", "-i", "{{in_1}}", "-vf", "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse", "-loop", "0", "{{out_1}}" ] }}The filter graph is one array element, so there are no shell quotes to escape. Send the body to https://api.xora.sh/v1/jobs with Authorization: Bearer YOUR_API_KEY, then poll GET /v1/jobs/{id} or pass webhookUrl, and download the GIF from the signed URL. See custom FFmpeg commands.
Keep the GIF small
| Change | Effect |
|---|---|
Shorter clip (-t) | The size goes down with the number of frames. |
Narrower (scale=320:-1) | Fewer pixels in each frame. |
Lower frame rate (fps=8) | Fewer frames. Below 8, motion looks choppy. |
paletteuse=dither=bayer:bayer_scale=5 | Less noise between frames, which often compresses better. |
palettegen=max_colors=128 | Fewer colours, smaller file, less detail. |
For anything longer than a few seconds, an MP4 is usually much smaller than a GIF. A muted, looping <video autoplay loop muted playsinline> plays like a GIF on the web. See how to compress video with FFmpeg.
Common errors
| Error | Cause | Fix |
|---|---|---|
| The GIF is many megabytes | The clip is long or wide. | Use a shorter clip, scale=320:-1 and fps=8. |
| Grainy colours or bands | No palette, or one palette for very different scenes. | Use palettegen and paletteuse. Cut one scene at a time. |
| An error about an unconnected or unknown filter label | Labels in the filter graph do not match. | Check that [a], [b] and [p] are each used once as an output and once as an input. |
VALIDATION_ERROR (Xora) | The output name has a hyphen or a space. | Use letters, numbers and underscores, for example demo_clip.gif. |
Failed and rejected Xora jobs cost nothing. See error handling.
Related
- Custom FFmpeg commands.
- FFmpeg cheat sheet: the common commands, each as an API call.
- How to trim and cut video with FFmpeg.
Frequently asked questions
How do I convert a video to a GIF with FFmpeg?
Run ffmpeg -ss 10 -t 5 -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop 0 output.gif. This takes 5 seconds from the 10-second mark, at 10 frames per second and 480 pixels wide, with a palette made for that clip.
Why does my FFmpeg GIF look grainy or have wrong colors?
A GIF can show only 256 colours. Without a palette, FFmpeg uses a general palette that does not fit the video. Add palettegen and paletteuse to make a palette from the clip itself.
How do I make a GIF smaller?
Make it shorter, narrower and with fewer frames per second. Width and duration have the largest effect. For a long or large clip, an MP4 or WebM file is usually much smaller than a GIF and plays in the same places on the web.
Can I convert video to GIF without installing FFmpeg?
Yes. Send the video URL and the same FFmpeg arguments to the Xora API in mode ffmpeg with an output name that ends in .gif. Xora returns a signed URL for the GIF.