MKVMERGE(1)

NAME
mkvmerge - Merge multimedia streams into a Matroska file

SYNOPSIS
mkvmerge [global options] -o out [options1] <file1> [[options2] <file2> ...] [@optionsfile]

DESCRIPTION

This program takes the input from several media files and joins their streams (all of them or just a selection) into a Matroska file. <http://www.matroska.org/> (http://www.matroska.org/)

Global options:

-v, --verbose
    Increase verbosity.

-q, --quiet
    Suppress status output.

-o, --output out
    Write to the file 'out'. If splitting is used then this parameter is treated a bit differently. See the --split parameter discussion for details.

--title <title>
    Sets the general title for the output file, e.g. the movie name.

--global-tags <file>
    Read global tags from the XML file. See the section about tags below for details.

--default-language <lng>
    Sets the default language code. Unless overridden with the --language option this language code will be used. The default language code is 'und' for 'undefined'.

Chapter handling: (global options)

--chapter-language <language>
    Sets the ISO639-2 language code that is written for each chapter entry. Applies only to simple chapter files. Defaults to "eng". See the section about chapters below for details.

--chapter-charset <charset>
    Sets the charset that is used for the conversion to UTF-8 for simple chapter files. Defaults to the current system locale. This switch does also apply to chapters that are copied from an Ogg/OGM file. See the section about chapters below for details.

--cue-chapter-name-format <format>
    mkvmerge supports reading CUE sheets for audio files as the input for chapters. CUE sheets usually contain the entries PERFORMER and TITLE for each index entry. mkvmerge uses these two strings in order to construct the chapter name. With this option the format used for this name can be set. The following meta characters are supported:
    %p is replaced by the current entry's PERFORMER string,
    %t is replaced by the current entry's TITLE string,
    %n is replaced by the current track number and
    %N is replaced by the current track number padded with a leading zero if it is < 10.
    Everything else is copied as-is.
    If this option is not given then mkvmerge defaults to the format '%p - %t' (the performer, followed by a space, a dash, another space and the title).

--chapters <file>
    Read chapter information from the file. See the section about chapters below for details.

General output control (advanced global options):

--track-order <FID1:TID1[,FID2:TID2,...]>
    This option changes the order in which the tracks for an input file are created. The argument is a comma separated list of pairs IDs. Each pair contains first the file ID which is simply the number of the file on the command line starting at 0. The second is a track ID from that file. If some track IDs are omitted then those tracks are created after the ones given with this option have been created.

--cluster-length  n[ms]
    Put at most n data blocks into each cluster. If the number is postfixed with 'ms' then put at most n milliseconds of data into each cluster. The maximum length for a cluster is 32767ms. Programs will only be able to seek to clusters, so creating larger clusters may lead to imprecise seeking and/or processing.

--no-cues
    Tells mkvmerge not to create and write the cue data which can be compared to an index in an AVI. Matroska files can be played back without the cue data, but seeking will probably be imprecise and slower. Use this only if you're really desperate for space or for testing purposes. See also option --cues which can be specified for each input file.

--no-clusters-in-meta-seek
    Tells mkvmerge not to create a meta seek element at the end of the file containing all clusters. See also the section about MATROSKA FILE LAYOUT.

--disable-lacing
    Disables lacing for all tracks. This will increase the file's size, especially if there are many audio tracks. This option is not intended for everyday use.

--enable-durations
    Write durations for all blocks. This will increase file size and does not offer any additional value for players at the moment.

--timecode-scale <n>
    Forces the timecode scale factor to n. Valid values are in the range 1000..10000000 or the value -1. Normally mkvmerge will use a value of 1000000 which means that timecodes and durations will have a precision of 1ms. For files that will not contain a video track but at least one audio track mkvmerge will automatically chose a timecode scale factor so that all timecodes and durations have a precision of one sample. This causes bigger overhead but allows precise seeking and extraction. If the magical value -1 is used then mkvmerge will use sample precision even if a video track is present.

File splitting and linking (more global options):

--split size:<d[k|m|g]> or shorter --split <d[k|m|g]>

--split duration:<HH:MM:SS.nnnnnnnnn|ns> or shorter --split <HH:MM:SS.nnnnnnnnn|ns>

