This tutorial explains how to create animated GIF from images using FFmpeg. Here I will talk about a handy method to instantly create a GIF from a set of JPG images. To do this, I will use FFmpeg, which is a powerful audio/video processing suite that acts as backend to a lot of applications.
FFmpeg does not give any inherent method to convert images to gifs, but there is an interesting workaround. First I will create a video file from the images and then convert that video to GIF using a palette file and of course FFmpeg. All this would be done with just a few commands.
FFmpeg is a command line tool that lets you manipulate each frame of a video. You can do almost anything that you want using this tool, like create time-lapse video, denoise video, and a lot more. Today let’s see how to use FFmpeg to create animated gifs from images.
There are other software also out there that you can use to convert a video to GIF or create a GIF. But the advantage of using FFmpeg is that you will get more options for the output GIF. You can generate a high quality GIF by customizing the parameters like desired frame rate, scaling parameters, rotating input images, and a few other options. You can specify all these in a single command and generate final GIF. And then you can just reuse the commands to create many more similar gifs.
How to Create Animated GIF from Images using FFMPEG?
For creating an animated GIF from images using FFmpeg, you just have to install it on your PC. You can use this link to download FFMPEG. Also, make sure you have the images that you will use to create GIF. It is helpful if your images have similar names, and have a number that denote the sequence in which you want images to come in the gif. Like: image1.jpg, image2.jpg, image3.jpg, and so on.
Once your images are ready with proper names, follow these steps:
Step 1: Open command prompt in the folder that has the input images for the GIF. Since, we want to generate a high quality GIF, so we will have to generate a color palette from the input images. Without color palette, the final GIF will not be visually pleasing.
Run this command to generate a color palette from the input images:
ffmpeg -f image2 -i "InputImagesLocaltion" -vf scale=900:-1:sws_dither=ed,palettegen "PaletteImage"
(in the above command, replace “InputImagesLocation” with the folder path and filename pattern where your images are which will make the gif. “PaletteImage” would be replaced by name of the palette. It can be anything like “palette.png”).
Note: In this command, I have used “image%d.jpg” as the name of the input image. This is because the name of images in my case are like image1.jpg, image2.jpg… and so on. If you want to use the same command, then you will have to make sure that the input images on your PC have the names same as mine. Otherwise, you will have to change the format of the filename.
Step 2: Run this command to convert the input images to an intermediary video. You can choose any video format here and you just have to append that in the end of the output video name. For example, here I have used “FLV” as the output video option.
ffmpeg -f image2 -framerate 1.2 -i "InputImagesLocaltion" "OutputVideoName"
Step 3: Now, generate the final GIF by running this command, it will use the intermediary video and the palette image that we have generated in above steps to create the final GIF. The final GIF will be placed in the current working directory.
ffmpeg -i "OutputVideoName" -i "PaletteImage" -filter_complex "fps=1.2,scale=500:-1:flags=lanczos[x];[x][1:v]paletteuse" "OutputGIF"
That’s it. This is all it takes to create an animated GIF from images. If you have never touched FFmpeg to do anything, then you will find it a bit back-breaking. But after once you successfully create a GIF, it will be too easy for you to use it.
In case you want to change frame rate, or dimensions, then play around with “filter_complex” that I have used in the command above. You can read more about it here.
Final thoughts
A lot of people don’t use FFmpeg because it is a command line tool. But actually this is an ideal tool for a lot of audio-video related operations like generating a GIF from images. All you have to do is run a few commands to create a high quality GIF from images and I really liked that. You can use this method on any platform in the same way.