Shell script to enumerate titles and chapters of a dvd and rip each into a separate avi using mencoder

BASH script to enumerate titles and chapters of a dvd and rip each into a separate avi using mencoder. DVD device can be a path to mounted iso (e.g. /mnt/iso/) or a real device (e.g. /media/cdrom0/).

To rip just the longest title, try this.

#!/bin/bash
# script to enumerate titles and chapters of a dvd and rip each into a separate xvid avi
# dvd-device can be a path to mounted iso (e.g. /mnt/iso/) or a real device (e.g. /media/cdrom0/)
# expects dvd as $1 and dvd name as $2

# error handling
function err_exit { echo -e 1>&2; exit 1; }

# Confirm two arguments were passed
if [ $# -ne 2 ]; then
  echo -e "\n Usage error! Need two arguments.\n Example: $0 dvd-device My.DVD\n"
  exit 1
fi

# Check if VIDEO_TS exists
ddir=$(echo "$1/VIDEO_TS" | sed 's_/\+_/_')
if ! [ -d "$ddir" ]; then
  echo -e "\n Error!\n Can't find $ddir \n"
  exit 1
fi

log="dvd2xvid-all.log"
tmp="/tmp/dvd.title.chapter.list"

# remove log if exists
if [ -f $log ]; then
   echo -e "\n Removing existing $log.."
   rm $log || err_exit
fi

# test lsdvd
lsdvd -x $1 1> /dev/null || err_exit

# create Title and Chapter list
lsdvd -x $1 | grep "^Title" > $tmp || err_exit

# log date
echo -e "$(date)" >> $log || err_exit

# outer loop goes through titles
for title in $(cat $tmp | grep -Eo "^Title: [0-9]{2}" | awk '{print$2}'); do 
  chapters=$(cat $tmp | grep "^Title: $title" | grep -Eo "Chapters: [0-9]{2}" | awk '{print$2}')
    # inner loop goes through chapters for each title
    for chapter in $(seq 01 $chapters); do 
      rm -f divx2pass.log frameno.avi || err_exit 
      echo -e "\n\n\n$(date +%H:%M:%S) Processing Title $title, Chapter $chapter" >> $log
      # real work happens here
      SRC="dvd://$title -chapter $chapter -dvd-device $1 -alang en"
      # audio pass
      echo -e "\n$(date +%H:%M:%S) started apass" >> $log
      nice -n 3 mencoder -oac mp3lame -lameopts mode=2:cbr:br=96:vol=0 \
      -ovc frameno -o frameno.avi $SRC || err_exit && \
      # video pass 1
      echo -e "\n\n$(date +%H:%M:%S) started vpass1" >> $log || err_exit
      nice -n 3 mencoder -sws 2 -oac copy -ovc lavc \
      -lavcopts vcodec=mpeg4:vbitrate=2000:vhq:vpass=1 \
      -ffourcc XVID $SRC -o /dev/null || err_exit && \
      # video pass 2
      echo -e "\n\n$(date +%H:%M:%S) started vpass2" >> $log || err_exit
      nice -n 3 mencoder -sws 2 -oac copy -ovc lavc \
      -lavcopts vcodec=mpeg4:vbitrate=2000:vhq:vpass=2 \
      -ffourcc XVID $SRC -o "$2.Title$title.Chapter$chapter.avi" || err_exit
    done
done

# log more stuff
echo "$(date +%H:%M:%S) done" >> $log || err_exit
echo -e "\n\n\n########## lsdvd output ##########" >> $log || err_exit
echo "$(lsdvd -x $1)" >> $log || err_exit

rm $tmp || err_exit

1 Comment

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).