Creating an animated GIF from a video file

VideoFFMPEG
23 Aug 2021

FFMPEG is a powerful tool which achieves this with one line of code.

For a basic conversion this will work…

file=yourfile.mp4;ffmpeg -i ${file} -filter_complex "[0:v] split [a][b];[a] palettegen [p];[b][p] paletteuse" ${file%.*}.gif
file=yourfile.mp4;ffmpeg -i ${file} -filter_complex "[0:v] split [a][b];[a] palettegen [p];[b][p] paletteuse" ${file%.*}.gif

Where yourfile.mp4 is the name of the video. It will output and animated gif with the filename yourfile.gif.

Reducing filesize

Often the filesize of a generated video can get very large, as GIFs are a much less optimised for video.

Two simple ways to reduce the filesize are by…

This can be done by modifying the -filter_complex to…

-filter_complex "[0:v] fps=24,scale=640:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse"
-filter_complex "[0:v] fps=24,scale=640:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse"

In this example the framerate (frames per second) is set with fps=24 and the width of the video is set with scale=640:-1.

The 640 refers to 640px wide and :-1 indicates that the aspect ratio of the video should be maintained, so the height scales to match the width.

The cut and paste command is…

file=yourfile.mp4;ffmpeg -i ${file} -filter_complex "[0:v] fps=24,scale=640:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse" ${file%.*}.gif
file=yourfile.mp4;ffmpeg -i ${file} -filter_complex "[0:v] fps=24,scale=640:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse" ${file%.*}.gif

Resources