--split timecodes:A[,B[,C...]]
    Splits the output file after a given size or a given time. At the moment mkvmerge supports three different modes.
    1. Splitting by size.
    The parameter d may end with k, m or g to indicate that the size is in KB, MB or GB respectively. Otherwise a size in Bytes is assumed. After the current output file has reached this size limit a new one will be started. The size: prefix may be omitted for compatibility reasons.
    2. Splitting after a duration.
    The paramter must have the form HH:MM:SS.nnnnnnnnn for specifying the duration in up to nano-second precision or a number n followed by the letter 's' for the duration in seconds. "HH" is the number of hours, "MM" the number of minutes, "SS" the number of seconds and "nnnnnnnnn" the number of nanoseconds. Both the number of hours and the number of nanoseconds can be omitted. There can be up to nine digits after the decimal point. After the duration of the contents in the current output has reached this limit a new output file will be started. The duration: prefix may be omitted for compatibility reasons.
    3. Splitting after specific timecodes.
    The parameters A, B etc must all have the same format as the ones used for the duration (see above). The list of timecodes is separated by commas. After the current file has reached the current split point's timecode a new file is created. Then the next split point given in this list is used. The timecodes: prefix must not be omitted.
    For this splitting mode the output filename is treated differently than for the normal operation. It may contain a printf like expression '%d' including an optional field width, e.g. '%02d'. If it does then the current file number will be formatted appropriately and inserted at that point in the filename. If there is no such pattern then a pattern of '-%03d' is assumed right before the file's extension: '-o output.mkv' would result in 'output-001.mkv' and so on. If there's no extension then '-%03d' will be appended to the name.

--split-max-files <n>
    Create at most n files, even if the last file will be longer or larger than indicated by --split.

--link
    Link files to one another when splitting the output file. See the section FILE LINKING below for details.

--link-to-previous <SID>
    Links the first output file to the segment with the given SID. See the section FILE LINKING below for details.

--link-to-next <SID>
    Links the last output file to the segment with the given SID. See the section FILE LINKING below for details.

Attachment support (more global options):

--attachment-description <description>
    Plain text description of the following attachment. Applies to the next --attach-file or --attach-file-once command.

--attachment-mime-type <MIME type>
    MIME type of the following attachment. Applies to the next --attach-file or --attach-file-once command. A list of officially recognized MIME types can be found e.g. at <ftp://ftp.isi.edu/in-notes/iana/assignments/media-types/media-types> The MIME type is mandatory for an attachment.

--attachment-name <name>
    Sets the name that will be stored in the output file for this attachment. If this option is not given then the name will be derived from the file name of the attachment as given with the --attach-file or the --attach-file-once option.

--attach-file <file name>

--attach-file-once <file name>
    Creates a file attachment inside the Matroska file. The MIME type must have been set before this option can used. The difference between the two forms is that during splitting the files attached with --attach-file are attached to all output files while the ones attached with --attach-file-once are only attached to the first file created. If splitting is not used then both do the same.
    mkvextract can be used to extract attached files from a Matroska file.
    Note: If an input file is a Matroska file then the attached files will not be copied to the output file(s). This may change in the future.

Options that can be used for each input file:

-a, --atracks <n,m,...>
    Copy the audio tracks n, m etc. The numbers are track IDs which can be obtained with the --identify switch. They're not simply the track numbers (see section TRACK IDS). Default: copy all audio tracks.

-d, --vtracks <n,m,...>
    Copy the video tracks n, m etc. The numbers are track IDs which can be obtained with the --identify switch (see section TRACK IDS). They're not simply the track numbers. Default: copy all video tracks.

-s, --stracks <n,m,...>
    Copy the subtitle tracks n, m etc. The numbers are track IDs which can be obtained with the --identify switch (see section TRACK IDS). They're not simply the track numbers. Default: copy all subtitle tracks.

-b, --btracks <n,m,...>
    Copy the button tracks n, m etc. The numbers are track IDs which can be obtained with the --identify switch (see section TRACK IDS). They're not simply the track numbers. Default: copy all button tracks.

-A, --noaudio
    Don't copy any audio track from this file.

-D, --novideo
    Don't copy any video track from this file.

-S, --nosubs
    Don't copy any subtitle track from this file.

-B, --nobuttons
    Don't copy any button track from this file.

--no-chapters
    If the source is a Matroska file then don't copy chapters from it.

--no-attachments
    If the source is a Matroska file then don't copy attachments from it.

--no-tags
    If the source is a Matroska file then don't copy tags from it.

-y, --sync <TID:d[,o[/p]]>
    Synchronize manually, delay the audio track with the id TID by d ms. The track IDs are the same as the ones given with --identify (see section TRACK IDS).
    d > 0: Pad with silent samples.
    d < 0: Remove samples from the beginning.
    o/p: adjust the timestamps by o/p to fix linear drifts. p defaults to 1000 if omitted. Both o and p can be floating point numbers.
    Defaults: no manual sync correction (which is the same as d = 0 and o/p = 1.0).
    This option can be used multiple times for an input file applying to several tracks by selecting different track IDs each time.

