Skip to content

Instantly share code, notes, and snippets.

@RamKromberg
Created July 11, 2021 04:49
Show Gist options
  • Save RamKromberg/e1028c41882c68cc22c2e6d7246c34e0 to your computer and use it in GitHub Desktop.
Save RamKromberg/e1028c41882c68cc22c2e6d7246c34e0 to your computer and use it in GitHub Desktop.
convert lsdvd output to chapters file for mkvtoolnix using bash and bc
#!/usr/bin/env bash
#for nixos users:
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash bc lsdvd
#lsdvd2chapters.sh: lsdvd wrapper for mkvtoolnix
#2021-07-11 08:00:00 GMT+2
#
#lsdvd2chapters.sh [input_dir] [output] [title]
#input_dir: path to VIDEO_TS. default: ./
#output: path to output file. default: stdout.
#title: which title we process. default: 01.
#
#Depends on bash, bc and lsdvd.
#Public Domain
#TODO: safely handle arguments, lsdvd etc edge cases.
dir=${1:-./}
out=${2:-/dev/stdout}
title=${3:-01}
sum=0
tab=' '
wtitle=""
while IFS=$'\ ' read -r -a myArray
do
case "${myArray[0]}" in
'Disc')
echo Disc: "${myArray[2]}" 1>&2
;;
'Title:')
wtitle="${myArray[1]%,}"
echo Title: $wtitle 1>&2
;;
$tab'Chapter:')
if [ "$title" = "$wtitle" ]; then
sum="$(date -u -d "jan 1 1970 ${myArray[3]%,}" +"%s.%3N") + $sum"
secs="$(echo $sum|bc)"
floored=${secs%%.*}
h=$(( floored / 3600 ))
m=$(( ( floored / 60 ) % 60 ))
s=$(( floored % 60 ))
ns=${secs#"$floored."}
echo -n CHAPTER"${myArray[1]%,}"=
printf "%02d:%02d:%02d.%s\n" $h $m $s $ns
echo CHAPTER"${myArray[1]%,}"NAME=
#else
fi
;;
*)
#echo "${myArray[@]}"
#echo "${myArray[0]}"
;;
esac
done < <(lsdvd -c "$dir" 2>/dev/null) > "$out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment