FFmpeg to get Animated GIFs from videos

FFmpeg, a powerful multimedia processing tool, provides developers with the capability to create animated GIFs from videos. Animated GIFs are widely used for sharing short, looping animations on the web or in messaging applications. In this detailed guide, we will explore how to use FFmpeg to convert videos into animated GIFs, providing step-by-step instructions and customization options.

  1. Installing FFmpeg:
    Ensure that FFmpeg is installed on your system before proceeding. Download the latest version from the official website (https://ffmpeg.org/download.html) or use package managers like Homebrew on macOS or APT on Linux.
  2. Navigate to the Video File Directory:
    Open a terminal or command prompt and navigate to the directory containing the video file you want to convert to an animated GIF. Use the cd command to change the directory:
   cd /path/to/video/directory
  1. Execute FFmpeg Command:
    Use the following FFmpeg command to convert the video into an animated GIF:
   ffmpeg -i input_video.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif output.gif

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "fps=10,scale=320:-1:flags=lanczos": Sets the frame rate to 10 frames per second and scales the output to a width of 320 pixels, maintaining the aspect ratio using the Lanczos resampling algorithm.
  • -c:v gif: Specifies the GIF codec for the output.
  • output.gif: Defines the output filename.
  1. Adjusting Parameters:
  • Frame Rate (-vf fps): Modify the frame rate to control the speed of the animated GIF.
  • Output Size (-vf scale): Adjust the width and height to resize the output. The :-1 in the example maintains the aspect ratio.
  • Resampling Algorithm (-vf flags=lanczos): Choose a resampling algorithm based on your preference and image quality requirements.
  1. Executing the Command:
    Press Enter to execute the FFmpeg command. FFmpeg will process the video and create an animated GIF based on the specified parameters. The progress will be displayed in the terminal.
  2. Viewing the Output:
    Once the process is complete, navigate to the output directory where the animated GIF is saved. Open the generated GIF using an image viewer to preview the looping animation extracted from the video.
  3. Customization and Additional Options:
    Explore FFmpeg’s documentation for advanced options, such as adding text, adjusting colors, or specifying a range of frames. Customizing the command allows you to create animated GIFs with specific characteristics.
  4. Optimizing GIF Size:
    Animated GIFs can be large in size. To optimize the GIF size, consider using the optimize option:
ffmpeg -i input_video.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif -f gif - | gifsicle --optimize=3 --delay=10 > output.gif

This command uses the gifsicle tool to further optimize the GIF.

The video files are created from the frames that can be saved to the frames of an animated GIF, the image type that is frequently used on the web in a form of banners and short animations. Because the frames are saved uncompressed, it is useful only with shorter videos, otherwise the file size of animated GIF can be very large.

For example, to convert a short SWF file to the animated GIF to create an alternative for the users without a Flash plugin, we can use the command (the pixel format must be set to rgb24)

ffmpeg -i promotion.swf -pix_fmt rgb24 promotion.gif

The default resolution of the mptestsrc video source is 512×512 pixels, other listed sources have 320×240 pixels resolution. The most versatile is the color image source that is able to generate the image of any color and any size, for example to create a teal background for a leaderboard banner sized 728×90 pixels, we can use the command

ffmpeg -f lavfi -i color=c=#008080:s=728×90 leaderboard.jpg

Conclusion: Creating animated GIFs from videos using FFmpeg provides a versatile and efficient way to share short, looping animations. By following this comprehensive guide, you can use FFmpeg to process videos, customize the output, and create animated GIFs tailored to your specific requirements.

Leave a Comment