#!/bin/bash
###############################################
# shindig.sh 0.1
# This little script converts chapter times
# exported from Metadata Hootenanny into a
# format readable by the mkvtoolnix utilities
###############################################
# It accepts one command-line argument, the
# path to the Hootenanny output .txt to read
###############################################
INPUTFILE="$1"
#############
# SED STUFF
#############
# First, ignoring the extra font info for QT on Hootenanny's first line, as
# well as the chapter title lines, chop off [ and ] from the timecode lines,
# and store them in the TIMECODE array for later use.
#------------------------------------------------------------------------------
# Also, the timecodes need 0 appended, because Matroska expects one more
# digit of precision than .mov does.
TIMECODE=($(cat $INPUTFILE | sed -e 's/\[//' -e 's/\]/0/' -e '/^Chapter*/ d' -e '/{QT*/ d'))
# Then, ignoring the extra font info for QT on Hootenanny's first line, as
# well as the timecode lines, store the titles in TITLE for later use.
TITLE=($(cat $INPUTFILE | sed -e 's/\(Chapter*\)/\1/' -e '/^\[/ d' -e '/{QT*/ d'))
#############
# LOOP PREP
#############
# For the timecode index and the loop.
ITERATOR1=0
# For the chapter title index.
ITERATOR2=0
# We want to iterate through based on the number of timecodes stored
# in the TIMECODE array.
CHAPTERNUM=$(echo ${#TIMECODE[*]})
# And yet, we don't want the last chapter marker from Hootenanny,
# because it's just the last frame of the video. So decrement.
CHAPTERNUM=$((CHAPTERNUM-1))
# This xml header text only needs to be at the top, so
# throw it in before the loop starts.
echo -e "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
echo -e "<!DOCTYPE Chapters SYSTEM \"matroskachapters.dtd\">"
echo -e "<Chapters>"
echo -e "<EditionEntry>"
#######################################
# LOOPING, WHERE THE REAL WORK'S DONE
#######################################
# Loop through all the chapters.
while [ $ITERATOR1 -lt $CHAPTERNUM ]
do
echo -e "<ChapterAtom>"
echo -e "<ChapterTimeStart>"
# The \t is just so the output is a little clearer.
# Can't seem to get this stuff to all output on one
# line, so tabs at least make it legible.
echo -e "\t" ${TIMECODE[$ITERATOR1]}
echo -e "</ChapterTimeStart>"
echo -e "<ChapterDisplay>"
# Timecode written, it's on to the title.
echo -e "<ChapterString>"
# Again, a tab for legibility.
# Then, two words from the title array.
echo -e "\t" ${TITLE[$ITERATOR2]} ${TITLE[$ITERATOR2+1]}
echo -e "</ChapterString>"
echo -e "<ChapterLanguage>eng</ChapterLanguage>"
echo -e "</ChapterDisplay>"
echo -e "</ChapterAtom>"
# Iterate the timecodes and the loop by one.
ITERATOR1=$((ITERATOR1+1))
# Because Hootenanny uses two-word title names,
# and each word is stored separately in the array,
# iterate it by two to jump past both words used
# this time through the loop.
ITERATOR2=$((ITERATOR2+2))
done
#############
# POST LOOP
#############
# This is just to close out the xml divs.
echo -e "</EditionEntry>"
echo -e "</Chapters>"