Converting .gif to web-safe video formats using ffmpeg
By Sidney Liebrand on Feb 14, 2021•3 min readWhen recording animated content for my blog posts I use a gif recorder. However, using .webm or .mp4 files is usually more efficient and works in pretty much every major browser today.
Convert .gif to .webm
To convert a .gif file to .webm, use the following command:
ffmpeg -i file.gif -c vp9 -b:v 0 -crf 40 file.webm
This will output a .webm file that works in most browsers, but what are these options doing exactly? Let's dive in:
-i
: Specifies the input file, in this casefile.gif
.-c
: Specifies the codec,vp9
works in most browsers.-b:v
: Specifies the video bitrate,0
allows us to specify the quality via the-crf
option.-crf
: Specifies the quality, ranges between 0-63, lower means better quality.
More detailed explanations about the options can be found on the ffmpeg VP9 wiki.
These .webm files will suffer a little bit of quality loss, but if more quality is
needed the -crf
flag can be set to a lower value easily. With these settings
the .webm file is (on average) 60% smaller than the .gif file.
When we check caniuse, .webm support looks good too, mostly. Indeed the only real reason today to use .mp4 on the web is to support Internet Explorer.
Multimedia format designed to provide a royalty-free, high-quality open video compression format for use with HTML5 video. WebM supports the video codec VP8 and VP9.
updated May 16, 2024- 1Older browser versions did not support all codecs.
- 2Older Edge versions did not support progressive sources.
- 3Can be enabled in Internet Explorer and Safari for MacOS by manually installing the codecs in the operating system.
- 4Supports VP8 codec used in WebRTC.
- 5Supports VP9 codec used in WebRTC (off by default.)
- 6Supports VP9 WebM used in MediaSource on macOS Big Sur or later.
- 7Safari 14.1 – 15.6 has full support of WebM, but requires macOS 11.3 Big Sur or later.
Convert .gif to .mp4
To convert a .gif file to a web-safe .mp4, use the following command:
ffmpeg -i file.gif -movflags +faststart -pix_fmt yuv420p \ -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" file.mp4
Let's also cover the options for this one:
-i
: Specifies the input file, in this casefile.gif
.-movflags
: For web video we can specify+faststart
to allow the video to start playing before the download is complete.-pix_fmt
: The defaultyuv444p
can't be played by some mobile browsers so we set it toyuv420p
instead.-vf
: This flag is allows us to set"scale=trunc(iw/2)*2:trunc(ih/2)*2"
to ensure the video width and height are divisible by 2 which would otherwise cause an error when usingyuv420p
.
More detailed explanations about the options can be found on the ffmpeg H.264 wiki.
Additionally, this commit
provides some additional context about -pix_fmt
and -vf
.
The .mp4 file is (on average) 50% smaller than the .gif file. While less efficient than .webm, it is still much better than using .gif. Additionally, this .mp4 file is widely supported in nearly all browsers.
Commonly used video compression format.
updated May 16, 2024- Firefox supports H.264 on Windows 7 and later since version 21. Firefox supports H.264 on Linux since version 26 if the appropriate gstreamer plug-ins are installed.
- Partial support for older Firefox versions refers to the lack of support in OS X & some non-Android Linux platforms.
- 1The Android 2.3 browser requires specific handling to play videos.
- 2Partial support refers to the lack of hardware acceleration.
- Chrome has performance problems with long h.264 videos.
- Browsers have trouble with more than one audio track in a video (for multi-language support): IE 11 supports it, Firefox plays the last track, Chrome plays the first track.
- In Windows 7, the maximum supported resolution is 1920 × 1088 pixels for both software and DXVA decoding. MSDN.
Conclusion
Hopefully this post made it a little bit easier to understand how to convert
.gif files to .webm or .mp4 using ffmpeg
. It a powerful tool that can help us
optimize our usage of animated media on the web. It is definitely worth looking
into if you run a website that displays lots of gifs.
Until next time :)
👋