How to Convert mkv to mp4 Using FFmpeg?

FFmpeg is the most widely used video conversion and compression software used for multimedia files data conversion. The FFmpeg can perform the data conversion losslessly, which helps maintain the video quality during conversion. This guide will elaborate on the mkv to mp4 conversion with the help of FFmpeg.

Let’s discuss the methods to convert mkv to mp4 with this timeline:

Let’s move to the pre-requisite.

Pre-Requisite: Install FFmpeg on Linux

The ffmpeg open-source software can be used to convert the mkv file format to mp4. In this section, the conversion of the mkv file to mp4 is performed using several examples. Let’s start with the installation of the ffmpeg.

The FFmpeg is not built-in for Linux distros. To install the FFmpeg in different distributions, use the following commands:

$ sudo apt install ffmpeg                          #In Debian/Ubuntu-based.
$ sudo yum install ffmpeg ffmpeg-devel             #In RHEL-based.
$ sudo dnf install ffmpeg ffmpeg-devel             #In Fedora-based.
$ sudo pacman -S ffmpeg                            #In Arch-based.

Let’s dig into the methods to convert mkv to mp4.

How to Convert mkv to mp4 Using FFmpeg?

Before getting into the examples, let’s have a look at the syntax of the FFmpeg which is described below:

Syntax:

$ ffmpeg -i <input-filename> <options> <output-filename>

The components of syntax are described below:

  • FFmpeg: Shows the FFmpeg software.
  • i: Represents the input file.
  • input -filename: Replace with the mkv file name that needs to be converted.
  • output-filename: Replace with the converted mp4 file name.

For more details on the FFmpeg command, use the command stated below:

$ ffmpeg --help

There are several parameters on that basis we can convert the “mkv” file to “mp4” file. The FFmpeg allows the users to control the codec (audio, video, subtitles, etc.), dimension, frame rate, and other properties. In this section, we will discuss different methods of converting mkv files to mp4 using different FFmpeg options.

Example 1: Convert a Single mkv File to mp4

The FFmpeg “i” option allows the users to input the file to convert it to another type of media file. To convert the “mkv” file to an “mp4” file losslessly, use any of the below commands:

$ ffmpeg -i Samplevideo.mkv Samplevideo.mp4 $ ffmpeg -i example.mkv -c copy example.mp4

It will take a few seconds to convert mkv to mp4:

The error-free output shows the mkv file is successfully converted to the mp4 file.

Example 2: Convert Multiple mkv Files to mp4

Several mkv files can be mp4 files in FFmpeg using a single command. To convert all the “mkv” files to mp4 in the current directory, use the below command in the terminal:

$ for f in *.mkv; do ffmpeg -i "$f" -c copy "${f%.mkv}.mp4"; done

All the files in that directory will be converted to the “mp4” files.

To verify the conversion of “mkv” files to “mp4” files, the “ls” command is used:

$ ls

The output shows the three “mkv” files present in that directory are converted to “mp4” files.

Example 3: Convert mkv to mp4 with Hiding the Details

While converting the mkv file to mp4 files, the output shows details of the file like copyrights, libraries, etc., to hide that details in the output, use the “-hide_banner” option.  For instance, to convert a “Samplevideo.mkv” file to a “Samplevideo.mp4” file hiding the details about the file, use this command:

$ ffmpeg -hide_banner -i Samplevideo.mkv Samplevideo.mp4

Example 4: Convert mkv File to mp4 with Specific Codecs

There are several codecs in FFmpeg which include, audio, video, mux, and others that can be managed using the FFmpeg. For instance, to copy (c) the video (v) of “Samplevideo” using the libx264 codec, use the following code:

$ ffmpeg -i Samplevideo.mkv -c:v libx264 Samplevideo.mp4

The output shows the mp4 video conversion codec is “libx264”.

Example 5: Convert mkv File to mp4 with Specific Dimensions

The FFmpeg provides a size “s” option, which sets the dimension for the video conversion. To convert the “Samplevideo” from mkv to mp4 with the “1280×720” dimension, execute this code:

$ ffmpeg -hide_banner -i Samplevideo.mkv -c:a copy -c:v libx264 -s 1280x720 Samplevideo.mp4

Example 6: Convert mkv File to mp4 with Specific Frame Rate

The “r” option of FFmpeg represents the frame rate size. The frame rate size displays the consecutive picture after a certain time which affects the video quality, which can be set using FFmpeg. To set the frame rate of a “Samplevideo” mkv to p4 conversion rate to “30”, use the following command:

$ ffmpeg -hide_banner -i Samplevideo.mkv -c:a copy -c:v libx264 -r 30 Samplevideo.mp4

Example 7: Convert mkv File to mp4 With Specific Bitrate

The “b” option allows us to control the bitrate for video conversion. The bit rate shows the data processed per second, which can be managed using the FFmpeg. To control the bit rate to “1M” per second for converting mkv “Samplevideo” to mp4, use this command:

$ ffmpeg -hide_banner -i Samplevideo.mkv -c:a copy -c:v libx264 -b:v 1M Samplevideo.mp4

Example 8: Convert mkv to MP4 With Audio, Video and Subtitles

The advanced option “map” allows the user to convert the process the conversion of audio, video, and subtitles codecs. To convert mkv “Samplevideo” to mp4 with all the codecs, use the below-written command:

$ ffmpeg -hide_banner -i Samplevideo.mkv -map 0 Samplevideo.mp4

Additional Tip: How to Convert mp4 to mkv?

The FFmpeg can convert the “mp4” files to “mkv” as well. For example, to convert the mp4 “Samplevideo” file to mkv, execute the following command:

$ ffmpeg -i Samplevideo.mp4 -c copy Samplevideo.mkv

That’s all from this post!

Conclusion

The FFmpeg is a multimedia data conversion tool that can be utilized to convert mkv to mp4 format using customized settings. The FFmpeg allows the users to control the codes, dimensions, and frame rate while converting “mkv” to mp4 format. This guide has provided detailed guide on converting the mkv files to mp4 using FFmpeg.