An FFmpeg command for nice GIFs

It's fun to take breaks and share what you're working on during the weekend, but I always found it kind of frustrating to make a nice GIF, without downloading some bloated screen recording software. So a few years ago I whipped up an FFmpeg command for converting .mp4 to .gif, specifically for sharing on the LDjam site. The goals were: - Keep the file size as small as possible - Retain as much quality as possible - Resize to a consistent width

I'm sharing it here in case anyone finds it useful. Since I'm on Windows, I wrote it as a .bat script so that I can just drag and drop any video on it, and get a GIF without needing to do anything else. Tweak the FPS and WIDTH values as you see fit. (This assumes you have FFmpeg downloaded and available on your system PATH).

``` REM mp4togif.bat

SET FPS=30 SET WIDTH=320

SET SRC=%1 SET DST=%SRC%.gif SET PALETTE=%SRC%.palette.png

ffmpeg -y -i %SRC% -vf "fps=%FPS%,scale=%WIDTH%:-1:flags=lanczos,palettegen=statsmode=diff" %PALETTE% ffmpeg -y -i %SRC% -i %PALETTE% -lavfi "fps=%FPS%,scale=%WIDTH%:-1:flags=lanczos,mpdecimate,paletteuse=dither=bayer:bayerscale=5:diff_mode=rectangle" %DST% del %PALETTE% ```

Bonus info for the FFmpeg nerds: - Even though I record in 60FPS, I chose to retime to 30FPS so that I don't have to worry about file size as much (this site limits you to 4MB uploads) - Doing a 2-pass render to generate a palette first gets you way better color accuracy - The combination of mpdecimate and palettegen=diff options will optimize for situations where you have a lot of static backgrounds, with moving foregrounds. I'm mostly doing pixel art with static-ish backgrounds, so this works well for me.