Chapter Renumber mkv

# Updates the chapter names in the .mkv metadata so that the chapter strings 
# (the name of the chapter) match chapter number. 
# This fixes mismatches that can occur during an append, like so:
#
#   CHAPTER01=00:00:00.000
#   CHAPTER01NAME=Chapter 07
#
# If the string is a chapter, it remains unchanged
# If the string it a chapter string, it changes the end to match the chapter number
# Anything else is removed

CHAPTERS=`mkvextract chapters -s $1`
NEWCHAPTERS=""

IFS=$'\n'

for X in $CHAPTERS; do 
  # CHAPTER01=*
  if [[ $X =~ ^CHAPTER[[:digit:]]*= ]];then
    NEWCHAPTERS+=$X$'\n'
  # CHAPER01NAME=*
  elif [[ $X =~ ^CHAPTER[[:digit:]]*NAME=.* ]];then
    Y=`echo $X | sed 's/\(^CHAPTER\(.*\)NAME=\).*/\1Chapter \2/'`
    NEWCHAPTERS+=$Y$'\n'
  fi
done

echo "$NEWCHAPTERS" > newchapters

mkvpropedit test.mkv --chapters newchapters
Comments