--delay <TID:x>
    The delay to apply to the packets of the track by simply adjusting the timecodes. The argument x must be postfixed with s, ms, us or ns to specify seconds, milliseconds, microseconds and nanoseconds respectively.

--cues <TID:none|iframes|all>
    Controls for which tracks cue (index) entries are created for the given track (see section TRACK IDS). none inhibits the creation of cue entries. For iframes only blocks with no backward or forward references ( = I frames in video tracks) are put into the cue sheet. all causes mkvmerge to create cue entries for all blocks which will make the file very big.
    The default is iframes for video tracks and none for all others. See also option --no-cues which inhibits the creation of cue entries regardless of the --cues options used.
    This option can be used multiple times for an input file applying to several tracks by selecting different track IDs each time.

--default-track <TID>
    Sets the 'default' flag for the given track (see section TRACK IDS). If the user does not explicitly select a track himself then the player should prefer the track that has his 'default' flag set. Only one track of each kind (audio, video, subtitles, buttons) can have his 'default' flag set.
    This option can be used multiple times for an input file applying to several tracks by selecting different track IDs each time.

--blockadd <TID:level>
    Keep only the BlockAdditions up to this level for the given track. The default is to keep all levels. This option only affects certain kinds of codecs like WAVPACK4.

--track-name <TID:name>
    Sets the track name for the given track (see section TRACK IDS) to name.

--language <TID:language>
    Sets the language for the given track (see section TRACK IDS). Both ISO639-2 language codes and ISO639-1 country codes are allowed. The country codes will be converted to language codes automatically. All languages including their ISO639-2 codes can be listed with the --list-languages option.
    This option can be used multiple times for an input file applying to several tracks by selecting different track IDs each time.

-t, --tags <TID:file>
    Read tags for the track with the number TID from the file. See the section about tags below for details.

--aac-is-sbr <TID>
    Tells mkvmerge that the track with the ID TID is SBR AAC (also known as HE-AAC or AAC+). This options is needed if a) the source file is an AAC file (NOT for a Matroska file) and b) the AAC file contains SBR AAC data. The reason for this switch is that it is technically impossible to automatically tell normal AAC data from SBR AAC data without decoding a complete AAC frame. As there are several patent issues with AAC decoders I won't implement this decoding stage. So for SBR AAC files this switch is mandatory. The resulting file might not play back correctly or even not at all if the switch was omitted.
    If the source file is a Matroska file then the CodecID should be enough to detect SBR AAC. However, if the CodecID is wrong then this switch can be used to correct that.

--timecodes <TID:filename>
    Read the timecodes to be used for the specific track ID from filename. These timecodes forcefully override the timecodes that mkvmerge normally calculates. Read the section about EXTERNAL TIMECODE FILES.

--default-duration <TID:x>
    Forces the default duration of a given track to the specified value. The argument x must be postfixed with s, ms, us or ns to specify the default duration in seconds, milliseconds, microseconds and nanoseconds respectively.
    This argument is mainly useful for debugging purposes and should normally not be used. If the default duration is not forced then mkvmerge will try to derive the track's default duration from the container and/or codec used.

--append-to <SFID1:STID1:DFID1:DTID1[,...]>
    This option controls to which track another track is appended. Each spec contains four IDs: a file ID, a track ID, another file ID and a second track ID. The first pair, "source file ID" and "source track ID", identifies the track that is to be appended. The second pair, "destination file ID" and "destination track ID", identifies the track the first one is appended to.
    If this option has been omitted then a standard mapping is used. This standard mapping appends each track from the current file to a track from the previous file with the same track ID. This allows for easy appending if a movie has been split into two parts and both file have the same number of tracks and track IDs with the command
    mkvmerge -o output.mkv part1.mkv +part2.mkv

Options that only apply to video tracks:

-f, --fourcc <TID:FourCC>
    Forces the FourCC to the specified value. Works only for video tracks in the 'MS compatibility mode'.

--display-dimensions <TID:widthxheight>
    Matroska files contain two values that set the display properties that a player should scale the image on playback to: display width and display height. These values can be set with this option, e.g. '1:640x480'.
    Another way to specify the values is to use the --aspect-ratio or the --aspect-ratio-factor option (see below). These options are mutually exclusive.

--aspect-ratio <TID:ar|w/h>
    Matroska files contain two values that set the display properties that a player should scale the image on playback to: display width and display height. With this option mkvmerge will automatically calculate the display width and display height based on the image's original width and height and the aspect ratio given with this option. The ratio can be given either as a floating point number or as 'width/height', e.g. 16/9.

