#!/bin/bash
SCRIPT=`basename "$0"`
MKVEXTRACT=`which mkvextract`
MKVMERGE=`which mkvmerge`
outdir="./"
series=00
if [ "$1" == "" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "$SCRIPT: Give MKV to chapter-split as an argument, optionally an output dir, and optionally series number"
echo "$SCRIPT: e.g.: ripped_season_03.mkv ./ 3"
echo "$SCRIPT: e.g.: ripped_music_videos.mkv ~/Music_Clips/"
exit 1
fi
if [ ! -f "$1" ]; then
echo "$SCRIPT: MKV file not found [$1]"
exit 1
fi
if [ "$2" != "" ]; then
outdir="$2"
if [ ! -d "$outdir" ] || [ ! -x "$outdir" ] || [ ! -w "$outdir" ]; then
echo "$SCRIPT: The output directory has a problem ($outdir)"
echo "$SCRIPT: Not existing, not writable, not a dir, etc."
exit 1
fi
fi
if [ "$3" != "" ]; then
series="$3"
### Check it's 2 digits, or make it so
if [ ${#series} -gt 2 ]; then
series=`echo $series | cut -b 1-2`
elif [ ${#series} -lt 2 ]; then
series="0$series"
fi
echo "$SCRIPT: Series number is [$series]"
fi
if [ "$MKVEXTRACT" == "" ] || [ ! -x "$MKVEXTRACT" ]; then
echo "$SCRIPT: 'mkvextract' seems to be missing"
echo "Is MKVToolsNix Installed?"
echo "sudo apt-get install mkvtoolsnix"
exit 2
fi
if [ "$MKVMERGE" == "" ] || [ ! -x "$MKVMERGE" ]; then
echo "$SCRIPT: 'mkvmerge' seems to be missing"
echo "Is MKVToolsNix Installed?"
echo "sudo apt-get install mkvtoolsnix"
exit 2
fi
infile="$1"
timecodes=`"$MKVEXTRACT" chapters -s "${infile}"|grep "CHAPTER[0-9][0-9]="|sed '1,1d;:a;N;$!ba;s/CHAPTER[0-9][0-9]\=//g;s/\n/,/g'`
if [ "$series" == "00" ]; then
# no series number
"$MKVMERGE" --split timecodes:"${timecodes}" "${infile}" -o "${outdir}/${infile%.mkv}_%02d.mkv"
else
"$MKVMERGE" --split timecodes:"${timecodes}" "${infile}" -o "${outdir}/${infile%.mkv} S${series}E%02d.mkv"
fi
Mosu enhanced the --split function of mkvmerge by a chapter option.
Splitting before specific chapters.
Syntax: --split chapters:all or --split chapters:A[,B[,C...]]
Example: --split chapters:5,8
The parameters A, B, C etc must all be positive integers. Numbering starts at 1. The list of chapter numbers is separated by commas. Splitting will occur right before the first key frame whose timecode is equal to or bigger than the start timecode for the chapters whose numbers are listed. A chapter starting at 0s is never considered for splitting and discarded silently.
The keyword all can be used instead of listing all chapter numbers manually.
I used "mkvmerge -o ShallWeDancesplit.mkv --split chapters:2 ShallWeDance.mkv" and ended up with two files... ShallWeDancesplit-001.mkv which was that annoying 90 second intro and ShallWeDancesplit-002.mkv which was the rest of the movie in one piece.
Tried the new feature, and it's a lot more difficult than running one of the scripts provided by posters above.
Since MakeMKV is a program with a very intuitive and simple GUI, it makes no sense to learn complicated commands just to split a DVD by chapters. It'd be much easier to check a box in the GUI somewhere.
Music video and cartoon compilation DVDs are very common, so this is a much needed feature.
fryk. wrote:MKV split by chapters
Here is a little batch script for the windows command processor. Copy the code in your text editor and save it as 'SplitMKV.CMD' in your MKVToolNix program folder. The script can handle up to 400 chapters in a single MKV file (XP or better). It will search & find all the chapters, and split the MKV file on every chapter time mark. Usage: 'SplitMKV MyVideo.MKV'.
@echo off &setlocal EnableDelayedExpansion
set source=%~1
set destination=output\%~nx1
if not exist output\ mkdir output
for /f "usebackq tokens=4" %%i in (`mkvinfo --ui-language en "%source%" ^| findstr /i /c:"ChapterTimeStart: "`) do (
if defined timecodes set timecodes=!timecodes!,
set timecodes=!timecodes!%%i
)
mkvmerge -o "%destination%" --split timecodes:%timecodes% "%source%"
endlocal