Convert videos to MP4 format using ffmpeg

In this article we will show you how to convert any video to MP4 format using H264 video codec.
This is one of the best ways to convert videos to MP4 format in Linux systems, and is also an excellent way to convert videos to MP4 format under Windows or Mac systems.

In this article we are going to convert video files into MP4 format. The MP4 format is a container file format itself so it can contain various stream formats inside, but we will be focusing on H264 video as it is one of the most accepted formats. It is an excellent video format with high quality video in reduced space which can be processed very fast.

We will be using ffmpeg as the program converting the video files.

If we have a video file example.mov and want to convert it to MP4 format with H264 video we can use the following command:

ffmpeg -i example.mov example.mp4 -hide_banner

In this example ffmpeg detects that the output file has MP4 extension so by default it will convert the video into MP4 file with H264 for the video and aac for the audio. It will convert the video file using default settings.

Some options in video conversion to MP4 file

Now we are doing the same conversion than before but using some options that we will explain:

ffmpeg -i example.mov -f mp4 -vcodec libx264 -preset fast -profile:v main -acodec aac example.mp4 -hide_banner

In this conversion we are using some parameters:

  • -f mp4 with this parameter we are specifying that the output file will have mp4 format.
  • -vcodec libx264 with this parameter we are specifying the video codec that we want to use. In this case we are using libx264 which is a H264 video encoder. (we can use the parameter -c:v libx264 instead)
  • -preset fast with this parameter we are specifying the encoder (libx264) some options for the encoding process. We can use any of the different presets that exists (ultrafast, superfast, veryfast, faster, fast, medium, slow, slower or veryslow from the fastest to the slowest). Slower presets will produce better quality video in less space but it will take more time to process.
  • -profile:v main this parameter is similar to preset. The profile parameter indicates the encoder if it can use some advanced options to improve the video compression. The downside is that using these advanced options can make the video unplayable on some old devices. We are using the main profile as it is very well supported but we can use one of the following: baseline, main, high, high10, high422, and high444
  • -acodec aac with this parameter we are specifying the audio encoder to use, in this case we are using audio aac codec.
  • -hide_banner will hide some compilation information about ffmpeg.

Some other options

We have seen some basic parameters but we also should know some other important parameters such as:

  • crf the constant rate factor is a value between 0 and 51. It indicates the encoder to attempt to achieve a certain output quality. The value 0 indicates lossless and 51 is the worst possible, with 23 as the default value. A lower value indicates higher quality.
  • bitrate we can indicate the encoder a bitrate so the encoder will try to achieve it. We can indicate an average bit rate, a minimum bitrate or a maximum bitrate.
  • faststart this is a must when you are going to use your video file online. This will move some metadata information to the beginning of the file and allow the video to begin playing before it is completely downloaded.

Conclusion and some recommendations

The MP4 format is one of the most popular video formats today as it is used in some of the most important Internet sites (such as YouTube) because of its widespread support and excellent quality. It is one of the formats that I should recommend without doubt to store videos or serve them over Internet (the other format is webm with vp8 encoder -see how to convert videos into webm format-)

  • The default parameters should work for most people. They produce excellent results.
  • The encoding process can take some time, especially if the video is long. So it can be useful trying to encode a little part of the video and check the output quality before going with the full video. See how to extract a part from a video here.
  • You can use some filters before the encoding, so the video can be converted as you want. For example, you can resize a video and encode it to mp4 format with this command.
    ffmpeg -i example.mov -vf scale=320:180 -c:v libx264 -preset fast -c:a aac output.mp4 -hide_banner

    (In this example the input video is resized to 320x180px before encoding process) As you can see in this example it is only needed to include the scale video filter to resize the video. More about how to resize a video using ffmpeg.

The encoding process has more advanced options. We have included in this article what we think are the most important and useful options, but if you want to read more about this conversion you can check the ffmpeg h.264 encoding gude.

    Did you find this article helpful?