--aspect-ratio-factor <TID:ar|w/h>
    Another way to set the aspect ratio is to specify a factor. The original aspect ratio is first multiplied with this factor and used as the target aspect ratio afterwards.
    Another way to specify the values is to use the --aspect-ratio option (see above). These options are mutually exclusive.

--cropping <TID:left,top,right,bottom>
    Sets the pixel cropping parameters of a video track to the given values.

Options that only apply to text subtitle tracks:

--sub-charset <TID:charset>
    Sets the charset for the conversion to UTF-8 for UTF-8 subtitles for the given track ID. If not specified the charset will be derived from the current locale settings. Note that a charset is not needed for subtitles read from Matroska files as these are always stored in UTF-8.
    This option can be used multiple times for an input file applying to several tracks by selecting different track IDs each time.

Options that only apply to VobSub subtitle tracks:

--compression <TID:method>
    Selects the compression method to be used for the VobSub track. Note that the player also has to support this method! Valid values are 'none' and 'zlib'. The default is 'zlib' compression.

Other options:

-i, --identify <filename>
    Will let mkvmerge probe the single file and report its type, the tracks contained in the file and their track IDs. If this option is used then the only other option allowed is the filename.

-l, --list-types
    Lists supported input file types.

--list-languages
    Lists all languages and their ISO639-2 code which can be used with the --language option.

--priority <priority>
    Sets the process priority that mkvmerge runs with. Valid values are "lowest", "lower", "normal", "higher" and "highest". If nothing is given then "normal" is used. On Unix like systems mkvmerge will use the nice(2) function. Therefore only the super user can use "higher" and "highest". On Windows all values are useable for every user.

--command-line-charset <charset>
    Sets the charset to convert strings given on the command line from. It defaults to the charset given by system's current locale. This settings applies to arguments of the following options: --title, --track-name and --attachment-description.

--output-charset <charset>
    Sets the charset to which strings are converted that are to be output. It defaults to the charset given by system's current locale.

-r, --redirect-output <filename>
    Writes all messages to the file filename instead of to the console. While this can be done easily with output redirection there are cases in which this option is needed: when the terminal reinterprets the output before writing it to a file. The charset set with --output-charset is honored.

