This Does Not Compute

Technology, gaming, music and things that just don't compute

Undoing Ordered Chapters in MKV Files on OS X

…aka “fixing fansubbed anime files so you can play them properly.”

About 99% of fansubbed anime is distributed as MKV, or Matroska, video files. MKV is just a container, like MP4 or MOV, and the video, audio and subtitle tracks can be any format inside that container. One of the unique features of MKV files is that they support segment linking, often called “ordered chapters” in fansub groups.

Think of the structure of a typical show. There’s usually a prologue or teaser, the opening sequence, the main body of the episode, then the closing credits (and sometimes a “next episode” trailer afterwards). The opening and credits (commonly referred to as the OP and ED) are, most of the time, the same for every episode. What segment linking (ordered chapters) allows fansubbers to do is to put the OP and ED in separate files, and not include them in the episode files themselves. The video player plays the teaser from the episode file, then looks to the OP file to play the opening, then back to the episode file to continue on, all seamlessly.

This makes a lot of sense, in that a good chunk of space can be saved by only including the data for the opening and credits just once for the whole set of episodes. This reduces download time, as well as how much space that batch of files takes up.

The problem is that not all players can support ordered chapters. In fact, VLC only started supporting it relatively recently, and a lot of other common players and converters (like the PlayStation 3 and Handbrake) either barf at the file or skip the links altogether. The bummer is that there’s no easy way to reassemble the separate episode, OP and ED files into a single file. I was fearing I’d have to do a bunch of manual video editing in order to fix some files using ordered chapters so I could run them through Handbrake, until I ran across a slick Perl script written by someone who dislikes ordered chapters as much as I do.

Problem is, the script was written with Linux (specifically, Linux Mint Debian edition) in mind — which is great for Linux users (and indeed, the script works great in a LMDE virtual machine I fired up to test it out), but not so useful for those of us running Mac OS X. However, all of the software that the script depends on to work has been ported to OS X, so it’s just a matter of putting all the pieces together. That’s what I managed to figure out.

The (free) software you’ll need to make all this magic happen:

  • The UnlinkMKV script by Garret Noling (click the Downloads tab, then “Download as zip”)
  • MacPorts (get the package installer appropriate for your version of OS X)
  • XCode (from the Mac App Store; for older versions of OS X, it’s on the Mac OS X installation disc)

And here’s how you rig it all up:

  1. Download and save UnlinkMKV to someplace convenient.
  2. Download and install MacPorts.
  3. Install XCode (if you’re getting it from the Mac App Store, start the download before you go to bed — if you’re lucky, it’ll be done in the morning). If you’re installing it off the OS X disc, also check the box to install the command-line tools.
  4. Launch XCode and accept the EULA. If you got XCode from the Mac App Store, proceed to step 5. Otherwise, skip to step 6.
  5. Go to the XCode menu, then Preferences, then the Downloads tab. Click on Components, then click Install next to Command Line Tools. Get yourself something to drink as this will take a while.
  6. Close XCode.
  7. From the Finder, go to the Go menu, then Utilities. Launch Terminal.
  8. Enter the command sudo port install mkvtoolnix and go watch some YouTube or something while it cranks through all the software it has to download and compile.
  9. When it’s done, enter the command sudo port install p5-12-xml-libxml and go watch a little more YouTube (this one won’t take as long).
  10. Open up the unlinkmkv file that you downloaded in a text editor, like TextEdit.
  11. Find the line that starts with my $toolpath and change the part in quotes after it to “/opt/local/bin”. (This path is where the script will look for the tools we just installed in Terminal.) Save the change and close the file.
  12. Ready to rock! Copy the unlinkmkv script and the files you want to convert (don’t forget the OP and ED files too) into a folder, then in Terminal, navigate to that folder. The easiest way to do this is to type the command cd followed by a space, then from the Finder, drag the folder onto the Terminal window. Terminal will automatically fill in the path to that folder, so you just need to press Return afterwards.
  13. The script works against the files one at a time. Run it by entering the command perl unlinkmkv [filename] where [filename] is the name of the file to convert.
  14. If all goes well you’ll see a bunch of text go flying by and, about a minute later, you’ll have a new file in your folder with the name ending in REBUILT.

This script may not work for every file, depending on how they were encoded, but I’ve had no problems so far with the files I’ve cranked through. And if the script works for you too, go shoot Garret an e-mail and say thanks — he did all the real work here.

Advertisements

Comments are closed.

%d bloggers like this: