Fun with splitting mkvs

So I had gotten my hands on a lot of DVDs of Caillou for my son. I didn’t had much time so I made a 1:1 copy of the Video_TS folder and as I don’t want to waste space I ran them through handbrake, outputting a single .m4v file for each DVD. I thought it would be easier to watch for him on the iPad then. But I didn’t think that at one point in the future I want to use the XBMC library. So I spend a couple of hours trying to figure out how to manually insert a TV show. I was getting to the point where the show would show up and every episode I had a custom .nfo file for. But the thumbnails weren’t working. Ok so I needed a better solution, split those damn video files into individual episodes and rename it so that XBMC can hopefully add it to the library.

I should have made this right away in Handbrake but at that time the feature was still experimental, even though it does work – I tested it later with another series. Anyway, splitting a mkv is easy when you have the chapter timezones. But first we need an mkv file and not an m4v – because mkvtoolnix doesn’t split those. Don’t ask google, it will just point you to some malware.

mkvtoolnix can remux m4v to mkv. So with a quick for loop in bash that was done, alright!


#!/bin/bash

for i in {01..22}
do
echo `./split.sh Caillou.S01E$i.mkv`
done

Hooray, first step done. So now to analyze those Chapter timezones:

Mhm the output looks like this:

$ ./mkvinfo Caillou.S01E01.mkv | grep ChapterTimeStart
| + ChapterTimeStart: 00:00:00.000000000
| + ChapterTimeStart: 00:04:08.200000000
| + ChapterTimeStart: 00:12:14.080000000
| + ChapterTimeStart: 00:16:22.280000000
| + ChapterTimeStart: 00:24:28.080000000
| + ChapterTimeStart: 00:28:36.200000000
| + ChapterTimeStart: 00:36:42.080000000
| + ChapterTimeStart: 00:40:50.280000000
| + ChapterTimeStart: 00:48:56.120000000
| + ChapterTimeStart: 00:53:04.200000000
| + ChapterTimeStart: 01:01:10.080000000
| + ChapterTimeStart: 01:05:18.240000000

So we have 12 Chapters, and one Episode is usually composed of two smaller episodes. That means we have to do something like this:

00:00:00.000000000 // 1 ep1
00:04:08.200000000 // 2 ep1
00:12:14.080000000 // 3 ep2 SPLIT
00:16:22.280000000 // 4 ep2
00:24:28.080000000 // 5 ep3 SPLIT
00:28:36.200000000 // 6 ep3
00:36:42.080000000 // 7 ep4 SPLIT
00:40:50.280000000 // 8 ep4
00:48:56.120000000 // 9 ep5 SPLIT
00:53:04.200000000 // 10 ep5
01:01:10.080000000 // 11 ep6 SPLIT
01:05:18.240000000 // 12 ep6

So we that looks reasonable, but how to format the output like this. Again cygwin to the rescue (I couldn’t use my Mac atm)


#!/bin/bash
INPUTFILE=$1

TIMELINE=`./mkvinfo $1 | grep ChapterTimeStart | cut -d ' ' -f 6 | awk 'NR%2' | awk 'ORS=","' | cut -d, -f2-`

echo "${TIMELINE%?}"

./mkvmerge -o c$1 --split "timecodes:${TIMELINE%?}" $1

Now just call this using the for loop from before and bam, all files are getting splitted. The first three DVDs look great, but then for some reason the other DVDs only get split into 3 files instead of 6, what happened? Well the bastards didn’t put in 12 Chapters anymore! So my whole formatting of the output was for naught. Now I still have to manually inspect every file to see how many chapters there and then figure out where to split. I would have probably spend less time doing this right from the start than trying with my feeble bash scripting skills to be cool. 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *