#!/bin/bash
# (c) Aaron Whitehouse 2013, v1.0
# Distributed under the GPLv3+
# Requires mplayer (tested with MPlayer svn r34540 (Ubuntu)). 
# Extracts the titles or chapters from an iso of a DVD
if [ $# != 2 ]; then
  echo "Call this with the desired iso file and if you want 'titles' extracted or 'chapters'"
  echo "vob-from-iso mydvd.iso titles"
  echo "or vob-from-iso mydvd.iso chapters"
  exit
fi
TitleLine=$(mplayer dvd://99 -identify -dvd-device $1 | grep "ID_DVD_TITLES")
No_Titles_Position=$(echo `expr index "$TitleLine" =`)
No_Titles=${TitleLine:$No_Titles_Position}
echo "there are $No_Titles titles in this file"
# For each title
for ((title=1; title <= No_Titles ; title++))
do
  if [ $2 == "chapters" ]; then
    # Extract chapters
    ChapLine=$(mplayer dvd://99 -identify -dvd-device $1 | grep "ID_DVD_TITLE_"$title"_CHAPTERS")
    No_Chap_Position=$(echo `expr index "$ChapLine" =`)
    No_Chap=${ChapLine:$No_Chap_Position}
    echo "Title $title has $No_Chap Chapters"
    # And for each chapter within each title
    for ((c=1; c <= No_Chap ; c++))
      do
      mplayer dvd://$title -dvd-device $1 -chapter $c-$c -dumpstream -dumpfile $1"_"$title"_"$c.vob
    done
  elif [ $2 == "titles" ]; then
    mplayer dvd://$title -dvd-device $1 -dumpstream -dumpfile $1"_"$title".vob"
  fi
done
exit

