Tageac3to

Apply delay to MKV/eac3to chapter files

Small bash script to apply a delay to MKV/eac3to chapter files (CHAPTERXX=/CHAPTERXXNAME= format):

#!/bin/bash
# usage: ./chapshift.sh "+0.792 seconds" < <input> > <output>
set -o errexit -o noclobber -o nounset -o pipefail

date_offset="$1"

apply_delay() {
    date --date="$1 $date_offset" +%T,%N | cut -c 1-12
}

while read -r origline
do
    line=`echo ${origline} | sed 's/\=/\=\ /g' | sed 's/\n//g'`
    if [[ $line =~ ^CHAPTER[0-9][0-9]=\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9] ]]
    then
        read -r chapter start_date <<<"$line"
        if [[ $start_date =~ 00:00:00.000.* ]]
        then
            new_start_date="00:00:00.000"
        else
            new_start_date="$(apply_delay "$start_date")"
        fi
        chapter=`echo ${chapter} | sed 's/=\ /=/g'`
        new_start_date=`echo ${new_start_date} | sed 's/\,/\./g'`
        printf "%s%s\n" "${chapter}" "$new_start_date"
    else
        printf "%s\n" "$origline"
    fi
done