W A R N I N G !

W A R N I N G !

This page is full of non-facts and bullsh!t, (just like the internet and especially forums and other blogs), please do not believe entirely without exercising your intellect. Any resemblance to real things in reality is purely coincidental. You are free to interpret/misinterpret the content however you like, most likely for entertainment, but in no case is the text written on this blog the absolute truth. The blog owner and Blogger are not responsible for any misunderstanding of ASCII characters as facts. *cough* As I was saying, you are free to interpret however you like. *cough*

Sunday, August 2, 2009

Splitting/cutting MKV files - finally!

For very long, there wasn't anything useful on this page. Which went against my philosophy of blogging. Which is to write information that cannot be found elsewhere, and hopefully be useful.

One day I decided to face again a problem which I've had for years and finally found a solution for it. It was such a long problem because nobody had written a solution that could be found by me. Ironically it would've been so easy for regular encoders/muxers that they wouldn't be bothered to write a guide for it.

So here I am, writing the solution for people who did not have a chance to come across it yet.

By split I mean extracting out a segment of the video from the MKV file, with subtitles, multiple audio tracks and whatever that can be stuffed into an MKV container, extract it into another MKV file (for proper H.264, subtitles support), without reencoding. Not Haali Media Splitter mind you. Those are DirectShow splitters.

We need 3 (or 4) programs for this -

- AVISynth
- AVISynthesizer (optional)
- VirtualDub/VirtualDubMod
- MKVtoolnix w/mkvmerge GUI (again, the GUI part is optional, but only if you're super hardcore programmer)

With some skills you can probably replace AVISynth to VirtualDub with Windows Media Player, but we're looking at accuracy and reliability here.

This is what you see when you open the file in mkvmerge GUI.

Go to the Global tag.

This is where you insert the timecodes.

Frankly, I don't know how to explain what timecode is. But frankly, if you didn't know what timecode is and cannot understand it just by looking, you shouldn't attempt in any form of video encoding or editing.

What will happen is that mkvmerge will split the file into three segments - the part before what you want, the part of what you want, and the part after what you want. You can delete the part before, and abort muxing the part after, hence leaving just the part you want.

Now... where to get the timecodes?

That's where VirtualDub comes in:

This is the start of the segment that I want

This is the end - take down the timecodes. Take note - mkvmerge requires the timecode to be in HH:MM:SS.nnn format, unlike H:MM:SS.nnn in VirtualDub. Change accordingly.

But you cannot just open any file in VirtualDub. That's where AVISynth (and AVISynthesizer) comes in handy. You can find guides on AVISynth easily, and I have a guide on AVISynthesizer here but the image has been deleted by ImageShack - do not use ImageShack for any long-term image hosting.

So there you go - open the file in VirtualDub, search for the start and end of what you want, but instead of clicking Save as AVI write down the timecodes of the frames (or the frame before that frame - see below)

Issues on keyframes

As you seasoned cutters/joiners know, Direct Stream Copy (or no reencoding) means the video has to start on a keyframe. If you start on a non-keyframe, either the starting is skipped to the next keyframe, or pushed forward to the previous keyframe, depending on the program.

mkvmerge uses the first keyframe after whichever frames you selected as the start and end of the segment. Even if that frame happens to be a keyframe also (? - not sure, needs to be verified), which people would instinctively select. i.e.

Example -

I select frames 100-500.
Frames 100, 200 and 600 are keyframes.
The segment starts from frame 200 and ends on 600.

It's dumb, I know. But this thing isn't meant to be a cutter in the first place.

So instead of selecting frame 100, start with the frame before it e.g. frame 99 instead.

Now how to know which are the keyframes - since all frames show up as keyframes in VirtualDub - which is correct because AVISynth feeds it uncompressed AVI

Use the Next scene seek function/button in VirtualDub - it's just a scene change detector, but most of the time a keyframe will lie where a scene change is, so it's a good guide. I've seen exceptions however, especially when fading in/out.

Do not use Prev scene seek unless you want to wait for eternity.

Known bugs

Apart from the above mentioned (which isn't technically a bug), there may be some syncing problems at the start of the segment, due to the way MKV works as opposed to AVI. Goes away after a few frames however.


ZOMBiE CyGiG said...

Im not sure, but I think the latest VLC player has these features.

ZOMBiE CyGiG said...

I found the link

Guess it isnt exactly what u wanted after all.