How to Convert Video to MP4 Using FFmpeg
This article provides a straightforward guide on how to write an FFmpeg command to export videos in the MP4 container format. You will learn the basic command syntax, how to specify the most compatible video and audio codecs, and how to optimize your output file for web playback and quality.
The Basic Command
To convert any input video into an MP4 file using FFmpeg’s default settings, use the following basic command structure:
ffmpeg -i input.mov output.mp4In this command: * -i input.mov specifies the path to
your source video file. * output.mp4 is the desired name
and format of your output video.
Ensuring Maximum Compatibility (H.264 and AAC)
While the basic command works, specifying the video and audio codecs ensures that your MP4 file will play seamlessly on almost all modern devices, web browsers, and media players. The standard combination for MP4 is the H.264 video codec and the AAC audio codec.
Run this command to force these codecs:
ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4-c:v libx264instructs FFmpeg to encode the video stream using the H.264 encoder.-c:a aacinstructs FFmpeg to encode the audio stream using the AAC encoder.
Adjusting Video Quality and File Size
You can control the quality and size of your output MP4 using the Constant Rate Factor (CRF). The CRF scale runs from 0 to 51, where lower values mean higher quality (and larger file sizes), and higher values mean lower quality. A value between 18 and 28 is recommended, with 23 being the default.
To set the quality, use the -crf flag:
ffmpeg -i input.mov -c:v libx264 -crf 23 -c:a aac output.mp4Optimizing MP4 for Web Playback
If you plan to upload the MP4 video to the web, you should optimize
the file structure so the video can start playing before it is fully
downloaded. You can achieve this by moving the index information (moov
atom) to the beginning of the file using the
-movflags +faststart flag:
ffmpeg -i input.mov -c:v libx264 -c:a aac -movflags +faststart output.mp4