How to convert video to animated GIF using FFmpeg

Animated GIFs are often used where it is difficult or impractical to use video files. Common scenarios are in emails, where videos are not supported, or on websites, where you want the animation to autoplay. Short snippets from films and television shows are often turned into GIFs and used to create memes. But how do you go about converting a video to an animated GIF?

While it is possible to use Shotstacks video editing or media transformation APIs to generate animated GIFs, you may just want to do this manually on your own computer. In this article, we will show you how to convert a video to an animated GIF using FFmpeg.

What is FFmpeg?

FFmpeg is free and open-source software used for handling video, audio, and other multimedia files and streams. It is a command-line tool that can be used to convert video and audio files, as well as perform other tasks such as video streaming, recording, editing and transcoding. It is available for Windows, Mac OS X, and Linux.

Prerequisites

To follow along with this tutorial, make sure you have FFmpeg installed. You can check if FFmpeg is installed by running the following command in your terminal:

ffmpeg -version

If you don't have FFmpeg installed, you can download it from the official FFmpeg website.

To generate a GIF from a video, you will need FFmpeg v2.6 or higher. Versions higher than v2.6 include the palettegen and paletteuse filters, which you will learn more about later in this article. FFmpeg v2.6 was released in 2015, so you should be fine if you have installed FFmpeg more recently.

You will also need a sample video file to test the commands with. You can use any video file you have on your computer, or you can download and save this test file we prepared:

Download the file to your computer and save it as input.mp4 in a folder where you will run the FFmpeg commands.

Converting the MP4 video to animated GIF

Open your terminal (or command prompt on Windows) and navigate to the folder where you saved the video file. To convert the video to a GIF, run the following command:

ffmpeg -i input.mp4 output.gif

Here's a break down of the command and options:

  • ffmpeg: the ffmpeg software program or command we want to run.
  • -i input.mp4: the name of the input video, in our case we use input.mp4.
  • output.gif: the name of the output GIF file.

Once the command completes you should see a new file next to the input.mp4 file called output.gif which you should be able to view. By simply providing the output file name with the .gif extension, FFmpeg knows to automatically convert the video to a GIF.

Converting part of a video to a GIF

It is common to see GIFs used as memes where only a few seconds of a video are converted to a GIF. To do this, run the following command:

ffmpeg -i input.mp4 -ss 5.0 -t 2 trimmed.gif

Here's what the options in this command do:

  • -ss 5: extracts the video starting at the 5 second mark.
  • -t 2: sets the duration of the output video.

The FFmpeg command above converts the mp4 video to a GIF starting at the 5 second mark and lasting for 2 seconds. The output file is called trimmed.gif.

Creating a looping GIF using FFmpeg

It is also common to see GIFs that loop over and over again. To create a looping GIF, we can use the -loop option. The -loop option takes a number as a parameter, which specifies the number of times the GIF should loop:

ffmpeg -i input.mp4 -loop 0 looped.gif

In this example, the -loop 0 option tells FFmpeg to loop the GIF forever. You can also specify a number, which will loop the GIF that many times. For example, -loop 1 will loop the GIF once, -loop 2 will loop the GIF twice, and so on. Notice that a loop count of 1 will play the GIF twice, as the GIF plays once and then loops once. To not loop the GIF at all, you can use -loop -1 setting or just omit the -loop option altogether.

Optimizing the GIF file size and quality

If you look at the files size of the GIF's you just created you will notice they are much larger than the original video. If you used the provided whale.mp4 video, the original file is 8.6MB, but the GIF is 140MB. Even the 2 second trimmed video is 16MB.

This is down to how videos and GIF's are compressed. Video file compression and codecs such as H.264 and H.265 are highly efficient for encoding moving images. GIF's, on the other hand, are not as efficient where each frame of the GIF is stored as a separate image, which results in a much larger file size.

Whenever you create media files for the internet, whether it is a video, image, or audio file, you should always try to optimize the file size for fast page load times, reduced storage and bandwidth costs, and a better user experience.

Reducing the animated GIF file size

