Trim MKVs

Let's suppose you want to crop the first 1:30 out of a directory of files. There isn't a trim function exactly, but one can use the split parts feature of mkvmerge.

mkvmerge -o test.mkv --split parts:130s- inputFile.mkv

We used 130s to designate the start position at 130s in, and the '-' to indicate the end of the file. One can also trim off the end by supplying an end time code.

Put that in a loop, and you're good.  

IFS=$(echo -en "\n\b")
for X in `ls *.mkv`; do Y=`echo $X|sed 's/\.mkv/.new.mkv/'`;mkvmerge -o "$Y" --split parts:130s- "$X" ;done

Splitting based on chapters isn't as convenient, but when the split time is variable but marked by chapters, it's the way to go. You can only split the files, but that's workable.  The output parts are auto-named, but tools input validation doesn't know that and you have to differentiate them anyway, so we put the output in /tmp

Let's say you want to leave off the credits, in chapter 6, and you're already in the directory with the files to be operated on

for X in `ls *.mkv`; do ~/bin/mkvmerge -o "/tmp/$X" --split chapters:6  "$X";done
for X in `ls /tmp/*-001.mkv`; do Y=`echo $X|sed 's/-001\.mkv/\.mkv/' | sed 's/\/tmp\//\.\//'`;mv $X $Y;done
rm /tmp/*.mkv


Comments