@optionsfile
    Reads additional command line arguments from the file optionsfile. Lines whose first non-whitespace character is a hash mark (#) are treated as comments and ignored. White spaces at the start and end of a line will be stripped. Each line must contain exactly one option. There is no meta character escaping.
    The command line mkvmerge -o "my file.mkv" -A "a movie.avi" sound.ogg could be converted into the following option file:
    # Write to the file "my file.mkv".
    -o
    my file.mkv
    # Only take the video from "a movie.avi".
    -A
    a movie.avi
    sound.ogg

-h, --help
    Show usage information.

-V, --version
    Show version information.

USAGE

For each file the user can select which tracks mkvmerge should take. They are all put into the file specified with '-o'. A list of known (and supported) source formats can be obtained with the '-l' option.

EXAMPLES

Let's assume you have a file called MyMovie.avi and the audio track in a separate file, e.g. MyMovie.wav. First you want to encode the audio to OGG:

$ oggenc -q4 -oMyMovie.ogg MyMovie.wav

After a couple of minutes you can join video and audio:

$ mkvmerge -o MyMovie-with-sound.mkv MyMovie.avi MyMovie.ogg

If your AVI already contains an audio track then it will be copied as well (if mkvmerge supports the audio format). To avoid that simply do

$ mkvmerge -o MyMovie-with-sound.mkv -A MyMovie.avi MyMovie.ogg

After some minutes of consideration you rip another audio track, e.g. the director's comments or another language to MyMovie-add-audio.wav. Encode it again and join it up with the other file:

$ oggenc -q4 -oMyMovie-add-audio.ogg MyMovie-add-audio.wav
$ mkvmerge -o MM-complete.mkv MyMovie-with-sound.mkv MyMovie-add-audio.ogg

The same result can be achieved with

$ mkvmerge -o MM-complete.mkv -A MyMovie.avi MyMovie.ogg \
  MyMovie-add-audio.ogg

Now fire up mplayer and enjoy. If you have multiple audio tracks (or even video tracks) then you can tell mplayer which track to play with the '-vid' and '-aid' parameters. These are 0-based and do not distinguish between video and audio.

If you need an audio track synchronized you can do that easily. First find out which track ID the Vorbis track has with

$ mkvmerge --identify outofsync.ogg

Now you can use that ID in the following command line:

$ mkvmerge -o goodsync.mkv -A source.avi -y 12345:200 outofsync.ogg

This would add 200ms of silence at the beginning of the audio track with the ID 12345 taken from outofsync.ogg.

Some movies start synced correctly but slowly drift out of sync. For these kind of movies you can specify a delay factor that is applied to all timestamps - no data is added or removed. So if you make that factor too big or too small you'll get bad results. An example is that an episode I transcoded was 0.2 seconds out of sync at the end of the movie which was 77340 frames long. At 29.97fps 0.2 seconds correspond to approx. 6 frames. So I did

$ mkvmerge -o goodsync.mkv -y 23456:0,77346/77340 outofsync.mkv

The result was fine.

The sync options can also be used for subtitles in the same manner.

For text subtitles you can either use some Windows software (like SubRipper) or the subrip package found in transcode(1)'s sources (in contrib/subrip). The general process is:

1.  extract a raw subtitle stream from the source:
    $ tccat -i /path/to/copied/dvd/ -T 1 -L | \
        tcextract -x ps1 -t vob -a 0x20 | \
        subtitle2pgm -o mymovie

2.  convert the resulting PGM images to text with gocr:
    $ pgm2txt mymovie

3.  spell-check the resulting text files:
    $ ispell -d american *txt

4.  convert the text files to a SRT file:
    $ srttool -s -w -i mymovie.srtx -o mymovie.srt

The resulting file can be used as another input file for mkvmerge:

$ mkvmerge -o mymovie.mkv mymovie.avi mymovie.srt

If you want to specify the language for a given track then this is easily done. First find out the ISO639-2 code for your language. mkvmerge can list all of those codes for you:

$ mkvmerge --list-languages

Search the list for the languages you need. Let's assume you have put two audio tracks into a Matroska file and want to set their language codes and that their track IDs are 2 and 3. This can be done with

$ mkvmerge -o with-lang-codes.mkv --language 2:ger --language 3:dut without-lang-codes.mkv

As you can see you can use the --language switch multiple times.

Maybe you'd also like to have the player use the Dutch language as the default language. You also have extra subtitles, e.g. in English and French, and want to have the player display the French ones by default. This can be done with

$ mkvmerge -o with-lang-codes.mkv --language 2:ger --language 3:dut --default-track 3 without-lang-codes.mkv --language 0:eng english.srt --default-track 0 --language 0:fre french.srt

If you do not see the language or default track flags that you've specified in mkvinfo's output then please read the section about DEFAULT VALUES.

TRACK IDS

Some of the options for mkvmerge need a track ID to specify which track they should be applied to. Those track IDs are printed by the readers when demuxing the current input file, or if mkvmerge is called with the --identify option. An example for such output:

$ mkvmerge -i v.mkv
File 'v.mkv': container: Matroska
Track ID 1: video (V_MS/VFW/FOURCC, DIV3)
Track ID 2: audio (A_MPEG/L3)

Track IDs are assigned like this:

*   AVI files: The video track has the ID 0. All audio tracks get the ID 1, 2...

*   AAC, AC3, MP3, SRT and WAV files: The one 'track' in that file gets the ID 0.

*   Ogg/OGM files: The track's ID is its position in the Ogg stream. The first stream encountered has the ID 0, the second one the ID 1 etc.

*   Matroska files: The track's ID is the track number as reported by mkvinfo or mkvmerge --identify. It is not the track UID.

The special track ID '-1' is a wild card and applies the given switch to all tracks that are read from an input file. This was the behavior of these switches prior to version 0.4.4.

The options that use the track IDs are the ones whose description contains 'TID'. The following options use track IDs as well: --atracks, --vtracks, --stracks and --btracks.

SUBTITLES

There are several text subtitle formats that can be embedded into Matroska. At the moment mkvmerge supports only text subtitle formats. These subtitles must be recoded to UTF-8 so that they can be displayed correctly by a player.

mkvmerge does this conversion automatically based on the system's current locale. If the subtitle charset is not the same as the system's current charset then the user can use --sub-charset switch. If the subtitles are already encoded in UTF-8 then you can use --sub-charset UTF-8.

The following subtitle formats are supported at the moment:

*   Subtitle Ripper (SRT) files

*   Substation Alpha (SSA) / Advanced Substation Alpha scripts (ASS)

FILE LINKING

Matroska supports file linking which simply says that a specific file is the predecessor or successor of the current file. To be precise, it's not really the files that are linked but the Matroska segments. As most files will probably only contain one Matroska segment I simply say 'file linking' although 'segment linking' would be more appropriate.

Each segment is identified by a unique 128 bit wide segment UID. This UID is automatically generated by mkvmerge. The linking is done primarily via putting the segment UIDs (short: SID) of the previous/next file into the segment header information. mkvinfo(1) prints these SIDs if it finds them.

If a file is split into several smaller ones and linking is used then the timecodes will not start at 0 again but will continue where the last file has left off. This way the absolute time is kept even if the previous files are not available (e.g. when streaming). If no linking is used then the timecodes should start at 0 for each file. By default mkvmerge does not use file linking. If you want that you can turn it on with the '-link' option. This option is only useful if splitting is activated as well.

Regardless of whether splitting is active or not the user can tell mkvmerge to link the produced files to specific SIDs. This is achieved with the options '--link-to-previous' and '--link-to-next'. These options accept a segment SID in the format that mkvinfo(1) outputs: 16 hexadecimal numbers between 0x00 and 0xff prefixed with '0x' each, e.g. 0x41 0xda 0x73 0x66 0xd9 0xcf 0xb2 0x1e 0xae 0x78 0xeb 0xb4 0x5e 0xca 0xb3 0x93. Alternatively a shorter form can be used: 16 hexadecimal numbers between 0x00 and 0xff without the '0x' prefixes and without the spaces, e.g. 41da7366d9cfb21eae78ebb45ecab393.

If splitting is used then the first file is linked to the SID given with '--link-to-previous' and the last file is linked to the SID given with '--link-to-next'. If splitting is not used then the one output file will be linked to both of the two SIDs.

DEFAULT VALUES

The Matroska specs say that some elements have a default value. Usually an element is not written to the file if its value is equal to its default value in order to save space. The elements that the user might miss in mkvinfo's output are the language and the default track flag. The default value for the language is English (eng), and the default value for the default track flag is true. Therefore if you used --language 0:eng for a track then it will not show up in mkvinfo's output.

ATTACHMENTS

Maybe you also want to keep some photos along with your Matroska file, or you're using SSA subtitles and need a special TrueType font that's really rare. In these cases you can attach those files to the Matroska file. They will not be just appended to the file but embedded in it. A player can then show those files (the 'photos' case) or use them to render the subtitles (the 'TrueType fonts' case).

Here's an example how to attach a photo and a TrueType font to the output file:
$ mkvmerge -o output.mkv -A video.avi sound.ogg --attachment-description "Me and the band behind the stage in a small get-together" --attachment-mime-type image/jpeg --attach-file me_and_the_band.jpg --attachment-description "The real rare and unbelievably good looking font" --attachment-type application/octet-stream --attach-file really_cool_font.ttf

CHAPTERS

The Matroska chapter system is more powerful than the old known system used by OGMs. The full specs can be found at <http://www.matroska.org/technical/specs/chapters/index.html>

mkvmerge supports two kinds of chapter files as its input. The first format, called 'simple chapter format', is the same format that the OGM tools expect. The second format is a XML based chapter format which supports all of Matroska's chapter functionality.

The simple chapter format It looks basically like this:

CHAPTER01=00:00:00.000
CHAPTER01NAME=Intro
CHAPTER02=00:02:30.000
CHAPTER02NAME=Baby prepares to rock
CHAPTER03=00:02:42.300
CHAPTER03NAME=Baby rocks the house

mkvmerge will transform every pair or lines (CHAPTERxx and CHAPTERxxNAME) into one Matroska ChapterAtom. It does not set any ChapterTrackNumber which means that the chapters all apply to all tracks in the file.

The charset used in the file is assumed to be the same charset that the current system's locale returns. If this is not the case then the switch --chapter-charset should be used. If the file contains a valid BOM (byte order marker) then all UTF styles are converted automatically. In this case --chapter-charset is simply ignored. You can use mkvinfo or mkvextract to verify that the chapter names have been converted properly.

The XML based chapter format The XML based chapter format looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Chapters SYSTEM "matroskachapters.dtd">
<Chapters>
  <EditionEntry>
    <ChapterAtom>
      <ChapterTimeStart>00:00:30.000</ChapterTimeStart>
      <ChapterTimeEnd>00:01:20.000</ChapterTimeEnd>
      <ChapterDisplay>
        <ChapterString>A short chapter</ChapterString>
        <ChapterLanguage>eng</ChapterLanguage>
      </ChapterDisplay>
      <ChapterAtom>
        <ChapterTimeStart>00:00:46.000</ChapterTimeStart>
        <ChapterTimeEnd>00:01:10.000</ChapterTimeEnd>
        <ChapterDisplay>
          <ChapterString>A part of that short chapter</ChapterString>
          <ChapterLanguage>eng</ChapterLanguage>
        </ChapterDisplay>
      </ChapterAtom>
    </ChapterAtom>
  </EditionEntry>
</Chapters> With this format three things are possible that are not possible with the simple chapter format: 1) The timestamp for the end of the chapter can be set, 2) chapters can be nested, 3) the language and country can be set.

