Added new eject_opts option for greater flexibility.
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index 7c6bc3cc5f236c360bd73308b1461df1b6bcbc26..8a8c2fa072287fc0371adb4ed29d70a5c0e6e03d 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
@@ -48,6 +48,7 @@ typeset mkvpropedit=""
 typeset mkvmerge=""
 typeset mp4box=""
 typeset dvdxchap=""
+typeset eject_opts="-T"
 typeset -i default_aid=128
 typeset -i aid_override=-1
 typeset -i force_onepass_mode=0
@@ -78,6 +79,7 @@ typeset -i custom_audio_2ch=0
 typeset -i custom_audio_6ch=0
 typeset -i minimum_feature_title_length=60
 typeset -i lsdvd_timeout=10
+typeset -i makemkv_copy_largest_title_only=1
 
 ##############################################################################
 # Local Machine Settings:
@@ -113,7 +115,7 @@ fi
 ##############################################################################################
 # Command line processing
 ##############################################################################################
-while (($#)) && getopts 162wmvifkzx:ht:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
+while (($#)) && getopts 162wmvifkzx:hT:t:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
 do
     case $opt in
         (n)     dvdname=$OPTARG;;
@@ -140,6 +142,7 @@ do
        (l)     aid_override=$OPTARG;;
        (r)     drc=$OPTARG;;
         (w)     set -x;;
+       (T)     minimum_feature_title_length=$OPTARG;;
         (h)     show_usage=1;;
         (:)     echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
         (\?)    echo >&2 "$0: invalid option '$OPTARG'"; errors=errors+1;;
@@ -167,9 +170,9 @@ function usage() {
     echo >&2 "                 - iphone = MP4, x264 codec, 2 pass encoding, forced 480:320 scaling"
     echo >&2 "                 - ipod = MP4, x264 codec, 2 pass encoding, forced 320:240 scaling"
     echo >&2 "                 Handbrake Only Encoder Profiles:"
-    echo >&2 "                 - mkvvhq = MKV, very high quality encoding (0.95 quality)"
-    echo >&2 "                 - mkvhq = MKV, high quality encoding (0.90 quality)"
-    echo >&2 "                 - mkv = MKV, high quality encoding (0.85 quality)"
+    echo >&2 "                 - mkvvhq = MKV, very high quality encoding, x264 codec, 2 pass encoding"
+    echo >&2 "                 - mkvhq = MKV, high quality encoding, x264 codec, 2 pass encoding"
+    echo >&2 "                 - mkv = MKV, high quality encoding, x264 codec, 2 pass encoding"
     echo >&2 "                 - mp4vhq = MP4, very high quality encoding, x264 codec, 2 pass encoding"
     echo >&2 "                 - mp4hq = MP4, high quality encoding, x264 codec, 2 pass encoding"
     echo >&2 "                 - mp4 = MP4, fast encoding, x264 codec, 2 pass encoding"
@@ -207,6 +210,7 @@ function usage() {
     echo >&2 "                 By default, all intermediary files are deleted. Only the final image is kept"
     echo >&2 "   -l <aid>      Specify the audio AID language ID to rip from the source DVD"
     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 "   -T <length>   Specify the minimum feature title length in minutes. This is used when picking which title to rip."
     echo >&2 "   -w            Set the sh Execute/Verbose flag (causes every command to be echoed)"
     echo >&2 ""
     exit 2
@@ -264,7 +268,7 @@ if [ -z "$encoder" ]; then
   encoder="mencoder"; # If we can't find handbrake, set mencoder as the default
   [[ -x `which $handbrake_xvid` ]] && [[ "$profile" =~ "xvid" ]] && encoder="handbrake";
   [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mp4" ]] && encoder="handbrake";
-  [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mk4" ]] && encoder="handbrake";
+  [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mkv" ]] && encoder="handbrake";
   [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "ip" ]] && encoder="handbrake";
   [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
 fi
@@ -334,9 +338,18 @@ if [ -z "$dvdname" ]; then
     echo "-E- Can't access the DVD device $dev"
     exit 1
   fi
+
   # now capture the volume name from the device
   dvdname=`volname $dev | awk '{ print $1 }'`
   ripdvd=1
+  # if the dvdname is still blank, go about it a different way (for BluRays)
+  if [[ -z "$dvdname" ]]; then
+    echo "-> Using makemkv to obtain disk label"
+    dvdname=`$makemkv info --noscan --robot disc:$makemkv_disc_id | grep "CINFO:2," | awk -F "," '{ print $3; }' | tr ' ' '_' | sed 's/\"//g'`
+    echo "   Found disk label: $dvdname"
+    ripdvd=1
+  fi
 
 else 
 
@@ -743,11 +756,12 @@ function encode_vob_file_handbrake {
     fi
   fi
 
-  # MP4 profile
-  if [[ "$profile" =~ "mp4" ]]; then
+  # MP4 || MKV profile
+  if [[ "$profile" =~ "mp4" ]] || [[ "$profile" =~ "mkv" ]]; then
     found_profile=1
     handbrake_cli="$handbrake_mp4"
-    final_output_file="$dest/$dvdname.mp4"
+    [[ "$profile" =~ "mp4" ]] && final_output_file="$dest/$dvdname.mp4"
+    [[ "$profile" =~ "mkv" ]] && final_output_file="$dest/$dvdname.mkv"        
     PASSES="$PASSES -T"
 
     # Handle custom parameter overrides
@@ -775,43 +789,6 @@ function encode_vob_file_handbrake {
     fi
   fi
 
-  # MKV profile
-  if [[ "$profile" =~ "mkv" ]]; then
-    found_profile=1
-    handbrake_cli="$handbrake_mp4"
-    final_output_file="$dest/$dvdname.mkv"
-    PASSES=""
-
-    # Handle custom parameter overrides
-    if [ $custom_video_bitrate == 1 ]; then
-      handbrake_opts[0]="-b $target_video_bitrate"
-    fi
-    if [ $custom_audio_2ch == 0 ]; then
-      audio_2ch=0
-    fi
-    if [ $custom_audio_6ch == 0 ]; then
-      audio_6ch=0
-    fi
-
-    # Very High Quality
-    if [ "$profile" == "mkvvhq" ]; then
-      handbrake_opts[0]="-q 0.95"
-    fi
-    # High Quality
-    if [ "$profile" == "mkvhq" ]; then
-      handbrake_opts[0]="-q 0.90"
-    fi
-    # Fast
-    if [ "$profile" == "mkv" ]; then
-      handbrake_opts[0]="-q 0.85"
-    fi
-
-    # Common options to all profiles above
-    handbrake_opts[1]="--strict-anamorphic"
-    handbrake_opts[2]="--crop 0:0:0:0"
-  fi
-
   # iphone and ipod MP4 profiles
   if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
     found_profile=1
@@ -1155,7 +1132,7 @@ function make_dvd_mkv_image {
     dstdir=${dstmkv%.[^.]*}
     ((min_length = minimum_feature_title_length * 60))
 
-    echo -e "\n   Ripping: $makemkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir"
+    echo -e "\n   Ripping: $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir"
     mkdir -p "$dstdir"
     $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir 2>&1 | tee -a "$ddrescuelog"
     makemkv_retval=$?
@@ -1164,22 +1141,33 @@ function make_dvd_mkv_image {
        fatal_and_exit "-E- Unhandled makemkv error"
     fi
 
-    # Move the largest created MKV file into place
-    # There might be multiple ones, so just grab the largest one
-    largest_mkv_file=`/bin/ls -1S "$dstdir"/*.mkv | head -1`
-    echo -e "\n   Moving largest mkv file $largest_mkv_file -> $dstmkv\n" | tee -a "$ddrescuelog"
-    mv "$largest_mkv_file" "$dstmkv"
-
-    # Move all of the created MKV file into place
-    #echo "\n" | tee -a "$ddrescuelog"
-    #dstfile=${dstmkv%.[^.]*}
-    #typeset -i num=0
-    #for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
-    #echo -e "   Moving $i -> $dstfile.$num.mkv" | tee -a "$ddrescuelog"
-    #mv "$i" "$dstfile.$num.mkv"
-    #((num = num + 1 ))
-    #done
-    #echo "\n" | tee -a "$ddrescuelog"
+    if [ $makemkv_copy_largest_title_only -eq 1 ]; then
+      # Move the largest created MKV file to destination
+      # There might be multiple ones, so just grab the largest one
+      largest_mkv_file=`/bin/ls -1S "$dstdir"/*.mkv | head -1`
+      echo -e "\n   Moving largest mkv file $largest_mkv_file -> $dstmkv\n" | tee -a "$ddrescuelog"
+      mv "$largest_mkv_file" "$dstmkv"
+    else
+      # Move all of the created MKV files to destination
+      echo "\n" | tee -a "$ddrescuelog"
+      dstfile=${dstmkv%.[^.]*}
+      typeset -i num=0
+      typeset -i titles=0
+      for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
+       ((titles = titles + 1))
+      done
+      if [[ $titles > 1 ]]; then
+        for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
+          echo -e "   Moving $i -> $dstfile.$num.mkv" | tee -a "$ddrescuelog"
+          mv "$i" "$dstfile.$num.mkv"
+          ((num = num + 1 ))
+        done
+      else 
+         echo -e "  Moving $dstdir/*.mkv -> $dstfile.mkv" | tee -a "$ddrescuelog"
+         mv "$dstdir"/*.mkv "$dstfile.mkv"
+      fi
+      echo "\n" | tee -a "$ddrescuelog"
+    fi
 
     if [ $? != 0 ]; then
        fatal_and_exit "-E- Unhandled mv error"
@@ -1857,14 +1845,27 @@ if [ $mirror_mode -eq 1 ]; then
   if [[ "$ripper" == "makemkv" ]]; then
       # use makemkv to make an MKV file of the disk
       make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
-      echo "   Moving $tmpdir/$dvdname.mkv -> $dest/$dvdname.mkv"
-      mv "$tmpdir/$dvdname.mkv" "$dest/$dvdname.mkv"
+      # move the ripped movies into place
+      typeset -i num=0 
+      for i in `/bin/ls -1 "$tmpdir/$dvdname"*.mkv`; do
+        if [[ "$i" =~ .[[:digit:]].mkv ]]; then
+          finaldest="$dest/$dvdname.$num.mkv"
+        else
+          finaldest="$dest/$dvdname.mkv"
+        fi
+        while [[ -e "$finaldest" ]]; do
+          ((num = num + 1 ))
+          finaldest="$dest/$dvdname.$num.mkv"
+        done
+        echo "   Moving $i -> $finaldest"
+        mv "$i" "$finaldest"
+      done
       # add this video data to the mythtv DB
       fill_mythvideo_metadata "$dest_filename"
   else       
       # use ddrescue to make an ISO image of the disk
       make_dvd_iso_image "$tmpdir/$dvdname.iso"
-      echo "   Moving $tmpdir/$dvdname.mkv -> $dest/$dvdname.mkv"
+      echo "   Moving $tmpdir/$dvdname.iso -> $dest/$dvdname.iso"
       mv "$tmpdir/$dvdname.iso" "$dest/$dvdname.iso"
       # add this video data to the mythtv DB
       fill_mythvideo_metadata "$dest_filename"
@@ -1872,7 +1873,7 @@ if [ $mirror_mode -eq 1 ]; then
 
   # eject the disk upon completion
   if [ $eject_disk -ne 0 ]; then
-    eject -T $dev
+    eject $eject_opts $dev
   fi
 
   date=`date`
@@ -1912,6 +1913,7 @@ if [ $mirror_mode -eq 0 ]; then
 
     # Create our "vobfile" using makemkv if told to do so
     if [[ "$isofile" =~ "iso" ]] && [[ "$ripper" == "makemkv" ]]; then
+       echo "-> Creating VOB/MKV file from $isofile"
        vobfile="$tmpdir/$dvdname.mkv"
        make_dvd_mkv_image "$vobfile" "iso" "$isofile"
     fi
@@ -1991,7 +1993,7 @@ if [ $mirror_mode -eq 0 ]; then
     fi
 
     # eject the DVD disk since we are finished with it
-    [ $eject_disk -eq 2 ] && eject -T $dev
+    [ $eject_disk -eq 2 ] && eject $eject_opts $dev
 
     # encode the VOB file to a compressed file format
     if [ $make_final_dest_comp -eq 1 ]; then
@@ -2077,7 +2079,7 @@ if [ $mirror_mode -eq 0 ]; then
   remove_intermediate_dvdbackup_folder
 
   # eject the DVD disk upon completion
-  [ $eject_disk -eq 1 ] && eject -T $dev
+  [ $eject_disk -eq 1 ] && eject $eject_opts $dev
 
   date=`date`
   echo "$date DVD rip completed" | tee -a "$logfile"