Added ability to deal with iso files given via the -n option
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index 3e6d5059b9cf910df259fccf5ce72413c944993d..c47fd59772b2c7521e2c15b37950a3e97ee45807 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
@@ -45,6 +45,12 @@ typeset dest=""
 typeset isofile=""
 typeset vobfile=""
 typeset dvdpath=""
+typeset aspect=""
+typeset SCALE=""
+typeset CROP=""
+typeset -i keep_isofile=0
+typeset -i keep_vobfile=0
+typeset -i keep_dvdfolder=0
 typeset -i keep_intermediate_files=0
 typeset -i make_final_dest_vob=0
 typeset -i make_final_dest_iso=0
@@ -60,7 +66,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;;
@@ -75,6 +81,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;;
@@ -94,6 +101,7 @@ function usage() {
     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 "                 5) If dvdname is a full path to an ISO 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."
@@ -104,12 +112,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
 }
@@ -125,8 +136,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
 
@@ -149,6 +160,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
@@ -178,6 +205,7 @@ else
   if [ -d "$dvdname" ]; then
     dvdpath="$dvdname"
     dvdname=`basename "$dvdname"`
+    keep_dvdfolder=1
     if [ -z "$dvdname" ]; then
      echo "-E- Unable to extract dvdname from path: $dvdpath"
      exit 1
@@ -187,16 +215,37 @@ else
       exit 1
     fi
   fi
-  # check to see if dvdname is a full path to an MPG2 (VOB) file
-  # if it is, set dvdname and vobfile appropriately
+
+  # Check to see if dvdname is a full path to a file
   if [ -f "$dvdname" ]; then
-    vobfile="$dvdname"
-    dvdname=`basename "$dvdname"`
-    file "$vobfile" | grep -q "MPEG"
+    valid_file=0
+
+    # check to see if dvdname is a full path to an MPG2 (VOB) file
+    # if it is, set dvdname and vobfile appropriately
+    file "$dvdname" | grep -q "MPEG"
     if [ $? == 0 ]; then
       # It is a valid MPEG2 file, now strip the extension off our dvdname
+      vobfile="$dvdname"
+      dvdname=`basename "$dvdname"`
+      dvdname=${dvdname%.[^.]*}
+      keep_vobfile=1
+      valid_file=1
+    fi
+
+    # check to see if dvdname is a full path to an ISO file
+    # if it is, set dvdname and isofile appropriately
+    file "$dvdname" | grep -q "ISO"
+    if [ $? == 0 ]; then
+      # It is a valid ISO file, now strip the extension off our dvdname
+      isofile="$dvdname"
+      dvdname=`basename "$dvdname"`
       dvdname=${dvdname%.[^.]*}
-    else 
+      keep_isofile=1
+      valid_file=1
+    fi    
+
+    # If we didn't find a handler for the file above, complain
+    if [ $valid_file -eq 0 ]; then
       echo "-E- Unsupported file type: $vobfile"
       exit 1
     fi
@@ -740,8 +789,8 @@ function fill_mythvideo_metadata {
   fi
 }
 
+# remove the intermediate VOB file
 function remove_intermediate_vob_file {
-  # remove the intermediate VOB file
   if [ $keep_intermediate_files -eq 0 ]; then
     [[ -e "$tmpdir/$dvdname.VOB" ]] && rm -f "$tmpdir/$dvdname.VOB"
   else
@@ -749,8 +798,9 @@ function remove_intermediate_vob_file {
   fi
 }
 
+# remove the original DVD image 
 function remove_intermediate_iso_file {
-  # remove the original DVD image 
+  [[ $keep_isofile -eq 1 ]] && return 1
   if [ $keep_intermediate_files -eq 0 ]; then
     [[ -e "$isofile" ]] && rm "$isofile"
   else
@@ -758,6 +808,7 @@ function remove_intermediate_iso_file {
   fi
 }
 
+# remove the intermediate dvdbackup folder
 function remove_intermediate_dvdbackup_folder {
   if [ $keep_intermediate_files -eq 0 ]; then
     [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
@@ -897,7 +948,7 @@ 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_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.
@@ -959,7 +1010,7 @@ if [ $mirror_mode -eq 0 ]; then
       done
 
       if [ $keep_intermediate_files -eq 0 ] && [ $make_final_dest_vob -eq 0 ]; then
-        [[ -e "$vobfile" ]] && rm -f "$vobfile";
+        [[ -e "$vobfile" ]] && [[ $keep_vobfile -eq 0 ]] && rm -f "$vobfile";
         [[ -e "$passlogfile" ]] && rm -f "$passlogfile";
       else
         echo "-> Keeping VOB file: $vobfile" | tee -a "$logfile"