Updated usage for new -a option
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index bd4442a5248d841fd0b804eaab59ca5c8adab1a8..a63ca4d7b15a7477ddbacaf52a3b48641eea4a0f 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
 # Known Issues/Limitations:
 # - Mirror mode is always done in ISO mode
 #
-# Package Dependencies:
-# lsdvd dvdauthor gddrescue dvdbackup makexml tovid mencoder vlc mplayer genisoimage
+# Package Dependencies (apt-get install these for example):
+# lsdvd dvdauthor gddrescue dvdbackup tovid mencoder mplayer genisoimage libdvdcss2
+#
+# Specific Executable (program) Dependencies (must be found in $PATH):
+# volname makexml lsdvd dvdauthor gddrescue dvdbackup mencoder mplayer mkisofs
 #
 # Optional Dependencies:
 # lookup imdb info/posters for mythvideo: http://www.mythtv.org/wiki/Fill_mythvideo_metadata.pl
 #
 
-###############################################
-# Local Machine Settings - EDIT THIS SECTION
-
-# Specify the device path to the DVD drive
-dev=/dev/dvd2
-
-# Specify the path to the log directory 
-logdir=/var/log/ripdvd
-
-# Specify the path to the tmp rip directory
-tmpdir=/myth/video/DVDs/.ripdvd
-
-# Specify the email address to send job notifications to
-mailto=ajp@pippins.net
-
-# Setup the path to the tools needed by this ripper
-PATH=/etc/mythtv/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/bin:.:$PATH
-
-# Use mplayer dumpstream to create the main VOB file
-# If set to 0, cat and other utils will be used to make it instead.
-# Using mplayer dumpstream is by far the best way to create the main VOB file
-# The only thing this doesn't work for are DVDs with the 99 title copy protection.
-# The script will autodetect these types of disks, and change this to 0 if necessary.
-# If this is set to 0, other methods will be employed to get the DVD VOB dump.
-use_mplayer_dumpstream=1
-
-# 2-channel AVI encoding audio bit rate
-audio_bitrate=224
-
-# If set to 1, this script will call another script to fill in the mythvideo metadata
-# If you don't use mythtv, set this option to 0. If you use mythtv, and have downloaded
-# the fill_mythvideo_metadata.pl script shown above, you can leave this option set to 1.
-fill_mythvideo_metadata=1
-
-# If the 99 titleset copy protection scheme is detected, trust the autodetection or abort.
-# The autodetection may work, but it probably won't in this case. Set to 0 by default. 
-# Override to 1 if you want the script to wing it, and hope it picks the right feature title.
-# Otherwise, you will need to rerun the script providing the feature title with the -t option.
-trust_feature_title_autodetect_when_uncertain=0
+##############################################################################
+# Local Machine Settings:
+# Sources both the "default" conf file tracked by GIT (rip_dvd.conf.dist)
+# and the local conf file created by each local machine (rip_dvd.conf)
+# Copy the rip_dvd.conf.dist file to rip_dvd.conf and edit the later.
+# This will allow you to override all the default values to meet your needs
+# in a way that won't get clobbered when you pull updates from my GIT repo.
+##############################################################################
+config="${0%/*}/rip_dvd.conf"
+[ -e "${config}.dist" ] && . ${config}.dist
+[ -e "${config}" ] && . ${config}
 
 ###############################################
 # Command line processing
@@ -70,6 +45,9 @@ typeset dest=""
 typeset isofile=""
 typeset vobfile=""
 typeset dvdpath=""
+typeset aspect=""
+typeset SCALE=""
+typeset CROP=""
 typeset -i keep_intermediate_files=0
 typeset -i make_final_dest_vob=0
 typeset -i make_final_dest_iso=0
@@ -85,7 +63,7 @@ typeset -i invalid_feature_title=0
 typeset -i feature_title_override=0
 typeset -i mplayer_dumpstream_incompatibility=0
 
