How to use the command 'mkvmerge' (with examples)

How to use the command 'mkvmerge' (with examples)

The mkvmerge command is a part of the MKVToolNix suite, a collection of tools designed to work with Matroska files. Matroska is a versatile multimedia container format, similar to AVI, MP4, or ASF, which supports a large variety of video, audio, subtitle tracks, and more, within a single file. Mkvmerge focuses on merging and extracting multimedia streams from MKV files. It is a powerful tool for anyone dealing with digital video content who needs to customize or manipulate multimedia files, whether for personal entertainment consumption or professional editing.

Use case 1: Display information about a Matroska file

Code:

mkvmerge --identify path/to/file.mkv

Motivation:

Before making any modifications to a multimedia file, it’s often essential to know the kind of content encapsulated within it. Displaying information about a Matroska file helps users understand the structure of the file, such as the number of tracks, the types of each track (video, audio, or subtitle), and the codecs used. This information is crucial for users who need to manipulate or extract specific streams.

Explanation:

  • mkvmerge: The command-line tool used for merging and handling MKV files.
  • --identify: This option tells mkvmerge to scan the given file and display the details of its contents.
  • path/to/file.mkv: The path to the Matroska file that you want to analyze.

Example Output:

File 'path/to/file.mkv': container: Matroska
Track ID 0: video (V_MPEG4/ISO/AVC)
Track ID 1: audio (A_AAC)
Track ID 2: subtitles (S_TEXT/UTF8)

Use case 2: Extract the audio from track 1 of a specific file

Code:

mkvextract tracks path/to/file.mkv 1:path/to/output.webm

Motivation:

Sometimes, you may want to extract an audio stream from a video file for purposes such as creating a podcast or using the audio in another project. This can be especially useful for remixes or when someone needs only the audio content for listening purposes.

Explanation:

  • mkvextract: A separate tool, often bundled with mkvmerge, specifically used for extracting tracks from Matroska files.
  • tracks: A sub-command that specifies which tracks to extract.
  • path/to/file.mkv: The path to the Matroska file from which you want to extract the audio.
  • 1:path/to/output.webm: Indicates track number 1 (typically an audio track) and designates the output location and format for the extracted file.

Example Output:

After running the command, the audio from the specified track is saved to path/to/output.webm. The command-line interface does not display additional output beyond confirming successful extraction, unless errors are encountered.

Use case 3: Extract the subtitle from track 3 of a specific file

Code:

mkvextract tracks path/to/file.mkv 3:path/to/subs.srt

Motivation:

Extracting subtitles is a common task for users who want to use or modify the text without affecting the original video. This can be extremely helpful for translating the subtitles into other languages or for archiving purposes.

Explanation:

  • mkvextract: The tool used to extract specific elements from an MKV file.
  • tracks: Specifies that a particular track will be extracted.
  • path/to/file.mkv: The source Matroska file.
  • 3:path/to/subs.srt: Indicates the track number (here, track number 3, which is generally a subtitle track) and specifies the target location and file format for the extracted subtitle file.

Example Output:

The command extracts the subtitle from track 3 and saves it to path/to/subs.srt. As with audio extraction, the primary output is the successful creation of the file, with minimal interface output unless any issues occur.

Use case 4: Add a subtitle track to a file

Code:

mkvmerge --output path/to/output.mkv path/to/file.mkv path/to/subs.srt

Motivation:

Adding subtitles to a Matroska file increases accessibility for individuals who are deaf or hard of hearing, as well as for viewers who speak different languages. It is often necessary for creators who want to distribute their content to a broader audience.

Explanation:

  • mkvmerge: The command-line tool used for combining MKV-related files.
  • --output: Specifies the name and path of the resulting file.
  • path/to/output.mkv: The file where the newly merged content will be saved.
  • path/to/file.mkv: The original Matroska file to which the subtitles will be added.
  • path/to/subs.srt: The subtitle file in SRT format that is to be included in the Matroska file.

Example Output:

Upon completion, a new Matroska file, path/to/output.mkv, is created with the subtitle track added. This operation typically completes with a confirmation message about the merge process, although detailed command-line output might not be essential in most scenarios.

Conclusion:

The mkvmerge and mkvextract tools provide robust functionality for handling Matroska multimedia files. Whether identifying file contents, extracting audio or subtitles, or merging new subtitles into a file, these command-line utilities offer users the ability to finely control and customize their multimedia content with efficiency and precision. By mastering these commands, users can seamlessly manipulate video and audio files to suit a wide variety of personal or professional needs.

Related Posts

How to use the command 'idnits' (with examples)

How to use the command 'idnits' (with examples)

The ‘idnits’ tool is a valuable command-line utility designed primarily for authors and reviewers of Internet-Drafts (I-Ds) within the Internet Engineering Task Force (IETF) framework.

Read More
How to Use the Command 'pamshadedrelief' (with Examples)

How to Use the Command 'pamshadedrelief' (with Examples)

pamshadedrelief is a command-line utility part of the Netpbm suite of graphics tools, designed to generate shaded relief images from elevation maps.

Read More
How to use the command 'kubectl logs' (with examples)

How to use the command 'kubectl logs' (with examples)

In Kubernetes, pods are the smallest deployable units that can be created and managed.

Read More