In this article we will show you how to convert any video supported by FFMPEG to a video file in MKV format using FFMPEG command-line tool.
MKV format is the extension file used for the Matroska Multimedia Container. This is a file format which can hold an unlimited number of video, audio, picture, or subtitle tracks or streams in one file. It is similar in its design to other container types such as MP4 or ASF but it is based in an open specification.
MKV supports many video encoded streams such as H.264, or VP8 encoded video, or AAC, or VORBIS audio streams, which are some of the most common supported stream types. The user playing one MKV file must have a player that supports the encoded streams in order to view the file properly, so if the player supports only the audio stream, the user could only hear the audio but he won't see the video.
We recommend using H.264 or VP8 for video streams and AAC or VORBIS for audio as they are widely supported. However, other encoders can be used. We will show you some of these in this article.
The WEBM format is a profile of MKV files, so when you are converting a video to WEBM format, or you are storing in WEBM format, you are actually storing in MKV format. The difference is that WEBM is a subset of MKV configuration using VP8 encoding for video and VORBIS for audio streams. (VP9 video and OPUS audio have been introduced last years). Read more about how to convert a video file to a webm file using FFMPEG in this article.
So, for converting a video to MKV format you can use the basic FFMPEG command:
ffmpeg -i input_video.avi output_video.mkv
By default this command will produce an output video file using libx264 library (which produces H.264 video stream) and libvorbis library (which produces VORBIS audio stream). You can use the command specifying this options (which will produce the same result):
ffmpeg -i input_video.avi -f matroska -vcodec libx264 -acodec libvorbis output_video.mkv
In which we are using the parameters:
- -f matroska which specifies that the output file will have Matroska format
- -vcodec libx264 we're telling here to use the libx264 library which encodes video in H.264 coded streams. As we have said before you can use different video encoders. You can use other codecs changing libx264 for 'vp8' if you want to use VP8 encoding through libvpx library or 'vp9' if you want to use VP9 encoding. You can use more video encoders but the most widely supported codecs are H.264 and VP8.
- -acodec libvorbis which specifies to use the libvorbis library which encodes audio using VORBIS codec. You can chose another audio codec. Use 'aac' for AAC encoding or 'opus' for OPUS encoding.
Use the following command to convert an input video file into a MKV file using VP8 encoding with VORBIS audio encoding:
ffmpeg -i input_video.avi -f matroska -vcodec vp8 -acodec libvorbis output_video.mkv
The following will convert a video into a video using VP9 for video encoding and OPUS as audio encoding:
ffmpeg -i input_video.avi -f matroska -vcodec vp9 -acodec aac output_video.mkv
The next command will convert a video into a MKV file using MPEG4 video and AAC audio encoding:
ffmpeg -i input_video.avi -f matroska -vcodec mpeg4 -acodec aac output_video.mkv
Recommendations and Other considerations
Remember that you can perform another transformations as changing resolution or the frame rate to the video, when converting the video to a new format. For example use the next command to convert a video to a VP8/VORBIS video changing video resolution to 640x360px. (See more on changing video resolution using FFMPEG)
ffmpeg -i input_file.avi -f matroska -vcodec vp8 -vf scale=640:360 -acodec libvorbis output_file.mkv
Remember that some encoders have different base configurations, as VP8 will use some low quality configuration, or libx264 will use some basic configurations that you can change. Read more about libx264 configuration options in our article on how to convert a video to MP4 format.
Remember also that some encoders could be very slow when converting big video files.
Did you find this article helpful?