Create High Quality GIFs with ffmpeg

February - 2021

TL;DR

This command uses ffmpeg to convert a video into a high quality GIF:

ffmpeg -i "INPUT_PATH.mp4" \
-vf "fps=16,scale=600:-2:flags=lanczos,split[s0][s1];\
[s0]palettegen=max_colors=128:reserve_transparent=0[p];\
[s1][p]paletteuse" \
-y "OUTPUT_PATH.gif"

This is the same thing, but it clips out a section of the video starting at 2.3 seconds that's 1.3 seconds long instead of processing the entire clip:

ffmpeg -i "INPUT_PATH.mp4" -ss 2.3 -t 1.3 \
-vf "fps=16,scale=600:-2:flags=lanczos,split[s0][s1];\
[s0]palettegen=max_colors=128:reserve_transparent=0[p];\
[s1][p]paletteuse" \
-y "OUTPUT_PATH.gif"

Making The GIF

The command produces a high quality GIF at a reasonable size. You'll need ffmpeg installed to run it. Once you have that, change the INPUT_PATH.mp4 and OUTPUT_PATH.gif to whatever filenames you want and you'll be good to go.

The example is an .mp4, but ffmpeg can read most anything you throw at it.

Settings

Here are the different ways you can tweak the command:

  • fps: is for the frames per second. Setting the value larger make the GIF smoother but bigger. Smaller values reduce the file size but make it choppier.
  • scale: sets the pixel size of the output. The format is "width:height". The example uses scale=600:-2 which sets the width to 600 pixels and then automatically sets the height based off the original image. (The -2 means the height will be set to a even number of pixels.) Reverse the numbers if you'd rather set the height (e.g. `scale=-2:600).
  • max_colors: GIFs can have to up to 256 colors. ffmpeg will use all of them by default. If you lower the number it reduces the files size as a trade off being the reduced color pallet. Most GIFs work fine with 128 and lots work well with 64. It's all about how dynamic the original color pallet is.

Clipping Videos

You use the flags to set the start and end points for clipping the video if you want to go that route.

  • ss: The starting second to begin the clip at. This can be an integer, a decimal, or a timestamp. For example, "1", "1.4", or "1:15", respectively.
  • t: The length of time in seconds that you want the clip to be. This can be either an integer (e.g. "7") or a decimal (e.g. "4.2")

GIF Away

All of these settings are a matter of personal taste and where you're going to publish them. The defaults work great for most thing. If you want to try to reduce file size or deal with an image with a huge color pallet they're worth messing around with.

And, if you're interested here are some examples.