Sometimes I need to convert a video to a GIF, usually for sharing in a GitHub issue, pull request, or README where embedding video isn’t ideal. The problem is GIFs can balloon in size fast. A few seconds of video can easily become a 50MB GIF if you’re not careful.

FFmpeg handles this well. If you’re on Mac it can be installed via brew.

brew install ffmpeg

The trick to reasonable GIF sizes is generating a color palette from the video and using that palette when creating the GIF. This produces much smaller files with better quality than the default palette.

ffmpeg -i $in -vf "fps=10,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" $out

Replace $in with the input file, and $out with where you want the output GIF to be written.

A few things to tweak depending on your needs:

  • fps=10 controls the frame rate. Lower means smaller files. 10 is a good default.
  • scale=640:-1 sets the width to 640 pixels and scales height proportionally. Reduce this for smaller files.