How to Create a GIF from Images using FFmpeg?

In this article, we look at how to create a GIF file from a bunch of images using FFmpeg, adjust the frames-per-second (fps), speed, and looping using FFmpeg. To learn more about FFmpeg, please explore all our FFmpeg articles on OTTVerse.com.

Install FFmpeg first

If you don’t have FFmpeg installed, then you can download it from here or install it using these instructions for Windows, Mac, and Linux. Once you have FFmpeg installed on your computer, you can proceed with this tutorial.

Generating Images from a Video

To generate a set of images from a video, you need to take periodic screenshots or thumbnails from the video. You can do this using FFmpeg easily and the process is explained in this tutorial: Thumbnails & Screenshots using FFmpeg – 3 Efficient Techniques. The basic command is as follows –

ffmpeg -i simpsons.mp4 -vf "select='not(mod(n,10))',setpts='N/(30*TB)'" -f image2 simpimgs%03d.jpg

To learn how the commandline parameters work, do read the article linked above.

Now that you have the necessary images to create the GIF, let’s proceed to the next step.

Creating a GIF from Images using FFmpeg

Here is the command line for creating a GIF file using a series of images and the FFmpeg tool.

ffmpeg -f image2 -framerate 1 -i simpimgs%03d.jpg -loop -1 simpson.gif
  • -i simpimgs%03d.jpg – list of input images for the GIF file
  • -framerate 1 means that FFmpeg will display each image for 1 second (frames/second).
  • -loop -1 means that the GIF should not loop.
    • = loop infinitely and this is the default value.
    • 1 = loop once (i.e. play two times)
    • 2 = loop two times (i.e. play thrice)
  • simpson.gif – name of the output file.

GIF from images using FFmpeg

FrameRate of the GIF created using FFmpeg

Now, let’s speed up the GIF by increasing the frame rate to 10 fps as follows. You can see that the GIF completes much faster, right?

ffmpeg -f image2 -framerate 10 -i simpimgs%03d.jpg -loop -1 simpson_10fps.gif
GIF from images using FFmpeg

Looping a GIF using FFmpeg

Now, let’s take a look at creating a GIF that loops infinitely. We do this by controlling the -loop parameter and setting it to . As we saw earlier, the loop parameters are –

  • -1 = GIF should not loop.
  • = loop infinitely and this is the default value.
  • 1 = loop once (i.e. play two times)
  • 2 = loop two times (i.e. play thrice)

By setting the loop parameter to , we get the following GIF that plays infinitely.

Related:  Create Vintage Videos Using FFmpeg in 4 Simple Steps
GIF from images using FFmpeg

Conclusion

I hope this quick tutorial was useful to you and you know how to take a set of images and create a GIF out of it, and adjust the framerate, looping, etc. for the GIF.

Good luck, and happy streaming!

Be the first to comment

Leave a Reply

Your email address will not be published.


*