Remove Intro and Otro from MKVs

Let's suppose you want to remove the intro from a directory of MKVs.

There isn't a trim function exactly, but one can use the split parts feature of mkvmerge. This feature specifically keeps just the parts you want, rather than making a mess with multiple outputs as the other variants of split do.

Times

Let's suppose you want to remove the first 45 seconds. Normally you'd have to specify TIMSTAME-TIMESTAMP, but in this case you can leave off the ending time.

# Delete the first 45 seconds
for X in *.mkv;do
    mkvmerge -o ${X/.mkv/.trim.mkv} --split parts:45s- "$X"
done

Chapters

If you're lucky, your source has chapters. This can save you from guessing where things begin and end at. You can't put chapters directly into --split parts, but you can pull out the timestamps for them using mkvextract

# Pull out the section from chapter 2 thru chapter 4
for X in *.mkv; do
    START=`mkvextract "$X" chapters --simple - | grep CHAPTER02= | cut -d '=' -f 2`
    END=`mkvextract "$X" chapters --simple - | grep CHAPTER04= | cut -d '=' -f 2`
    mkvmerge -o "${X/.mkv/.trim.mkv}" --split parts:$START-$END "$X"
done


Sometimes this leaves the chapters our of order.

Splitting only happens on key-frames and those don't always line up on the precise moment you want to cut. In those cases, you'll have to encode with a start and end time.

Comments