-while (($#)) && getopts 2mvifkxht:n:d:b:s:t: opt "$@"
+while (($#)) && getopts 2mvifkxht:n:d:b:s:t:a: opt "$@"
 do
     case $opt in
         (n)     dvdname=$OPTARG;;
@@ -100,6 +78,7 @@ do
         (m)     mirror_mode=1;;
         (k)     keep_intermediate_files=1;;
         (t)     feature_title_override=$OPTARG;;
+       (a)     aspect=$OPTARG;;
         (w)     set -$opt;;
         (h)     show_usage=1;;
         (:)     echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
@@ -114,10 +93,11 @@ function usage() {
     echo >&2 "Revision $REV"
     echo >&2 "Options:"
     echo >&2 "   -d <destdir>  Specify the destination directory to store the ripped DVD to"
-    echo >&2 "   -n <dvdname>  Specify what the name of the DVD is to process from $tmpdir"
-    echo >&2 "                 If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
-    echo >&2 "                 If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
-    echo >&2 "                 This is optional. If not specified, the dvd will be pulled from $dev"
+    echo >&2 "   -n <dvdname>  Specify a path to a DVD folder or file to process:"
+    echo >&2 "                 1) If this option is not specified, the DVD will be ripped from $dev"
+    echo >&2 "                 2) If dvdname exists in $tmpdir, it will be ripped as a DVD instead of $dev"
+    echo >&2 "                 3) If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
+    echo >&2 "                 4) If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
     echo >&2 "   -m            Make a mirror image of the DVD and save it as a DVD ISO file"
     echo >&2 "                 The default operation is non-mirror mode where only the main"
     echo >&2 "                 feature title will be ripped."
@@ -128,12 +108,15 @@ function usage() {
     echo >&2 "                 You must also specify the target size or bitrate using the '-s' or '-b' options"
     echo >&2 "   -s <size>     Set the target size of the AVI file in MB (ex: 700, 1000, etc)"
     echo >&2 "   -b <bitrate>  Set the bitrate desired in the AVI file in kbits/sec (ex: 1500, 2000 (default), etc)"    
+    echo >&2 "   -a <W:H>      Specify the width x height aspect ratio to scale the DVD to (only used in -x mode)"
+    echo >&2 "      <W>        If only the width is given, it will autoset the height to a value which preserves the aspect ratio"
+    echo >&2 "                 The default behavior is autoaspect mode, which preserves the original aspect, with no scaling being done"
     echo >&2 "   -2            Use 2 channel MP3 audio encoding when making an AVI file (default is 6 channel AC3)"
     echo >&2 "   -k            Keep the intermediate files (good for debugging)"
     echo >&2 "                 In -x mode, run with this option to keep the original .VOB file"
     echo >&2 "                 By default, all intermediary files are deleted. Only the final image is kept"
     echo >&2 "   -t <title>    Specify the main feature title to pull from the DVD (only required if this script can't figure it out)"
-    echo >&2 "   -w            Set the sh Execute/Verbose flag"
+    echo >&2 "   -w            Set the sh Execute/Verbose flag (causes every command to be echoed)"
     echo >&2 ""
     exit 2
 }
@@ -149,8 +132,8 @@ if [ "$dest" == "" ]; then
   usage
 fi
 
-if ([ $target_bitrate -ne 0 ] || [ $target_size -ne 0 ]) && [ $make_final_dest_avi -ne 1 ]; then
-  echo "-E- You can't specify a bitrate in non AVI file mode. You must specify '-x' when using '-b' or '-s'" | tee -a $logfile
+if ([ $target_bitrate -ne 0 ] || [ $target_size -ne 0 ] || [ "$aspect" != "" ]) && [ $make_final_dest_avi -ne 1 ]; then
+  echo "-E- You can't specify a bitrate, target_size, or aspect in non AVI file mode. You must specify '-x' when using '-b' or '-s' or '-a'" | tee -a $logfile
   usage
 fi
 
@@ -173,6 +156,22 @@ if [ $mirror_mode -eq 1 ]; then
   fi
 fi
 
+# If the aspect ratio option was specified, set the scale variable appropriately for mencoder
+if [ "$aspect" != "" ]; then
+  echo "$aspect" | grep -q "x"
+  if [ $? == 0 ]; then
+    echo "-E- You must specify the aspect option with a value whose format is W:H"
+    exit 1
+  fi
+  echo "$aspect" | grep -q ":"
+  if [ $? != 0 ]; then
+    SCALE="scale -zoom -xy $aspect"
+  else
+    SCALE="scale=$aspect"
+  fi
+fi
+
+
 # Sanity Check - Key executables
 [[ ! -x `which lsdvd` ]] && echo "-E- missing dependency: lsdvd" && exit
 [[ ! -x `which volname` ]] && echo "-E- missing dependency: volname" && exit
@@ -228,6 +227,12 @@ else
   ripdvd=0
 fi
 
+# remove bad characters from the dvdname
+dvdname=${dvdname%.} # remove trailing '.' character
+
+# make a "safe" dvdname (remove special characters)
+safedvdname=`basename "$dvdname" | sed 's/[ !&*\\$?]/_/g'`
+
 # Make sure we have a non-empty dvdname
 if [ -z "$dvdname" ]; then
   echo "-E- unable to determine dvdname"
@@ -239,9 +244,6 @@ if [ -z "$vobfile" ]; then
   vobfile="$dest/$dvdname.VOB"
 fi
 
-# make a "safe" dvdname (remove special characters)
-safedvdname=`basename "$dvdname" | sed 's/[ !&*\\$?]/_/g'`
-
 # set up some variables to hold various logfiles
 logfile="$logdir/$dvdname.log"
 passlogfile="$tmpdir/$safedvdname.log"
@@ -340,14 +342,24 @@ function make_dvd_iso_image_from_folder {
   echo "   mkisofs -dvd-video \"$src\" 2>> \"$dumplog\" | dd of=\"$dst\" obs=32k seek=0 > /dev/null 2>> $dumplog" >> "$logfile"
   mkisofs -dvd-video "$src" 2>> "$dumplog" | dd of="$dst" obs=32k seek=0 > /dev/null 2>> "$dumplog"
 
+  # set the audio languages from the iso if it exists and is non-zero in size
+  if [ -s "$dst" ]; then
+    get_feature_title "$dst"
+    get_audio_id_from_iso "$dst"
+  fi
+
   # make sure we were able to create the iso image from the folder given to us
-  if [ ! -s "$tmpdir/$dvdname.iso" ] && [ $handle_error -eq 1 ]; then
+  if [ ! -s "$dst" ] && [ $handle_error -eq 1 ]; then
     echo "-> Unable to make an iso image from the DVD folder: $dvdpath"
     echo "   Falling back to mplayer to create a main feature VOB from the folder instead: $dvdpath"
+    # remove the bad iso file
+    [[ -e "$dst" ]] && rm -f "$dst"
     # get the feature title from the DVD folder
     get_feature_title "$dvdpath"
     # create our main VOB file from the ISO
-    create_main_vob_with_mplayer "$dvdpath"
+    create_main_vob_with_mplayer "$dvdpath" 
+    # get our audio id from the VOB file
+    get_audio_id_from_vob "$vobfile"
   fi
 }
 
@@ -457,9 +469,10 @@ function create_main_vob_with_mplayer {
 function get_audio_id_from_iso {
   # Adjust our audio ID to find the english audio stream
   # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
+  iso="$1"
   aidcheck=`tempfile`
   aid=128
-  mplayer -v -endpos 0 -dvd-device "$isofile" dvd://$feature_title > $aidcheck 2>&1
+  mplayer -v -endpos 0 -dvd-device "$iso" dvd://$feature_title > $aidcheck 2>&1
   grep -q "aid: $aid" $aidcheck
   while [ $? == 1 ] && [ $aid -lt 159 ]; do
     (( aid = aid + 1 ))
@@ -498,9 +511,10 @@ function get_crop_from_iso {
 function get_audio_id_from_vob {
   # Adjust our audio ID to find the english audio stream
   # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
+  vob="$1"
   aidcheck=`tempfile`
   aid=128
-  mplayer -v -endpos 0 "$tmpdir/$dvdname.VOB" > $aidcheck 2>&1
+  mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
   grep -q "Found audio stream: $aid" $aidcheck
   while [ $? == 1 ] && [ $aid -lt 159 ]; do
     (( aid = aid + 1 ))
@@ -696,6 +710,7 @@ function check_for_mplayer_dumpstream_incompatibility {
       if [ $trust_feature_title_autodetect_when_uncertain -eq 0 ]; then
         echo "-E- Unable to determine the feature title due to the 99 title copy protection scheme" | tee -a "$logfile"
         echo "    You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
+        echo "    You can google this DVD to find out what it's feature title is, or you can play it in a conventional DVD player to find it." | tee -a "$logfile"
         invalid_feature_title=1
       else 
         echo "    Falling back to non mplayer dumpstream methods to copy the DVD" | tee -a "$logfile"
@@ -842,7 +857,7 @@ if [ $mirror_mode -eq 0 ]; then
       if [ $use_mplayer_dumpstream -eq 1 ]; then 
 
         # get our audio id from the ISO file
-        get_audio_id_from_iso
+        get_audio_id_from_iso "$isofile"
 
         # create our main VOB file from the ISO
         create_main_vob_with_mplayer "$isofile"
@@ -865,7 +880,7 @@ if [ $mirror_mode -eq 0 ]; then
         create_main_vob_with_cat
 
         # get our audio id from the VOB file
-        get_audio_id_from_vob
+        get_audio_id_from_vob "$tmpdir/$dvdname.VOB"
   
         # check for corrupted VOB start
         check_vob_for_corrupted_start
@@ -905,8 +920,8 @@ if [ $mirror_mode -eq 0 ]; then
       # Edit these as needed to suite your needs
       mencoder_general_opts="-quiet $lang_opts -passlogfile $passlogfile"
       mencoder_output_opts="-ofps 30000/1001 -ffourcc DIVX"
-      mencoder_video_filter_opts="-vf pullup,softskip,hqdn3d=2:1:2$CROP"
-      mencoder_video_encoder_opts="-ovc xvid -xvidencopts pass=%PASS:chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:bitrate=$target_bitrate:autoaspect:threads=2"
+      mencoder_video_filter_opts="-vf pullup,softskip,hqdn3d=2:1:2$CROP,$SCALE"
+      mencoder_video_encoder_opts="-ovc xvid -xvidencopts pass=%PASS:chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:bitrate=$target_bitrate:autoaspect:threads=$mencoder_threads"
 
       # There are a number of different ways to encode 6 channel audio.
       # I've loaded 3 different ways into the mencoder_audioch_opts.