How to convert GIF to MP4 in terminal

Learn how to convert GIF to MP4 using FFmpeg from the command line.
Published 2023-03-23 5 min read
How to convert GIF to MP4 in terminal

Have you ever come across a fantastic GIF that you wanted to convert into an MP4 file using just your terminal? You’re in the right place! In this concise and straightforward guide, we will walk you through the process of converting GIFs into MP4 videos using simple command line techniques.

TL;DR

ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4

Why use MP4 instead of GIF?

Because GIF weights a lot. Let’s take the example of the famous meme. I’ve downloaded one copy of it and it was 707kB. Over 700kB for a 200x234 GIF! After conversion to mp4 it was only 41kB. Since GIF was already compressed heavily the result look just as good as the original:

What you’ll need to convert GIF to MP4?

To make the conversion happen you’ll need a GIF image and FFmpeg.

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

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 MP4 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 in this example. You should also check out a guide on converting GIF to WebM.

It is possible to create mp4 with a very simple command:

ffmpeg -i input.gif output.mp4

But it won’t open in some players, like QuickTime on MacOS. That’s why it’s safer to use more complicated command:

ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4

What happened here?

-i input.gif - this indicates the input file

-movflags faststart - this is a pretty broad topic. tl;dr - you need this for HTTP pseudo-streaming. A slightly longer version is that mp4 files contain metadata on things like video resolution or framerate. This metadata is needed for playback, but by default, it’s located at the end of a video. That’s why it has to be moved to the front if playback would start before downloading the whole clip.

-pix_fmt yuv420p it sets YUV420 color space for the output. It is suitable for web because it needs less data to represent the image, making to output lighter.

-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" is more complicated:

-vf sets video filters. The format is one of them. It changes the chroma subsampling format, works well, and produces slightly smaller files than the default. scale changes the resolution, for some reason if the height of a GIF is odd, the FFmpeg throws “height not divisible by 2” error, so we are scaling the video, so it would have an even width and height. iw, ih are input stream width and height, we divide them by 2 and trunc rounds them down. At last, we multiply it by 2 so in the worst-case scenario resolution of the MP4 will be one pixel smaller than the input.

Additional Options for GIF to MP4 conversion

When converting GIFs to MP4 files using FFmpeg, you have a variety of options to tailor the output to your specific needs. Here are some additional parameters and customizations you can apply:

  • Bitrate Adjustment: You can control the video’s bitrate to manage the quality and size of the output file. Use the -b:v flag to set the bitrate. For example, -b:v 1M sets the bitrate to 1 Mbps. Higher bitrates generally mean better quality but larger file sizes.
ffmpeg -i input.gif -b:v 1M output.mp4
  • Compression Level: For further size reduction, you can adjust the compression level using the -crf option. The CRF (Constant Rate Factor) ranges from 0 (lossless) to 63 (lowest quality). A good balance for quality and compression is usually between 18 and 24, but while converting gifs we can push it more, even 40 provides a decent quality in most cases.
ffmpeg -i input.gif -crf 20 output.mp4
  • Resizing: To resize the GIF, use the -vf "scale=w:h" option. Set w and h to the desired width and height. You can set one of these to -1 to keep the aspect ratio.
ffmpeg -i input.gif -vf "scale=320:-1" output.mp4

By utilizing these additional options and customizations, you can have more control over the final output of your MP4 file, ensuring it meets your specific requirements for quality, size, and format.

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

#cli