Best Command-Line Tools for Editing MKV Files
Manipulating MKV (Matroska) video files directly from the terminal offers unmatched speed, automation capabilities, and resource efficiency. This article highlights the best command-line tools for editing MKV files, detailing how you can use them to remux, crop, trim, split, merge, and edit metadata without relying on a graphical user interface.
1. MKVToolNix CLI (mkvmerge, mkvpropedit, mkvextract)
MKVToolNix is the official, definitive suite of tools designed specifically for the Matroska container format. The command-line utilities included in this suite are incredibly fast because they perform operations losslessly, without re-encoding the video or audio streams.
mkvmerge
Used for multiplexing (merging) multiple streams into a single MKV file, splitting files, or joining multiple MKV files together.
To merge video, audio, and subtitle files:
mkvmerge -o output.mkv video.mp4 audio.m4a subtitles.srtTo split a large MKV file by size (e.g., into 1GB chunks):
mkvmerge -o output.mkv --split size:1G input.mkvTo join/concatenate two MKV files:
mkvmerge -o combined.mkv file1.mkv + file2.mkv
mkvpropedit
An incredibly fast tool that allows you to analyze and modify properties of an existing MKV file (such as track names, languages, default flags, or titles) instantly without remuxing the entire file.
To change the language flag of the first audio track to English:
mkvpropedit input.mkv --edit track:a1 --set language=engTo set a subtitle track as the default:
mkvpropedit input.mkv --edit track:s1 --set flag-default=1
mkvextract
Used to extract specific tracks, chapters, or attachments from an MKV file.
To extract a subtitle track (assuming track ID 2):
mkvextract input.mkv tracks 2:subtitles.srt
2. FFmpeg
FFmpeg is the “Swiss Army knife” of command-line multimedia processing. While not exclusive to MKV, it is highly compatible with the Matroska format and is the best choice if you need to transcode, compress, or perform complex editing tasks like cropping, scaling, and audio volume adjustments.
Trimming/Cutting without Re-encoding
You can cut a segment out of an MKV file losslessly using the stream
copy (-c copy) function. This takes only a few seconds.
To cut from 00:01:30 to 00:03:00:
ffmpeg -i input.mkv -ss 00:01:30 -to 00:03:00 -c copy output.mkv
Cropping or Re-encoding Video
If you need to crop the video frame or change the video format, FFmpeg will re-encode the stream.
To crop a video to a 1920x1080 area starting 10 pixels from the left and 20 from the top:
ffmpeg -i input.mkv -vf "crop=1920:1080:10:20" -c:a copy output.mkv
Extracting or Removing Audio
To strip all audio from an MKV file, keeping only the video:
ffmpeg -i input.mkv -an -c:v copy output.mkv
Which Tool Should You Choose?
- Choose MKVToolNix CLI if you are working strictly with the MKV container and want to merge, split, extract tracks, or edit metadata. It does not re-encode files, making it incredibly fast and safe from quality loss.
- Choose FFmpeg if you need to edit the actual content of the video (e.g., cropping, compressing, filtering, or changing codecs) or if you are working with multiple file formats.