Joshua's Docs - FFMPEG - Assorted Notes and Snippets

Converting to GIF

If you want an online alternative, with conversion and optimization support, I've always had excellent luck with EZGif.

Here is the baseline command to convert from a video to GIF with FFMPEG:

ffmpeg -i MyVideo.mp4 -f gif MyGif.gif

However, in most scenarios, this is going to result in a huge file size for the resulting GIF, unless your input video clip is already heavily optimized (scaled, low FPS, etc.). To generate a more compact GIF, we can alter some options. For example:

-filter_complex "[0:v] fps=12,scale=480:-1"

The above limits the video track to 12 FPS, and the resolution to 480 x {auto} (maintaining aspect ratio).

GIPHY Engineering has a detailed writeup on how to use some of these advanced options to produce better GIF outputs.

Putting it all together, here is a sample command I have used to produce a minimal GIF:

ffmpeg -i my-video.mp4 -vf "fps=10,scale=360:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 -f gif unopt.gif

This StackExchange answer explains the command in more detail

To further optimize the output, I prefer to use gifsicle after generating the GIF with FFMPEG (examples).

Here is a variant I commonly use:

gifsicle -i unopt.gif --optimize=3 --colors 255 --lossy=30 -o optimized.gif

Optimizing File Size

Although there are many ways to get the image quality better in the output GIF with FFMPEG, which increases the file size, there are very few options for optimizing the output to reduce file size. Your main option is to just lower the output resolution.

However, there are both online tools and offline tools for optimizing GIF files, so you can use FFMPEG for the conversion, and then run the output through a secondary optimization step.

Markdown Source Last Updated:
Mon Jul 19 2021 02:10:22 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Tue May 18 2021 21:08:29 GMT+0000 (Coordinated Universal Time)
© 2024 Joshua Tzucker, Built with Gatsby