brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg palettegen makes a GIF that doesn't look washed out

Ffmpeg
~/tips/tip-ffmpeg-gif-palettegen

ffmpeg -i clip.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" clip.gif

Builds a GIF from a video with a color palette made for that clip.

A GIF can show only 256 colors. When ffmpeg converts a video straight to GIF it uses a generic palette, and the result has banding and dull colors.

The fix is to let ffmpeg build a palette from the clip itself and then use it, in one command:

ffmpeg -i clip.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" clip.gif

Reading it from left to right: fps=10 lowers the frame rate, which makes the file much smaller. scale=480:-1 sets the width, and lanczos keeps it sharp. split sends the video two ways: one copy goes into palettegen, which picks the 256 best colors for this clip, and paletteuse applies that palette to the other copy.

I’d keep GIFs short. Even at 480 pixels wide and 10 frames a second, a 10-second test clip came out at about 1.5 MB, and that grows fast with length. For a web page, a muted MP4 or WebM with autoplay and loop is often a tenth of the size and looks better. GIF is for places that accept nothing else, like some chat apps and README files.

Further reading: FFmpeg filters documentation

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link