How to convert GIF to WebM in terminal

Learn why and how to convert GIF to WebM using FFmpeg from the command line.
Published 2023-03-26 4 min read
How to convert GIF to WebM in terminal

Have you ever encountered a fantastic GIF, but it’s a chunky boy? The best way to make it smaller is to convert it to WebM, and you can do it from the terminal. In this concise and straightforward guide, we will walk you through the process of converting GIFs into WebM videos using simple command line techniques.

TL;DR:

ffmpeg -i input.gif -c vp9 -b:v 0 -crf 40 output.webm

Why convert GIF to WebM?

GIF is an old graphics format that allows for animation, which was created when the world was different. The final specification was published in 1989. It was fine for very simple bitmap graphics with a limited color palette, but it’s unsuitable for more complex animations, as file size becomes huge in such cases. Unfortunately for a long time, before we had HTML5 video it was the only option for the simple embedding of animated graphics. The custom of presenting animations as GIFs stuck around for too long.

WebM is a web-native video format, so it works great within this context. It provides output files that are light (in terms of file size) and easy to use. It’s a relatively new format, so older browsers don’t support it (eg. IE11 does not support WebM), so in many cases, mp4 fallback would be suitable. We’ve already covered how to convert GIF to mp4.

 

What you’ll need to convert GIF to WebM?

A GIF image and FFmpeg.

FFmpeg is an open-source project that consists of a vast collection of libraries for handling multimedia files. It allows users to convert, record, stream, and process audio and video files in various formats with ease.

To install it on Linux use, depending on the distro, this command:

apt install ffmpeg

or:

yum install ffmpeg

on Mac:

brew install ffmpeg

Convert GIF to WebM with FFmpeg

I’m going to assume that you want to convert your GIF in order to place it on a website. It’s an important assumption since it dictates the options used.

GIF to WebM basic example:

ffmpeg -i input.gif -c vp9 output.webm

This basically tells FFmpeg to convert GIF to WebM with default settings. It works fine, but we’ll go through two more advanced options, that allow for more control and better compression, usually without losing on perceived quality.

GIF to WebM constant quality example:

ffmpeg -i input.gif -c vp9 -b:v 0 -crf 40 output.webm

What happened here?

-i input.gif - this indicates the input file

-c vp9 - it specifies codec to vp9, which is essential to the WebM format, this is mandatory

-b:v 0 - if this value is set to anything other than 0 it would set the average bit rate and switch FFmpeg to constrained quality mode. By setting it to 0 it tells FFmpeg not to worry about bitrate and to focus on the next parameter.

-crf 40 - sets expected quality level. Values range from 0 to 63, 0 being the highest quality, and 63 being the lowest. Recommended values are between 15 and 35, but to be honest, in the case of converting GIFs we can safely add 10 to the base value - it would sharply decrease the final file size and GIF quality is low anyway, that’s why we’ve set it to 40. Watch the output WebM to decide if the quality is appropriate for your case.

By the way, Google recommends the following crf values based on frame heights:

Frame HeightTarget Quality (CQ), crf parameter
24037
36036
48034 (LQ) or 33 (MQ)
72032
108031
144024
216015

Thanks to converting GIF to WebM your files will be much lighter and thanks to HTML5 video users won’t notice it’s not an actual GIF.

And there you have it! By following this simple guide, you’ve now learned how to convert your favorite GIFs into lightweight WebM files using FFmpeg. It’s time to put GIF usage on the web to well-earned rest. Now, we recommend an article on replacing GIFs with HTML5 video.

#cli