How to Create a GIF from a Video using FFmpeg with Examples

Animated GIFs have become a popular medium for expressing emotions, sharing snippets of videos, and adding visual interest to online content.

While there are numerous tools available for creating GIFs, FFmpeg stands out as a powerful and versatile option, especially for video engineers and enthusiasts. In this comprehensive guide, I will teach you how to create a GIF from a video using FFmpeg.

From installing FFmpeg to optimizing settings, this tutorial will equip you with the knowledge and skills to craft stunning GIFs that captivate your audience.


Step 1: Installing FFmpeg

Before we dive into creating GIFs from videos, it’s important to have FFmpeg installed on your system. Here’s a step-by-step guide to installing FFmpeg on different operating systems:

  • Windows: Download the latest build of FFmpeg from the official website or use a package manager like Chocolatey or Scoop. Or, follow our FFMpeg installation guide on OTTVerse.
  • macOS: Install FFmpeg via Homebrew or MacPorts package managers.
  • Linux: Use your distribution’s package manager to install FFmpeg or compile it from source.

Once FFmpeg is installed, verify its installation by opening the command prompt or terminal and typing “ffmpeg -version” to ensure the correct version is displayed.

Step 2: Prepare Your Source Video

To create a GIF from a video using FFmpeg, you first need a suitable source video. Consider the following guidelines when selecting your video:

  1. Clip Length: Choose a short video clip or sequence that highlights the desired movement or action.
  2. Content Relevance: Ensure the selected clip aligns with the intended message or emotion of your GIF.
  3. Trimming and Editing: If necessary, trim and edit the video to isolate the specific section you want to convert into a GIF using a video editing software of your choice.

Step 3: Adjusting GIF Parameters

To achieve the desired result, it’s crucial to adjust the GIF’s parameters, including the frame rate, duration, and size. Here’s how to modify these parameters using FFmpeg:

  • Frame Rate: Add the “-r” option followed by the desired frame rate in frames per second. For example: ffmpeg -i input.mp4 -r 10 output.gif
  • Duration: Include the “-t” option followed by the desired duration in seconds. For example: ffmpeg -i input.mp4 -t 5 output.gif
  • Size of the GIF: Use the “-s” option followed by the desired width and height dimensions. For example: ffmpeg -i input.mp4 -s 480x320 output.gif
  • Bitrate Adjustment: Modify the bitrate with the “-b:v” option. For example: ffmpeg -i input.mp4 -b:v 500k output.gif

Experiment with these parameters to achieve the desired balance between file size and visual quality.

Step 4: Controlling Animation Looping

By default, animated GIFs loop continuously. However, FFmpeg allows you to control the looping behavior. Here are some options:

  1. Loop Forever: Include the “-loop 0” option. For example: ffmpeg -i input.mp4 -loop 0 output.gif
  2. Loop Specific Times: Add the “-loop <number>” option, where <number> represents the desired number of loops. For example: ffmpeg -i input.mp4 -loop 3 output.gif
  3. No Loop: Use the “-loop -1” option. For example: ffmpeg -i input.mp4 -loop -1 output.gif

These options provide flexibility in defining how your animated GIF repeats.

Step 6: Applying Filters and Effects

Most GIFs have some sort of color filters added to them, or even text overlays. Luckily, FFMpeg can do all of this and more! Here are some quick commandlines you can use for adding text, filtering, and adding overlays.

  1. Adding Text: Utilize the “drawtext” filter. For example: ffmpeg -i input.mp4 -vf "drawtext=text='Hello':fontsize=24:fontcolor=white:x=10:y=10" output.gif
  2. Applying Filters: Use various filters to apply visual effects. For example: ffmpeg -i input.mp4 -vf "boxblur=5:1" output.gif
  3. Creating Overlays: Composite images or logos using the “overlay” filter. For example: ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=10:10" output.gif

Experiment with different filters and effects to enhance the visual appeal of your animated GIFs

Examples of Creating GIFs using FFmpeg

Here are a few examples of different GIFs with various features using FFmpeg, along with the command lines and explanations of the parameters. Hope this is helpful in creating your own examples :-

Short GIF with Adjusted Frame Rate

./ffmpeg -i simpsons-trailer.mp4 -r 3 -t 10 -loop 1 simpsons.gif               

This command creates a GIF from the “input.mp4” video file. It adjusts the frame rate to 3 frames per second using the -r option, and limits the duration to 10 seconds using the -t option. The resulting GIF can be seen below.

Create a GIF from a video using FFmpeg

GIF with Text Overlay

./ffmpeg -i simpsons-trailer.mp4 -r 15 -t 3 -loop 6 -vf "drawtext=text='Hello':fontsize=36:fontcolor=black:x=100:y=100" simpsons-text.gif

In this example, the GIF is created from the “input.mp4” video file. The -vf option applies the “drawtext” filter, which overlays the text “Hello, World!” at the center of the GIF.

“fontsize” sets the size of the text to 24, and “fontcolor” determines the color of the text (white in this case). The resulting GIF, named “output.gif,” will display the text as seen below in the example.

GIF from  a video using FFmpeg with text overlay

GIF with Blurring Filter

./ffmpeg -i simpsons-trailer.mp4 -r 3 -t 10 -loop 1 -vf "boxblur=5:1" simpsons-blur.gif                     

In this example, the GIF is created from the “input.mp4” video file and FFmpeg’s inbuilt boxblur filter is used to blur the video and create an interesting visual effect.

GIF from a video using FFmpeg with blur filter

Conclusion

I hope this tutorial helped you create a GIF from a video file. From adjusting parameters to applying filters and optimizing file size, FFmpeg offers a wealth of options to bring your ideas to life. Remember to experiment, explore, and have fun as you embark on your animated GIF creation journey. To learn more about FFmpeg, please check out the following tutorials.

Pallycon April NAB 2024

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.