FFmpeg: How to Convert MKV To MP4

ffmpeg logo and mp and mkv icons on a dark blue background

In this article you will learn how to use FFmpeg to effortlessly convert mkv to mp4, additionally, you will be acquainted with re-encoding video and audio using different ffmpeg encoders.

We will discuss converting mkv files to mp4; there is a high probability that you might be in situations that require having mp4 files, or you may want to use systems that do not support the mkv container format. In these situations, it is imperative to convert the video file to another system-friendly file extension, such as the mp4 video format.

Basic FFmpeg commands consist of four elements:

ffmpeg [Input File] [Flags/Actions] [Output File]

How to Install FFmpeg

To install the FFmpeg project, we first go to our terminal and type the following update command to download package information from all configured sources:

sudo apt update

After making sure that the first command got executed correctly, Type the below-given command and hit Enter to install the project:

sudo apt install ffmpeg

After executing the preceding command. FFmpeg will get installed onto our system and also add the ffmpeg binary to the path variable. So now we can use the ffmpeg command in the command line.

One final step left is to verify that FFmpeg is actually installed. Let’s type ffmpeg to check the FFmpeg configuration.

ffmpeg
Output
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
Note: Another way to display the version of FFmpeg is by adding the -version command line switch.

Getting Started with FFmpeg

For this tutorial, we are going to work on a short clip from an old cartoon series. Let’s start by downloading the Matroska media file (link) by following the bellow given command:

wget https://archive.org/download/barney-bear-1939-mkv/Barney%20Bear%20-%20E22%20-%20Wee-Willie%20Widlcat%20%28June%2020%2C%201953%29.mkv
Output
--2022-06-19 16:22:24--  https://archive.org/download/barney-bear-1939-mkv/Barney%20Bear%20-%20E22%20-%20Wee-Willie%20Widlcat%20%28June%2020%2C%201953%29.mkv
Resolving archive.org (archive.org)... 207.241.224.2
Connecting to archive.org (archive.org)|207.241.224.2|:443... connected.
HTTP request sent, awaiting response... 302 Found
. . .
Connecting to ia801904.us.archive.org (ia801904.us.archive.org)|207.241.228.104|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 69396820 (66M) [application/octet-stream]
Saving to: 'Barney Bear - E22 - Wee-Willie Widlcat (June 20, 1953).mkv'

Change the file name to Bbear.mkv . Check the below command:

mv 'Barney Bear - E22 - Wee-Willie Widlcat (June 20, 1953).mkv' 'Bbear.mkv'
ls
Output
audio2.mp3 audio.aac audio.mp3 Bbear.mkv video.mp4

Checking the Media Streams and Information

It’s always recommended to check and familiarize ourselves with basic metadata about video files and also the containing streams. We can achieve this by using the -i command line argument without specifying an output.

ffmpeg -i Bbear.mkv
Output
Input #0, matroska,webm, from 'Bbear.mkv':
. . .
  Metadata:
    CREATION_TIME   :
    ENCODER         : Lavf55.12.0
  Duration: 00:06:31.47, start: 0.083000, bitrate: 1418 kb/s
  Stream #0:0: Video: h264 (Main), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 686x480, SAR 1:1 DAR 343:240, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
  Stream #0:1(und): Audio: ac3, 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      title           : Stereo
      LANGUAGE        : und
  Stream #0:2(und): Subtitle: ass

Note: We can achieve similar results by using ffprobe which is one of FFmpeg main executable, this command is used for getting the characteristics of media files containers and streams. Follow the bellow command:

ffprobe Bbear.mkv

After we’ve checked the media file information, notice the different streams, Video: matroska, Audio: ac3, and also the subtitle stream Subtitle: ass.

Note: As with any command line interface, the user should specify the path of input or output files depending on their working directories. In the examples given in this article, it is assumed that the user has navigated to the working directory to execute the FFmpeg commands.

Converting MKV to MP4

With ffmpeg we can choose between two different ways to convert media files. The first and easy one is to copy streams into the desired container, and the second one is to re-encode those streams in order to change their quality or size…

Converting MKV to MP4 Without Re-Encoding

To convert mkv to mp4 is actually quite easy; we can use ffmpeg command to copy the original video and audio streams into a new container. This simple method can save a lot of time since we are not encoding the media file, and the conversion process will be fast. Additionally, the video and audio quality will not be reduced. In order to achieve this, we will use the the -c copy ffmpeg option just as so:

ffmpeg -i Bbear.mkv -c copy Bbear_wout.mp4

You can check the streams of our new MPEG-4 file; you’ll notice that nothing changed – except for the container.

Another way to convert (copy streams into a new container) is by specifying the streams you want to be copied into the new container, to do that we will use the -c:v copy and -c:a copy options to replicate only the video and audio into the .mp4 file. Let’s check the following command:

ffmpeg -i Bbear.mkv -c:v copy -c:a copy Bbear_vid-aud.mp4

After displaying the new media file information, you’ll notice that the subtitle stream does not exist. (To copy the subtitle stream use the -c:s copy option)

Converting MKV to MP4 With Re-Encoding

FFmpeg is able to encode a wide variety of video and audio formats. The -c command line switch let us specify our favorite encoder. As you might have noticed, we can use the -c command option to set a codec for each stream; instead of the copy parameter. Follow the below-mentioned command for better understanding.

ffmpeg -i Bbear.mkv -c:v copy -c:a libvorbis Bbear_encode-AUD.mp4

In the preceding command, we encoded the audio stream by choosing the libvorbis encoder. However, we copied the video into the new container.

How about we set an encoder for our video stream as well? Let’s do that by changing the copy from the previous command to mpeg4, just as so:

ffmpeg -i Bbear.mkv -c:v mpeg4 -c:a libvorbis Bbear_ENCODE_vid-aud.mp4

Note: In case you are converting to mp4, these are some of the favorites codecs that you can use (mpeg4 , libx264 , libvpx-vp9 and libxvid ); on the other hand, you can choose from the following audio codecs (libfaac , libfdk_aac , libvorbis , libmp3lame, and acc).

We can also check with the -encoders flag all available encoders supported by the ffmpeg command. Check the following command:

ffmpeg -encoders
Encoders:
 V..... = Video
 A..... = Audio
 S..... = Subtitle
 .F.... = Frame-level multithreading
 ..S... = Slice-level multithreading
 ...X.. = Codec is experimental
 ....B. = Supports draw_horiz_band

Conclusion

In this article, we’ve walked you through the different ways of converting mkv to mp4, and also we showed you how to use ffmpeg‘s video and audio encoders to reduce size and control quality. We hope these commands may come in handy for you.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
You May Also Like
Bash Source Command
Read More

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current…