The mkvtoolnix distribution contains some sample files in the doc subdirectory which can be used as a basis.

General notes

When splitting files mkvmerge will correctly adjust the chapters as well. This means that each file only includes the chapter entries that apply to it, and that the timecodes will be offset to match the new timecodes of each output file.

mkvmerge is able to copy chapters from Matroska source files unless this is explicitly disabled with the --no-chapters option. At the moment mkvmerge is limited to one 'bunch of chapters' globally. This means that only the first chapter section found in all source files is used. If the user specified chapters on the command line then these take precedence over any chapters found in source files. mkvmerge does not merge chapters. This must be done manually by using mkvextract to extract the chapter information and editing the resulting files.

One shortcoming is that mkvmerge cannot parse chapter information found in OGM files.

TAGS

Introduction Matroska supports an extensive set of tags that is deprecated and a new, simpler system like it is is used in most other containers: KEY=VALUE. However, in Matroska these tags can also be nested, and both the KEY and the VALUE are elements of their own. The example file example-tags-2.xml shows how to use this new system.

Scope of the tags Matroska tags do not automatically apply to the complete file. They can, but they also may apply to different parts of the file: to one or more tracks, to one or more chapters, or even to a combination of both. The aforementioned URL gives more details about this fact.

One important fact is that tags are linked to tracks or chapters with the Targets Matroska tag element, and that the UIDs used for this linking are NOT the track IDs mkvmerge uses everywhere. Instead the numbers used are the UIDs which mkvmerge calculates automatically (if the track is taken from a file format other than Matroska) or which are copied from the source file if the track's source file is a Matroska file. Therefore it is difficult to know which UIDs to use in the tag file before the file is handed over to mkvmerge.

