+#!/bin/bash -e
+
+if [ -z "$1" -o -z "$2" ]; then
+ echo "Usage: $0 videodir chapterdir destdir"
+ exit 1
+fi
+
+videodir="$(cd "$1" && pwd)"
+chapterdir="$(cd "$2" && pwd)"
+destdir="$(cd "$3" && pwd)"
+
+cd "$videodir"
+find . -type f | while read infile; do
+ if [ ! -r "$infile" ]; then
+ echo "Missing file $infile"
+ exit 1
+ fi
+
+ outfile="${destdir}/${infile}"
+ if [ -e "$outfile" ]; then
+ echo "Skipping $infile, it already exists in destination directory!"
+ continue
+ fi
+
+ chapterfile="${chapterdir}/${infile}.chapters"
+ if [ ! -e "$chapterfile" ]; then
+ echo "Skipping $infile, no chapters!"
+ continue
+ fi
+
+ mkvmerge \
+ -o "$outfile" \
+ --chapters "${chapterfile}" \
+ --no-chapters \
+ "$infile"
+done
+