How to Convert MP4 to GIF with FFmpeg (High Quality, Small File)
Javid Jamae
·
2026-05-01
·
via DEV Community
<p><em>Originally published at <a href="https://www.ffmpeg-micro.com/blog/convert-mp4-to-gif-ffmpeg" rel="noopener noreferrer">ffmpeg-micro.com</a>.</em></p> <p>Converting an MP4 to a GIF with FFmpeg sounds simple until you actually try it. The default output is either a blurry mess or a 50MB file that nobody wants to load. The trick is knowing which FFmpeg flags control quality and file size independently.</p> <h2> The Basic Command (and Why It Looks Bad) </h2> <p>The most basic FFmpeg conversion looks like this:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>ffmpeg <span class="nt">-i</span> input.mp4 output.gif </code></pre> </div> <p>This technically works. But the result will look terrible.</p> <p>GIFs are limited to 256 colors per frame. FFmpeg's default color selection doesn't pick the right 256 to keep, so you get banding, dithering artifacts, and washed-out colors. The file will also be massive because there's no compression optimization happening. A 10-second 1080p clip can easily produce a 100MB+ GIF.</p> <h2> High-Quality GIFs with Palette Generation </h2> <p>The fix is FFmpeg's <code>palettegen</code> and <code>paletteuse</code> filters. This two-pass approach first analyzes your video to build an optimal 256-color palette, then converts using that palette instead of guessing.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code><span class="c"># Pass 1: Generate the optimal color palette</span> ffmpeg <span class="nt">-i</span> input.mp4 <span class="nt">-vf</span> <span class="s2">"fps=10,scale=480:-1:flags=lanczos,palettegen"</span> palette.png <span class="c"># Pass 2: Convert using that palette</span> ffmpeg <span class="nt">-i</span> input.mp4 <span class="nt">-i</span> palette.png <span class="nt">-filter_complex</span> <span class="s2">"fps=10,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse"</span> output.gif </code></pre> </div> <p>The difference is dramatic. Colors stay vibrant, gradients are smooth, and dithering artifacts mostly disappear.</p> <p>What each flag does:</p> <ul> <li> <code>fps=10</code> reduces the frame rate from the source (usually 24-30fps) to 10fps, cutting file size significantly</li> <li> <code>scale=480:-1</code> resizes to 480px wide while maintaining aspect ratio</li> <li> <code>flags=lanczos</code> uses Lanczos resampling for sharper downscaling</li> <li> <code>palettegen</code> analyzes all frames and builds the best 256-color palette</li> <li> <code>paletteuse</code> applies that palette during conversion</li> </ul> <p>You can also do this in a single command using <code>split</code> to avoid the intermediate palette file:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>ffmpeg <span class="nt">-i</span> input.mp4 <span class="nt">-filter_complex</span> <span class="s2">"[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse"</span> output.gif </code></pre> </div> <p>Same quality, one command, no temp files.</p> <h2> Controlling File Size </h2> <p>GIF files get big fast. Three things control the size: duration, resolution, and frame rate.</p> <p><strong>Trim to a specific clip</strong> with <code>-ss</code> (start time) and <code>-t</code> (duration):<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>ffmpeg <span class="nt">-ss</span> 00:00:05 <span class="nt">-t</span> 3 <span class="nt">-i</span> input.mp4 <span class="nt">-filter_complex</span> <span class="s2">"[0:v] fps=10,scale=320:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse"</span> output.gif </code></pre> </div> <p>This extracts 3 seconds starting at the 5-second mark.</p> <p><strong>Sizing reference:</strong></p> <div class="table-wrapper-paragraph"><table> <thead> <tr> <th>Resolution</th> <th>FPS</th> <th>5s clip</th> <th>10s clip</th> </tr> </thead> <tbody> <tr> <td>320px wide</td> <td>10</td> <td>~1-3MB</td> <td>~2-6MB</td> </tr> <tr> <td>480px wide</td> <td>10</td> <td>~3-6MB</td> <td>~5-12MB</td> </tr> <tr> <td>640px wide</td> <td>15</td> <td>~8-15MB</td> <td>~15-30MB</td> </tr> <tr> <td>1080px wide</td> <td>24</td> <td>~30-80MB</td> <td>~60-150MB</td> </tr> </tbody> </table></div> <p>For web use, 480px wide at 10fps is the sweet spot. Anything over 640px is usually overkill for a GIF.</p> <h2> Loop Control </h2> <p>GIFs loop forever by default. Control this with the <code>-loop</code> output option:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code><span class="c"># Play 3 times then stop</span> ffmpeg <span class="nt">-i</span> input.mp4 <span class="nt">-filter_complex</span> <span class="s2">"[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse"</span> <span class="nt">-loop</span> 3 output.gif <span class="c"># Play once, no loop</span> ffmpeg <span class="nt">-i</span> input.mp4 <span class="nt">-filter_complex</span> <span class="s2">"[0:v] fps=10,scale=480:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse"</span> <span class="nt">-loop</span> <span class="nt">-1</span> output.gif </code></pre> </div> <p><code>-loop 0</code> is infinite (the default). <code>-loop -1</code> plays once. <code>-loop N</code> plays N+1 times total.</p> <h2> Common Pitfalls </h2> <p><strong>The 256-color limit hits hardest on gradients and transitions.</strong> If your video has rapid color changes between scenes, you'll see color flickering. Adding <code>dither=bayer:bayer_scale=5</code> to the <code>paletteuse</code> filter smooths this out: <code>paletteuse=dither=bayer:bayer_scale=5</code>.</p> <p><strong>GIF transparency is binary.</strong> No semi-transparent pixels. If you're converting video with an alpha channel, expect hard edges instead of smooth blending.</p> <p><strong>Audio gets silently dropped.</strong> GIFs don't support audio and FFmpeg won't warn you about it. If you need sound, stick with MP4 or WebM.</p> <p><strong>Skipping palette generation is the single biggest mistake.</strong> The quality difference between a naive conversion and the palette method is night and day. Always use <code>palettegen</code> + <code>paletteuse</code> unless file size truly doesn't matter.</p> <h2> Convert MP4 to GIF with an API </h2> <p>If you'd rather skip the FFmpeg installation and two-pass workflow, FFmpeg Micro converts videos to GIF with a single API call:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>curl <span class="nt">-X</span> POST https://api.ffmpeg-micro.com/v1/transcodes <span class="se">\</span> <span class="nt">-H</span> <span class="s2">"Authorization: Bearer YOUR_API_KEY"</span> <span class="se">\</span> <span class="nt">-H</span> <span class="s2">"Content-Type: application/json"</span> <span class="se">\</span> <span class="nt">-d</span> <span class="s1">'{ "inputs": [{"url": "https://example.com/video.mp4"}], "outputFormat": "gif", "options": [ {"option": "-vf", "argument": "fps=10,scale=480:-1:flags=lanczos"} ] }'</span> </code></pre> </div> <p>You get back a job ID, poll for completion, and download the result. No infrastructure to manage, no FFmpeg to install, no server resources to worry about when processing volume spikes.</p> <p>Trim the source video before conversion with input-level options:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>curl <span class="nt">-X</span> POST https://api.ffmpeg-micro.com/v1/transcodes <span class="se">\</span> <span class="nt">-H</span> <span class="s2">"Authorization: Bearer YOUR_API_KEY"</span> <span class="se">\</span> <span class="nt">-H</span> <span class="s2">"Content-Type: application/json"</span> <span class="se">\</span> <span class="nt">-d</span> <span class="s1">'{ "inputs": [{ "url": "https://example.com/video.mp4", "options": [ {"option": "-ss", "argument": "5"}, {"option": "-t", "argument": "3"} ] }], "outputFormat": "gif", "options": [ {"option": "-vf", "argument": "fps=10,scale=480:-1:flags=lanczos"} ] }'</span> </code></pre> </div> <p>FFmpeg Micro handles the infrastructure and auto-scales with your workload. Pay per minute of video processed, with a free tier to start. <a href="https://www.ffmpeg-micro.com/pricing" rel="noopener noreferrer">Create your free account to get an API key</a>.</p> <p>For a deeper dive on FFmpeg encoding fundamentals, check out the <a href="https://dev.to/training/learn-ffmpeg">Learn FFmpeg course</a>.</p> <h2> Frequently Asked Questions </h2> <h3> What's the best resolution for a web GIF? </h3> <p>480px wide at 10fps works for most use cases. Short clips stay under 5MB, which loads quickly on any connection. Drop to 320px if the GIF is for chat or messaging apps where bandwidth matters more than detail.</p> <h3> Why does my FFmpeg GIF look grainy or washed out? </h3> <p>You're probably running <code>ffmpeg -i input.mp4 output.gif</code> without palette generation. FFmpeg's default color quantization produces poor results with only 256 colors to work with. The two-pass <code>palettegen</code> and <code>paletteuse</code> method described above fixes this completely.</p> <h3> Can I convert a GIF back to MP4 with FFmpeg? </h3> <p>Yes. Run <code>ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p output.mp4</code>. The MP4 will be much smaller because H.264 compression is far more efficient than GIF's LZW compression. Platforms like Twitter and Imgur convert uploaded GIFs to MP4 behind the scenes for this exact reason.</p> <h3> Is animated WebP better than GIF? </h3> <p>For most cases, yes. Animated WebP supports 24-bit color (16.7 million colors vs GIF's 256), produces smaller files at equivalent quality, and handles transparency with proper alpha blending. GIF's advantage is universal compatibility: it works in email clients, legacy browsers, and systems where WebP isn't supported.</p> <h3> How do I make a GIF from just part of a video? </h3> <p>Use <code>-ss</code> for the start time and <code>-t</code> for duration before the input: <code>ffmpeg -ss 00:00:30 -t 5 -i input.mp4 output.gif</code>. This grabs 5 seconds starting at the 30-second mark. Combine it with the palette method for the best quality.</p>
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。