You might have noticed that GIF's shared online are very small in terms of width and height (i.e. their resolution). Unlike videos where it is desirable to use HD resolutions, GIFs are the opposite and use much smaller dimensions. The smaller the dimensions, the smaller the file size.

Another way to reduce the file size is to reduce the frame rate. GIF's playback can sometimes appear a little bit jerky. This is normally because the frame rate has been reduced. The frame rate is the number of frames per second (fps) that are displayed. The higher the frame rate, the smoother the animation, but the larger the file size. Reducing the frame rate will reduce the file size, but the animation will be less smooth.

Here's a command to reduce the GIFs dimensions and frame rate:

ffmpeg -i input.mp4 -filter_complex "fps=10, scale=-1:360" compressed.gif

The options in this command do the following:

  • -filter_complex: applies one or more filters to the output, the filters are enclosed in the quotes.
  • fps=10: sets the frame rate to 10 frames per second, our original whale.mp4 footage uses 60fps.
  • scale=-1:360: sets the output height to 360px and scales the width proportionally.

Our new GIF; compressed.gif, is now 10.6MB, which is a 92% reduction in file size. The quality is not as good as the original, but it is still watchable and much better than the 140MB GIF we created earlier.

Improving GIF visual quality

One of the limitations of GIF's is that they only support 256 colors per frame. This means that if you have a video with a lot of colors, you can see color distortion or banding in the GIF. This is especially true if you have a lot of gradients in your video.

To work around this issue, FFmpeg uses a filter called palettegen to generate a custom color palette for the GIF. The palettegen filter is used in conjunction with the paletteuse filter to create a GIF with a colour palette closer to the original video and improves the visual quality of the GIF.

Using our original input.mp4 footage, we can create a new GIF with a custom color palette using the following command:

ffmpeg -i input.mp4 -filter_complex "[0]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" palette.gif

Again we use filter_complex to apply multiple filters to the output. The filters are enclosed in quotes and separated by a semicolon. Here's what each filter does:

  • [0]split[a][b]: divide the input video into two streams, labelled a and b.
  • [a]palettegen[palette]: directs the a stream to palettegen, creating a custom color palette stored in a stream named palette.
  • [b][palette]paletteuse: combines the b stream with the custom palette from palette, using paletteuse.

You will notice that the encoding process is very slow while the color palette is generated. The resulting GIF is even larger than the first output.gif file we created (462MB), but the visual quality is much better.

Generating an optimised video to GIF file

Putting all of the above together, we can create a new GIF that is both smaller in file size and better in visual quality. We'll also trim the video and use a short segment to create a GIF that can be used online. Finally we'll add an infinite loop.

ffmpeg -i input.mp4 -ss 5 -t 2 -loop 0 -filter_complex "fps=10, scale=-1:360[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" final.gif

This command brings everything together and turns our original video in to a 3MB, 2 second, great looking, looping GIF. All of the options should now be familiar except for the [s] label. This is a label we use to reference the stream that is created by the fps and scale filters. We then use this label to split the stream into two streams, a and b.

Here is our final animated GIF:

Final GIF created using FFmpeg from a video

Conclusion:

Using FFmpeg and the options and filters in this tutorial you should be able to convert videos to GIF's that are optimized for the web and look great. FFmpeg is an ideal tool for local conversion of videos to GIF's, but if you need to convert videos to GIF's at scale, you should consider using a video API such as Shotstack.

Shotstack can convert videos to GIFs in the cloud, using the same filters and options as FFmpeg, but without the need to install any software or write any code making it a worthwhile FFmpeg alternative.

Tharikh Babu

BY THARIKH BABU
11th December, 2023

Become an Automated Video Editing Pro

Every month we share articles like this one to keep you up to speed with automated video editing.


You might also like

Batch convert videos using FFmpeg

Batch convert videos using FFmpeg

Maab Saleem
How to crop and resize videos using FFmpeg

How to crop and resize videos using FFmpeg

Kathy Calilao
 Convert videos to MP3 using FFmpeg

Convert videos to MP3 using FFmpeg

Jeff Shillitto