FFmpeg Command to Output MKV Video

This article provides a quick and direct guide on how to use FFmpeg to output video files in the Matroska (MKV) container. You will learn the basic conversion command, how to copy video and audio streams without losing quality, and how to re-encode your files specifically for the MKV format.

The Basic MKV Conversion Command

To convert any video file into the MKV format using FFmpeg's default settings, use the following basic command structure:

ffmpeg -i input.mp4 output.mkv

In this command:

  • -i input.mp4 specifies your source input video file (this can be MP4, AVI, MOV, or any other supported format).
  • output.mkv is the name of the desired output file. FFmpeg automatically detects the .mkv extension and packages the output into a Matroska container.

Remuxing to MKV Without Re-encoding (Fastest)

If you want to change the container to MKV without wasting time or losing video quality, you can copy the existing video and audio streams directly. This process is called remuxing.

ffmpeg -i input.mp4 -c copy output.mkv
  • -c copy tells FFmpeg to stream-copy both the video and audio tracks. Because no re-encoding takes place, this process is incredibly fast and preserves 100% of the original quality.

Encoding Video and Audio for MKV

If you need to compress the video or ensure compatibility with specific codecs inside the MKV container, you can manually define the video and audio encoders. MKV is highly versatile and supports almost any codec, including H.264, H.265 (HEVC), VP9, and AV1.

To encode a video to H.264 with AAC audio inside an MKV container, use:

ffmpeg -i input.mov -c:v libx264 -crf 23 -c:a aac -b:a 192k output.mkv
  • -c:v libx264 encodes the video track using the H.264 codec.
  • -crf 23 sets the Constant Rate Factor (quality level, where lower is better quality; 18-28 is standard).
  • -c:a aac encodes the audio track using the AAC encoder.
  • -b:a 192k sets the audio bitrate to 192 kbps.