mkvmerge knows two options with which you can add tags to Matroska files: The --global-tags and the --tags options. The difference is that the former option, --global-tags, will make the tags apply to the complete file by removing any of those Targets elements mentioned above. The latter option, --tags, automatically inserts the UID that mkvmerge generates for the tag specified with the TID part of the --tags option.

Example Let's say that you want to add tags to a video track read from an AVI. mkvmerge -i file.avi tells you that the video track's ID (do not mix this ID with the UID!) is 0. So you create your tag file, leave out any Targets element and call mkvmerge:
$ mkvmerge -o file.mkv --tags 0:tags.xml file.avi

Tag file format mkvmerge supports a XML based tag file format. The format is very easy and closely connected to the Matroska tag specs found at the URL mentioned above. Both the binary and the source mkvtoolnix distributions come with a sample file called \xample-tags-2.xml which simply lists all known tags and which can be used as a basis for real life tag files.

The basics are:

*   The outermost element must be <Tags>.

*   One logical tag is contained inside one pair of <Tag> XML tags.

*   White spaces directly before and after tag contents are ignored.

Data types The new Matroska tagging system only knows two data types, a UTF-8 string and a binary type. The first is used for the tag's name and the <String> element while the binary type is used for the <Binary> type.

As binary data itself would not fit into a XML file mkvmerge supports two other methods of storing binary data. If the contents of a XML tag starts with '@' then the following text is treated as a file name. The corresponding file's content is copied into the Matroska element.

Otherwise the data is expected to be Base64 encoded. This is an encoding that transforms binary data into a limited set of ASCII characters and is used e.g. in email programs. mkvtoolnix comes with a utility, base64tool, that can be used to encode to and decode from Base64. mkvextract will output Base64 encoded data for binary elements.

The deprecated tagging system knows some more data types which can be found in the official Matroska tag specs. The following two paragraphs only apply to the deprecated tags (an example file is still available and called example-tags-deprecated.xml):

The types integer, unsigned integer, float, string and UTF-8 string look just like you expect them to: 4254, -2, 5.0, hello world and hello world.

The date format used by both mkvmerge when reading XML tag files and by mkvextract when outputting XML tag data is the ISO-8601 format. It has the following structure: YYYY-MM-DDTHH:MM:SS+TZTZ. YYYY is the year (four digits long), MM the month (two digits long starting with 01), DD the day of the month (two digits long starting with 01), HH the hour of the day (two digits long, range 00 - 23), MM the minute (two digits long, range 00 - 59), SS the seconds (two digits long, range 00 - 59). +TZTZ is the time zone, e.g. +0100 or -0200. An example would be 2003-07-30T19:10:16+0200.

MATROSKA FILE LAYOUT

The Matroska file layout is quite flexible. mkvmerge will render a file in a predefined way. The resulting file looks like this:

