Shell script to enumerate titles and chapters of a dvd and rip the longest one into avi using mencoder

BASH script to enumerate titles and chapters of a dvd and rip the longest one into 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 all titles and chapters, try this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# script to identify and rip the longest title of a dvd into 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
ak@dt:~$ cat /usr/local/bin/dvd2xvid-longest 
#!/bin/bash
# script to identify and rip the longest title of a dvd into 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 1>&2 "\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 1>&2 "\n Error!\n Can't find $ddir \n"
  exit 1
fi
 
log="dvd2xvid-longest.log"
 
# remove log if exists
if [ -f $log ]; then
   echo -e 1>&2 "\n Removing existing $log.."
   rm $log || err_exit
fi
 
# test lsdvd
lsdvd -x $1 1> /dev/null || err_exit
 
# get longest track
title=$(lsdvd -x $1 2> /dev/null | grep "Longest track" | awk '{print$3}')
 
# get longest chapter of the longest title
chapter=$(lsdvd -c -t$title $1 2> /dev/null | sed 's/^\s\+//' | grep "Chapter:" | sort -k4r | head -1 | awk -F'Chapter: ' '{print$2}' | awk -F',' '{print$1}')
 
# log date
echo -e "$(date)" >> $log || err_exit
 
# real work starts here
rm -f divx2pass.log frameno.avi || err_exit
# define source
SRC="dvd://$title -chapter $chapter -dvd-device $1 -alang en"
# audio pass
echo -e "\n\n$(date +%H:%M:%S) started apass" >> $log || err_exit
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.longest.track.avi" || err_exit
 
# log more stuff
echo "$(date +%H:%M:%S) done" >> $log || err_exit
echo -e "\n\n\n" >> $log || err_exit
echo -e "\nLongest track is $title"  >> $log || err_exit
echo "Longest chapter is $chapter"  >> $log || err_exit
echo -e "\n########## lsdvd output ##########" >> $log || err_exit
echo "$(lsdvd -x $1)" >> $log || 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).