Log in

View Full Version : mkvmerge syntax


LonelyPixel
14th March 2016, 18:41
I have 3 .mkv files with different track layouts. Two have video and audio, and the third has an additional subtitle track. I have a 4th file, an empty subtitle file alone.

The mkvmerge syntax is too complicated, I don't get it. Simply merging the source files fails because the tracks are different.

What command do I need to append all files and inject the additional subtitle file to the 1st and 3rd of them?

sneaker_ger
14th March 2016, 19:17
I think it's not possible, at least not without tricks. Mkvmerge cannot append the third file to the two before that. You could either create dummy subtitle tracks for the first two or insert the subtitles from the third file with a manual delay:
mkvmerge --append-to 1:0:0:0,1:1:0:1,2:0:1:0,2:1:1:1 -o output.mkv file1.mkv +file2.mkv --no-subtitles +file3.mkv --no-video --no-audio --sync 2:[LENGTH_OF_FILE1+FILE2_IN_MS] file3.mkv file4.srt

Probably easier to first append the three video+audio pairs + the first file. Then finally manually insert subtitles from file3.

LonelyPixel
14th March 2016, 19:29
I'm okay with doing it in steps of two files each. How would that look?

sneaker_ger
14th March 2016, 19:36
First step like above:
mkvmerge --append-to 1:0:0:0,1:1:0:1,2:0:1:0,2:1:1:1 -o output.mkv file1.mkv +file2.mkv --no-subtitles +file3.mkv file4.srt

Second step would be to extract the subtitle from file2.mkv and sync using a subtitle editor like Aegisub. Or open the appended file and look when the first frame of the original file3.mkv is and sync accordingly using delay(--sync) in mkvmerge.

Either way it needs some manual work unless you are a scripting guru and can automatically read and calculate [LENGTH_OF_FILE1+FILE2_IN_MS].

LonelyPixel
14th March 2016, 19:52
I can't get it to work. Here's my current effort. I've put it in a batch file and set variables to the tool binary and the source files because the actual file names are too long to write.

Some info about the files:

%mkvmerge% -i "%video1%"
%mkvmerge% -i "%video2%"
%mkvmerge% -i "%video3%"

Shows:

File 'D:\tmp\video1.mkv': container: Matroska
Track ID 0: video (V_MPEG4/ISO/AVC)
Track ID 1: audio (A_AC3)
Chapters: 5 entries
File 'D:\tmp\video2.mkv': container: Matroska
Track ID 0: video (V_MPEG4/ISO/AVC)
Track ID 1: audio (A_AC3)
Track ID 2: subtitles (S_HDMV/PGS)
Chapters: 2 entries
File 'D:\tmp\video3.mkv': container: Matroska
Track ID 0: video (V_MPEG4/ISO/AVC)
Track ID 1: audio (A_AC3)
Chapters: 3 entries

Additionally I have the empty subtitle file to fill the two source files that don't have a subtitle.

Then I try this:

%mkvmerge% --append-to 1:0:0:0,1:1:0:1,2:0:1:0,2:1:1:1 -o "%result%" "%video1%" + "%video2%" + "%video3%" + "%subtitle%"

The error goes:

'D:\tmp\empty.sup' track 0: Using the output module for the format 'PGS'.
Error: Only partial append mappings were given for the file no. 1 ('D:\tmp\video2.mkv').
Either don't specify any mapping (in which case the default mapping will be used) or specify a mapping for all tracks that are to be copied.

The resulting file should be one .mkv file that has one video track, one audio track, and one subtitle track that only shows text during the time of the 2nd of the 3 source files. The idea I've read about was to use an empty "dummy" subtitle file to satisfy mkvmerge and let it merge files with missing tracks.

sneaker_ger
14th March 2016, 20:02
The "+" before a file name is for appending only. You don't append the subtitle file so there must not be any "+" before "%subtitle%".

In my examples I assumed the third video has the extra subtitles, not the second. You have to adapt. Read them carefully, I am also using --no-subtitles.

The resulting file should be one .mkv file that has one video track, one audio track, and one subtitle track that only shows text during the time of the 2nd of the 3 source files. The idea I've read about was to use an empty "dummy" subtitle file to satisfy mkvmerge and let it merge files with missing tracks.
You can use a dummy track but it's a different concept. Basically you create an mkv with a subtitle track that has no lines at all. Then you add (not append) that to each of your video files. It's a multiple step process but it spares you the work of manually adjusting the subtitles.

LonelyPixel
14th March 2016, 20:09
Argh. So I understand that I need to do the following steps:

* Add the empty subtitle to video1 -> video1'
* Add the empty subtitle to video3 -> video3'
* Append all tracks normally of video1' + video2 + video3' -> result

Is that correct?

How can I add a track to a file? The manpage only talks about appending.

sneaker_ger
14th March 2016, 20:16
Just don't put a "+" before the file name.
mkvmerge -o output.mkv file1.mkv file2.mkv

LonelyPixel
14th March 2016, 20:24
Thank you! Finally that worked. Pretty simple now actually.

:: Add missing subtitle tracks
%mkvmerge% -o "d:\tmp\video1.mkv" "%video1%" "%subtitle%"
%mkvmerge% -o "d:\tmp\video3.mkv" "%video3%" "%subtitle%"

:: Combine files (with now matching tracks) into one
%mkvmerge% -o "%result%" "d:\tmp\video1.mkv" + "%video2%" + "d:\tmp\video3.mkv"