[EBML head] [segment {meta seek #1} {attachments} {chapters} [segment information] [track information] [cluster 1] {cluster 2} ... {cluster n} {cues} {meta seek #2} {tags}]

The elements in curly braces are optional and depend on the contents and options used. Some notes:

*   meta seek #1 includes only a small number of level 1 elements, and only if they actually exist: attachments, chapters, cues, tags, meta seek #2. Older versions of mkvmerge used to put the clusters into this meta seek element as well. Therefore some imprecise guessing was necessary to reserve enough space. It often failed. Now only the clusters are stored in meta seek #2, and meta seek #1 refers to the meta seek element #2.

*   Attachment, chapter and tag elements are only present if they were added.

The shortest possible Matroska file would look like this:

[EBML head] [segment [segment information] [track information] [cluster 1]]

This might be the case for audio-only files.

EXTERNAL TIMECODE FILES

mkvmerge allows the user to chose the timecodes for a specific track himself. This can be used in order to create files with variable frame rate video or include gaps in audio. A frame in this case is the unit that mkvmerge creates separately per Matroska block. For video this is exactly one frame, for audio this is one packet of the specific audio type. E.g. for AC3 this would be a packet containing 1536 samples.

Timecode files that are used when tracks are appended to each other must only be specified for the first part in a chain of tracks. For example if you append two files, v1.avi and v2.avi, and want to use timecodes then your command line must look something like this:
mkvmerge ... --timecodes 0:my_timecodes.txt v1.avi +v2.avi

There are three formats that are recognized by mkvmerge. The first line always contains the version number. Empty lines, lines containing only whitespace and lines beginning with '#' are ignored.

Timecode file format v1

This format starts with this line:
# timecode format v1
The second line gives the default number of frames per second:
assume 27.930
All following lines contain three numbers separated by commas: the start frame (0 is the first frame), the end frame and the number of frames in this range. The FPS is a floating point number with the dot default FPS is used. Example:
800,1000,25
1500,1700,30

Timecode file format v2

In this format each line contains a timecode for the next frame. This timecode must be given in ms precision. It can be a floating point number, but it doesn't have to be. You must give at least as many timecode lines as there are frames in the track. The timecodes in this file must be sorted. Example for 25fps:
# timecode format v2
0
40
80
etc.

Timecode file format v3

In this format each line contains a duration in seconds followed by an optional number of frames per second. Both can be floating point numbers. If the number of frames per second is not present the default one is used. For audio you should let the codec calculate the frame timecodes itself. For that you should be using 0.0 as the number of frames per second. You can also create gaps in the stream by using the gap keyword followed by the duration of the gap. Example for an audio file:
# timecode format v3
assume 0.0
25.325
7.530,38.236
gap, 10.050
2.000,38.236
etc.

Timecode file format v4

This format is identical to the v2 format. The only difference is that the timecodes do not have to be sorted. This format should almost never be used.

EXIT CODES

mkvmerge exits with one of three exit codes:

0   This exit codes means that muxing has completed successfully.

1   In this case mkvmerge has output at least one warning, but muxing did continue. A warning is prefixed with the text 'Warning:'. Depending on the issues involved the resulting file might be ok or not. The user is urged to check both the warning and the resulting file.

2   This exit code is used after an error occured. mkvmerge aborts right after outputting the error message. Error messages range from wrong command line arguments over read/write errors to broken files.

NOTES

What works (this list is probably outdated):

*   AVI as the video and audio source (only raw PCM, MP3 and AC3 audio tracks at the moment)

*   OGG as the source for video, audio (Vorbis, raw PCM, MP3 and AC3 audio) and text streams (subtitles).

*   WAV as the audio source

*   AAC audio files (ADTS AAC files and AAC from MP4)

*   AC3 audio files

*   DTS audio files

*   MP3 audio files

*   RealVideo and RealAudio from RealMedia files

*   FLAC audio files (both raw FLAC and OggFLAC)

*   Track selection

*   Manual audio synchronization by adding silence/removing packets for Vorbis audio and for text streams by adjusting the starting point and duration.

*   Manual audio synchronization for AAC, AC3, DTS and MP3 audio by duplicating or removing packets at the beginning.

*   Text subtitles can be read from SRT (SubRipper / subrip) files or taken from other OGM files.

*   SSA/ASS subtitles from SSA/ASS files

*   Simple chapters.

*   Full tags support.

What not works:

*   Manual audio synchronization for PCM sound (who needs it anyway?)

AUTHOR
mkvmerge was written by Moritz Bunkus <moritz@bunkus.org>.

SEE ALSO
mkvinfo(1), mkvextract(1), mmg(1)

WWW
The newest version can always be found at <http://www.bunkus.org/videotools/mkvtoolnix/> (http://www.bunkus.org/videotools/mkvtoolnix/)