Convert GIF to MP4 with FFmpeg in Linux
Converting an animated GIF to an MP4 video file using FFmpeg in the Linux terminal is a highly efficient way to reduce file sizes and improve playback compatibility across modern web platforms. This guide provides a quick, straightforward walkthrough of the exact command needed for this conversion, explains how the specific command flags optimize the output video quality, and addresses common troubleshooting steps for handling irregular GIF dimensions.
The Standard Conversion Command
To convert your GIF to an MP4 video, open your Linux terminal, navigate to the directory containing your file, and run the following standard command:
ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p output.mp4
Breaking Down the Command Flags
Understanding what each part of the FFmpeg command does ensures you get a compatible and high-quality video output:
-i input.gif: Specifies the input file name and format.-movflags faststart: Optimizes the MP4 file structure so the video can begin playing instantly on web browsers before it is fully downloaded.-pix_fmt yuv420p: Changes the pixel format to YUV420p. This is the most critical flag for GIF-to-MP4 conversions, as standard MP4 players and browsers cannot read the default pixel formats generated from many GIFs.output.mp4: Defines the desired name for your newly created video file.
Handling Odd-Numbered Dimensions (Fixing Errors)
H.264 MP4 videos require width and height dimensions that are divisible by two. Because GIFs often have odd-numbered pixel dimensions, you might encounter a “height not divisible by 2” error.
To fix this automatically during conversion, use a video filter flag
(-vf) to scale the video to the nearest even number:
ffmpeg -i input.gif -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags faststart -pix_fmt yuv420p output.mp4
This modified command tells FFmpeg to look at the input width
(iw) and input height (ih), divide them by
two, truncate the remainder, and multiply by two, ensuring perfect
compatibility without visibly distorting the image.