{"id":8,"date":"2007-12-17T00:55:55","date_gmt":"2007-12-16T23:55:55","guid":{"rendered":"http:\/\/www.uppcon.se\/thefluff\/hurfdurf\/?p=8"},"modified":"2009-07-05T17:09:40","modified_gmt":"2009-07-05T16:09:40","slug":"101-things-you-never-knew-you-could-do-with-matroska","status":"publish","type":"post","link":"https:\/\/mod16.org\/hurfdurf\/?p=8","title":{"rendered":"101 things you never knew you could do with Matroska"},"content":{"rendered":"<p>Today on the Fluffy Channel: magic tricks with the Matroska container. Namely, <em>editions<\/em>, <em>file linking<\/em> and <em>ordered chapters<\/em>. (<strong>Warning:<\/strong> very large amounts of techobabble walls of text ahead.)<\/p>\n<p>These are easily some of the most (if not <strong>the<\/strong> most) obfuscated features of the format; a few people know how to use them (and it has been used in one or a few releases) but absolutely noone has written any simple guides to it (hence this blog post). Which is a pity, because it&#8217;s an immensely powerful feature. To start off, why would you want to hack XML files and play with obscure hexadecimal segment ID&#8217;s? Well, with the above features you can do things like:<\/p>\n<ul>\n<li>Make any combination of audio\/video\/subtitle tracks into one choice in the track selection menu (useful to change between dub\/original+sub in one click)<\/li>\n<li>Create chapters-only Matroska files that when played display a virtual timeline pulling video\/audio\/subtitles from other Matroska files<\/li>\n<li>A practical implementation of the above might mean that you encode the OP of a TV series only once and then use chapters to get it inserted into the timeline of each episode<\/li>\n<li>Let the viewer choose between multiple &#8220;angles&#8221; or versions of the video (said versions may or may not be in the same file)<\/li>\n<li> Arbitrarily link MKV files or parts of MKV files so that they appear to the viewer as a whole<\/li>\n<li>And so on and so forth.<\/li>\n<\/ul>\n<p>For this exercise, you will need: a) the latest version of <a href=\"http:\/\/www.bunkus.org\/videotools\/mkvtoolnix\/\">mkvtoolnix<\/a>, b) the latest version of <a href=\"http:\/\/haali.cs.msu.ru\/mkv\/\">Haali&#8217;s Matroska splitter<\/a> (or just latest <a href=\"http:\/\/www.cccp-project.net\">CCCP<\/a>), c) your favorite text editor (never leave home without it) &#8211; preferably one with XML syntax highlighting, and d) a brain (in good working condition, fully awake). Let&#8217;s boldly go, etc.<br \/>\n<!--more--><\/p>\n<h3>File (or Segment) Linking and Segment UID&#8217;s<\/h3>\n<p>All of these concepts tie into each others somewhat, but linking is probably the most simple to understand, as well as the simplest to accomplish. Any combination of Matroska files with the same set of tracks and the same codecs can be <em>linked<\/em> so that when any one of them is opened, a virtual file containing the entire timeline of linked files is created and displayed to the viewer. The most common usage of this is to encode a full-length movie into one 1.4 GiB file and then split it into two 700 MiB parts that, when one of them is played, behave as if it contained the entire movie in one file. This simple use of the feature is very easy to accomplish; in mkvmerge GUI (henceforth known as mmg) just hit the Global tab and enable splitting in whatever form you want and make sure to tick the box &#8220;link files&#8221;.<\/p>\n<p>If you look further down in the same tab you&#8217;ll notice that there&#8217;s an option for &#8220;File\/segment linking&#8221;. This feature lets you link the muxed file to any two other files (with the same set of tracks and codecs used). Anyway the option asks you for something called a Segment UID. What is that, where do we find it and how do we use it?<\/p>\n<p>First of all, I have to let you in on a dirty secret. File linking isn&#8217;t actually file linking at all, since the linking isn&#8217;t done by files or by filenames. It&#8217;s actually <em>segment linking<\/em>, and it&#8217;s done by <em>segment Unique Identifiers<\/em> or &#8220;segment UID&#8217;s&#8221; (SUID&#8217;s). Most Matroska files contain only one segment, but there&#8217;s absolutely nothing that stops you from having more than one segment per file. When the splitter looks for linked files, it just scans all Matroska files in the same folder until it finds the linked segment UID; hence you can rename the files all you want and the linking will still work. On the other hand you can&#8217;t remux the files because that changes the UID.<\/p>\n<p>So how do we know what segment UID we should link to? The answer is, we use mkvinfo on the file we want to link to. You can use the GUI or the CLI, what you&#8217;re looking for is something like this:<br \/>\n<code>Segment UID: 0xa2 0xa6 0xa2 0xa7 0x45 0x22 0x6b 0x85 0x85 0xd4 0x75 0x5e 0x6c 0xf4 0x67 0x60<\/code><br \/>\nYou then copypaste that string of hexadecimal digits (everything after the colon) in the boxes in mmg to do the linking.<\/p>\n<p>If you had a TV series that always had the opening theme as the first thing of each episode, this means you could encode the OP as a separate file, chop it off from all the actual episodes, and then use linking to link in the OP as &#8220;previous segment&#8221; to all the episode files. Which would mean that to a user with the proper splitter all the episodes would appear to have the OP encoded in, but in reality it&#8217;s actually only encoded once and stored in a separate file. Nifty, you think? You haven&#8217;t seen a tenth of it yet. Read on&#8230;<\/p>\n<h3>Chapters, Black Magic Style: Ordered Chapters<\/h3>\n<p>Some of you may have used the simple (OGM-based) chapters format before, but that won&#8217;t suffice for what we want to do here, so let&#8217;s take a look at an example of a Matroska XML chapters file: <a href=\"http:\/\/mod16.org\/samples\/chapters-example.xml\">chapters-example.xml<\/a><br \/>\nThis section and the following ones will involve a lot of hacking of these in a text editor; you can either create them yourself by hand or you can use <code>mkvextract chapters \"file.mkv\" &gt; chapters.xml<\/code> to extract them from existing MKV files (you can then use that as a template for further hacking).<\/p>\n<p>Anyway, the first thing you&#8217;ll notice is that the big tag enveloping all the chapter entries (&lt;ChapterAtom&gt;) is the &lt;EditionEntry&gt; one. If you want multiple editions in the same chapter file, you will need more than one of these (we&#8217;ll get to this later). The rest is fairly straightforward, you have an unique chapter ID (automatically generated, but you can edit it yourself too), a start time (i.e. where your video player seeks when you click &#8220;go to this chapter&#8221;), and some display stuff (what will show up in the splitter menu). You&#8217;ll also notice that you can hide chapters so they don&#8217;t show up in the menu. Personally I&#8217;ve never used that particular feature.<\/p>\n<p>You can also look at the XML in the mmg Chapter Editor to get a nice tree-view of all the tags. It also prevents you from writing invalid syntax, and creates chapter UID&#8217;s for you automagically so using it may be a good idea, but I&#8217;m far from sure it supports all the interesting features we&#8217;re going to use (it doesn&#8217;t support ordered chapters or segment linking at all for example) so I&#8217;m going to stick with my trusted text editor.<br \/>\n<em>Update:<\/em> Harukalover mentions that <a href=\"http:\/\/www.alexander-noe.com\/video\/amg\/\">avimux GUI<\/a> has a surprisingly good editor for this.<\/p>\n<p>So what&#8217;s so interesting about this? It&#8217;s just chapters, isn&#8217;t it? Well, there are some tags you can add yourself to make things more interesting, the most important being <code>&lt;EditionFlagOrdered&gt;1&lt;\/EditionFlagOrdered&gt;<\/code>. This turns on heavy magic mode and turns our plain old chapters into <em>ordered chapters<\/em>, which are quite different. Normal chapters are just seek points; ordered chapters are a description of a virtual timeline. Each ordered chapter has both a start time and an <em>end<\/em> time. To help you understand this concept, let&#8217;s do something silly with ordered chapters. Say that we have a 30 second clip that we want to loop 3 times, but to save space we only want to encode it once. Ordered chapters to the rescue! <a href=\"http:\/\/mod16.org\/samples\/orderedchapters-example.xml\">orderedchapters-example.xml<\/a><\/p>\n<p>Take a look at the example. First off, ordering is turned on by setting the edition flag. Then, the section of the file given by <code>ChapterTimeStart<\/code> to <code>ChapterTimeEnd<\/code> is played once per <code>ChapterAtom<\/code> (note that both ChapterTimeStart <em>and<\/em> ChapterTimeEnd are <em>mandatory<\/em>). Note that the two last chapters are hidden so they won&#8217;t show up as seek points in the splitter. See the virtual timeline? When this is played, the viewer will just see one 1:30 long clip. Mux it for yourself and see; any input file will do, it just has to be longer than 30 seconds or weird things may happen (also make sure to tick &#8220;no chapters&#8221; in mmg).<\/p>\n<p>To reiterate the virtual timeline stuff (this is important): normally when you play a file it plays all the encoded stuff within, from start to end (the &#8220;real timeline&#8221;. With ordered chapters on the other hand&#8230; instead of the splitter displaying the real timeline to the viewer, a <em>virtual<\/em> timeline is constructed from the real one. What is played when is determined by the chapter start and end points (which refer to the real timeline). To the viewer, it appears as if the virtual timeline is the real one, but internally the splitter seeks to the given points in the real timeline to construct the virtual one.<br \/>\n(You may want to read the above paragraph more than once.)<\/p>\n<h3>Ordered Chapters and Segment Linking<\/h3>\n<p>Now that we understand the virtual timeline, how about this linking stuff from other files thing we&#8217;ve been hearing about? Well, that can be done too and it isn&#8217;t too hard. Ordered chapters are not limited to creating the timeline just from the current file, you can also specify that a given chapter should use a certain part of <em>another<\/em> file, specified by its segment UID. It&#8217;s not hard, really. Say that in the example before we wanted the middle of the three iterations to be taken from another file (presumably containing something else). <a href=\"http:\/\/mod16.org\/samples\/orderedchapters-example2.xml\">orderedchapters-example2.xml<\/a><\/p>\n<p>See what I did there? Just add <code>&lt;ChapterSegmentUID format=\"hex\"&gt;<\/code> followed by the SUID you want to pull in things from (0x before each hex digit optional) and blammo, you have linked in stuff from another file. Not specifying a ChapterSegmentUID means that the current segment will be used.<\/p>\n<p>You can mux this yourself to any file and see what happens too, you just need to replace the SUID with one pointing to another of your MKV&#8217;s in the same folder. Just make sure that both of them have same number of tracks in the same order and that they all are using the same codecs. Also make sure to tick &#8220;no chapters&#8221; in mmg or weird things can happen.<\/p>\n<h3>Editions and You<\/h3>\n<p>In Matroska, an <strong>edition<\/strong> is a set of chapters that is presented to the viewer; you could define it as a &#8220;timeline&#8221;. Most Matroska files (and indeed most if not all other multimedia files) only has one edition. However, Matroska isn&#8217;t limited to just one edition per file, you can have as many as you want. By itself it doesn&#8217;t have any immediately obvious uses other than the fact that you could abuse it to have several sets of chapter names in different languages, but if you combine it with ordered chapters things become very interesting. Among other things, it means you can have several virtual timelines co-existing in the same file; the viewer can (if he has a proper splitter (i.e. Haali&#8217;s)) choose between them as he wishes.<br \/>\n<em>Update:<\/em> Haali pointed out to me that you can actually have multi-language chapters without editions; by default the language matching the system&#8217;s default language will be displayed.<\/p>\n<p>Ever wanted to have two versions of an opening theme and letting the viewer choose between them seamlessly? With editions you can. Encode both versions of the opening and the rest of the episode or what-have-you to one file (order doesn&#8217;t matter). Now create two ordered chapters files, one for each timeline (one for op1 + main episode, one for op2 + main episode). Then you combine them using your trusted text editor by adding another <code>&lt;EditionEntry&gt;<\/code>, like so: <a href=\"http:\/\/mod16.org\/samples\/orderedchapters%2beditions-example.xml\">orderedchapters+editions-example.xml<\/a><\/p>\n<p>Note that there are now <em>two<\/em> <code>&lt;EditionEntry&gt;<\/code> tags, each with its own set of chapters and hence each with its own timeline. When played, regardless of which edition is chosen (it will default to the first, see the <code>&lt;EditionFlagDefault&gt;<\/code> flag) will appear to be a clip that is 4 minutes long, the first 30 seconds being identical, the next 60 differing and the last 180 being identical again. If you look at the XML, you will notice that the first edition\/timeline will play the source file like this: [chapter point] 30s [chapter point] 60s [chapter point] JUMP to 02:30, 180s.<br \/>\nThe second edition on the other hand looks like this: [chapter point] 30s [chapter point] JUMP to 01:30, 60s [chapter point] 180s.<\/p>\n<p>As usual you can test-mux my sample XML with any MKV longer than 5 minutes (remember to tick &#8220;no chapters&#8221;) and see what happens.<\/p>\n<p>For added confusion and\/or awesomeness you can of course combine editions with segment linking in the ordered chapters, so you can have one (or both) of the openings in a separate file(s). This may actually be a good idea to avoid making things difficult for people without proper splitters (such as MPlayer users on *nix for example). For bonus points, combine ordered editions with unordered ones.<\/p>\n<h3>Sky&#8217;s the Limit: Advanced Haxing etc.<\/h3>\n<p>You might have noticed that in the previous example that the editions showed up as Edition 1 and Edition 2. Not too descriptive, is it? Well, you can name them too, but it isn&#8217;t so easy as to just adding something to the chapters XML file. Instead you have to add a tags file as well. This is XML too, and it would look something like this for the previous example: <a href=\"http:\/\/mod16.org\/samples\/tags-example.xml\">tags-example.xml<\/a><\/p>\n<p>Just set the <code>EditionUID<\/code> in the <code>Targets<\/code> tag to the UID of the edition you want to change the name of, and then set the <code>String<\/code> to the name of it. Note that the <code>Name<\/code> tag is actually the name of the tag itself (must be TITLE to make anything show up) and not what will actually show up in the splitter menu.<\/p>\n<p>Another potentially interesting thing you can do with ordered chapters is to create small chapters-only files that when played show an entire TV series as one single long virtual file. Just create a 5 or 10 frames long blank video with the same settings that you&#8217;re using for the rest of the series, with corresponding audio and subtitle tracks, create ordered chapters file with segment linking, mux, and blammo 50 KiB file that when played display as a 5 hour long TV series. Of course you can include editions in it as well for added awesomeness.<\/p>\n<p>Yet another opportunity: tracksets. This is actually not included in the Matroska specs, it&#8217;s just a feature of Haali&#8217;s splitter. By abusing the tags file you can set and\/or disable\/enable combinations of tracks when a certain edition is selected. Hello, multi-language stuff! To use, add a tag with the <code>Name<\/code> set to TRACKSETEX, and the <code>String<\/code> value set to: <code>EditionUID VideotrackUID AudiotrackUID SubtitletrackUID Three-letter_language_code Optional_name<\/code><br \/>\nNo <code>Target<\/code> is necessary. Setting any of the track UID&#8217;s to &#8220;.&#8221; (a dot, full stop, whatever; without quotes) means &#8220;don&#8217;t change&#8221;, setting the subtitle track UID to &#8220;x&#8221; (lowercase letter X; again without quotes) means &#8220;disable subtitles&#8221;. When the edition given by the EditionUID is activated, the given set of tracks will be chosen. The track UID&#8217;s can be found with mkvinfo. If you&#8217;re too lazy for that you can also just specify track numbers if you say # before the number.<br \/>\nExample for the same sample as before: <a href=\"http:\/\/mod16.org\/samples\/tags-trackset-example.xml\">tags-trackset-example.xml<\/a><\/p>\n<p>Note that since you can&#8217;t remux a file without changing the SUID, it might be worth to know that there&#8217;s a feature in mmg&#8217;s chapter editor that allows you to write chapters straight into an MKV file <em>without<\/em> remuxing it; it can be found under the Chapter Editor menu -&gt; Save to Matroska file. Don&#8217;t do it to a file that already has chapters or you may get weird issues though.<br \/>\n<em>Update:<\/em> This is slightly incorrect; Haali&#8217;s GDSMux (comes with the splitter package and with the CCCP) lets you specify the SUID yourself, so if you use that you can remux while keeping it.<\/p>\n<h3>Caveats, Assorted Traps and Various Interesting Things to Note<\/h3>\n<ul>\n<li>Make sure all your various UID&#8217;s really are U or you&#8217;ll run into trouble. (They must at the very least be unique to the set of files\/segments you&#8217;re working with.)<\/li>\n<li>Never try to add ordered chapters to a file that already has chapters (ordered or not). Always tick &#8220;no chapters&#8221; when remuxing, just to be safe!<\/li>\n<li>Remember that the virtual timeline created by ordered chapters <em>replaces<\/em> the ordinary timeline; if there is a part of the source video that is not covered between your ChapterTimeStart\/End&#8217;s, that part will never be displayed to the user at all (might be useful for hiding easter eggs in your releases).<\/li>\n<li>On a related note, remember that all ChapterTimeStart\/End&#8217;s refer to timestamps of the underlying segment. If the underlying segment is VFR, this means you must take the timestamps <em>after<\/em> the timecodes have been applied! One method of doing this is using <a href=\"http:\/\/www.aegisub.net\">Aegisub<\/a> with timecodes loaded, another is to demux the v2 timecodes and amuse yourself by counting frames and matching them to line numbers.<\/li>\n<li>If a subtitle line spans an ordered chapters segment switch point, there will probably be artefacts and\/or other problems. So don&#8217;t do that.<\/li>\n<li>Likewise, there may be audio artefacts during such switch points under certain circumstances, so try placing the switch points where the audio is either silent or has a lot of noise going on.<\/li>\n<li>The requirement that the linked segments have the same sets of tracks and codecs used has a few caveats; similar settings has to be used as well. To be precise, the CodecPrivate header has to be the same (update: this may not be so strict after all, YMMV). Usually if something is encoded with the same settings, it will be, but your mileage may vary. See <a href=\"http:\/\/haali.cs.msu.ru\/mkv\/codecs.pdf\">Haali&#8217;s codecs list<\/a> for more information.<\/li>\n<li>Related to the above, if you want to have different editions with or without certain audio and\/or subtitle tracks, remember that you can abuse silent audio tracks and\/or subtitle tracks without any actual lines to satisfy the track number\/codec requirement.<\/li>\n<li>Still related: for the commonly used subtitle formats S_TEXT\/ASS and S_TEXT\/SSA, the entire [v4 Styles] section is stored in the CodecPrivate header, so if you use ordered chapters and softsubs, all styles you use have to be present (and identical) in all subtitle tracks.<\/li>\n<li>Since the timelines of all ordered chapters are taken directly from the underlying source timeline, to get subtitles to synch you need to make sure they synch with the &#8220;real&#8221; timeline.<\/li>\n<li>If you&#8217;re using <a href=\"http:\/\/www.x264.nl\">x264<\/a> to encode, you can force certain frames to be I-frames even if they normally wouldn&#8217;t be by manipulating the stats file, which may be interesting for handling virtual timelines efficiently. (Hint: if you seek to somewhere that isn&#8217;t an I-frame, for the seek to take effect you need to decode everything from the last I-frame to the sought frame, and virtual timelines are all internally constructed by seeking. Translation of the hint: you might get &#8220;hiccups&#8221; when going over chapter switch points if the chapter doesn&#8217;t start on a keyframe.)<\/li>\n<li>You can also do the same with <a href=\"http:\/\/www.xvid.org\">XviD<\/a> if you are so inclined, by using zones.<\/li>\n<li>While the video resolution of all the included segments has to be the same, the DAR (display aspect ratio) does not necessarily have to be. This has interesting implications for stuff that changes AR somewhere in the middle. (Player support for changing AR mid-stream may vary a lot, test thoroughly before using.)<\/li>\n<\/ul>\n<h3>Acknowledgements<\/h3>\n<p>The author would like to thank the following people and\/or entities (in alphabetical order):<\/p>\n<ul>\n<li>#darkhold<\/li>\n<li>Haali<\/li>\n<li>Harukalover<\/li>\n<li>Kaverin<\/li>\n<li>Nicholi<\/li>\n<li>Phar<\/li>\n<li>Unearthly<\/li>\n<\/ul>\n<p>Without them, this guide probably wouldn&#8217;t be here.<\/p>\n<h3>References<\/h3>\n<p>None.<\/p>\n<p>Or, OK, well, if two or three doom9 forum posts by Haali and some random #darkhold logs count as references then I guess&#8230;<\/p>\n<p>See <a href=\"http:\/\/forum.doom9.org\/showthread.php?t=99489\">http:\/\/forum.doom9.org\/showthread.php?t=99489<\/a> for the thing that started it all.<br \/>\nSee the <a href=\"http:\/\/bunkus.org\/videotools\/mkvtoolnix\/docs.html\">mkvmerge documentation<\/a> for information on the XML chapter and tags file formats.<br \/>\nSee <a href=\"http:\/\/haali.cs.msu.ru\/mkv\/\">Haali&#8217;s page<\/a> for the TRACKSET\/TRACKSETEX details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today on the Fluffy Channel: magic tricks with the Matroska container. Namely, editions, file linking and ordered chapters. (Warning: very large amounts of techobabble walls of text ahead.) These are easily some of the most (if not the most) obfuscated features of the format; a few people know how to use them (and it has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[22],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-encoding","tag-encoding"],"_links":{"self":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":1,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}