📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

Convert list of image into GIF with ffmpeg

To convert images to gif you can simply run this command:

ffmpeg -i path/to/folder demo.gif

If you need to scale down the output size you can use this command:

ffmpeg -i output/project/%06d.png -filter_complex "[0:v]scale=640:-2,split[x][z];[x]palettegen[y];[z][y]paletteuse" demo.gif

One think you need to keep in mind is that the scale size should be match with the aspect ratio of the image size.

The formula to calculate the scale size is as follows:

output_width = input_width * output_height / input_height

For example, let's say you have an input image with a resolution of 1920x1080, and you want to scale it down to a maximum width of 640 pixels:

input_width = 1920
input_height = 1080
output_height = 320

output_width = 1920 * 320 / 1080 = 568

So in this case, you would use a scale filter of scale=568:320:flags=lanczos, which would scale the input image down to a maximum width of 568 pixels while preserving the aspect ratio.

Note that in the FFmpeg command I gave earlier, we used the scale filter scale=640:-2, where the -2 tells FFmpeg to automatically calculate the output height based on the input aspect ratio. This is a more general approach that works well in most cases where you want to scale the image while preserving the aspect ratio.