Tweaked makemkv rip logic to only copy back the largest created mkv file
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index 8919900b63a2cf9a5a6771f071da471d760f97ec..0035a2d284ad8ec25a20e895a4296027cce22503 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
@@ -3,7 +3,7 @@
 # Author: Alan J. Pippin (apippin@pippins.net)
 # Date: 05/17/2009
 #
-  REV=2.3
+  REV=2.5
 #
 # Description: This script wraps a number of linux utilities to
 # create a recipe for ripping protected DVDs, circumventing 
@@ -35,9 +35,19 @@ typeset profile="xvidhq"
 typeset extension=""
 typeset mailto=""
 typeset encoder=""
+typeset ripper=""
 typeset default_alang="en"
 typeset track=""
 typeset drc="0.0"
+typeset handbrake_xvid=""
+typeset handbrake_mp4=""
+typeset makemkv=""
+typeset makemkv_disc_id=""
+typeset mkvextract=""
+typeset mkvpropedit=""
+typeset mkvmerge=""
+typeset mp4box=""
+typeset dvdxchap=""
 typeset -i default_aid=128
 typeset -i aid_override=-1
 typeset -i force_onepass_mode=0
@@ -57,15 +67,17 @@ typeset -i target_video_bitrate=2000
 typeset -i target_audio_bitrate=224
 typeset -i target_size=0
 typeset -i audio_2ch=0
-typeset -i audio_6ch=1
+typeset -i audio_6ch=0
 typeset -i invalid_feature_title=0
 typeset -i feature_title_override=0
 typeset -i mplayer_dumpstream_incompatibility=0
+typeset -i lsdvd_incompatibility=0
 typeset -i custom_video_bitrate=0
 typeset -i custom_audio_bitrate=0
 typeset -i custom_audio_2ch=0
 typeset -i custom_audio_6ch=0
-typeset -i minimum_feature_title_length=10
+typeset -i minimum_feature_title_length=60
+typeset -i lsdvd_timeout=10
 
 ##############################################################################
 # Local Machine Settings:
@@ -101,7 +113,7 @@ fi
 ##############################################################################################
 # Command line processing
 ##############################################################################################
-while (($#)) && getopts 162wmvifkzx:ht:n:d:b:B:s:t:a:p:e:j:l:r: opt "$@"
+while (($#)) && getopts 162wmvifkzx:ht:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
 do
     case $opt in
         (n)     dvdname=$OPTARG;;
@@ -116,6 +128,7 @@ do
         (i)     make_final_dest_iso=1;;
         (f)     make_final_dest_folder=1;; 
         (z)     make_final_dest_comp=1;;
+       (R)     ripper=$OPTARG;;
        (e)     encoder=$OPTARG;;
         (m)     mirror_mode=1;;
         (k)     keep_intermediate_files=1;;
@@ -154,6 +167,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 "                 - 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"
@@ -166,7 +182,8 @@ function usage() {
     echo >&2 "   -v            Make the final image a DVD VOB file"
     echo >&2 "   -i            Make the final image a DVD ISO file"
     echo >&2 "   -f            Make the final image a DVD folder"
-    echo >&2 "   -z            Make the final image a compressed file based on your profile selection and encoder"
+    echo >&2 "   -z            Make the final image a compressed fi[[ ! -e "$vobfile" ]]le based on your profile selection and encoder"
+    echo >&2 "   -R <ripper>   Specify the ripper to use to make the DVD image (valid rippers=ddrescue|makemkv|handbrake|mplayer) (default=makemkv if found)"
     echo >&2 "   -e <encoder>  Specify the encoder to use to make the compressed file (valid encoders=mencoder|handbrake) (default=handbrake if found)"
     echo >&2 "                 You must also specify the target size or bitrate using the '-s' or '-b' options with xvid profiles"
     echo >&2 "   -s <size>     Set the target size of the compressed file in MB (ex: 700, 1000, etc)"
@@ -230,11 +247,24 @@ if [ $mirror_mode -eq 1 ]; then
   fi
 fi
 
+# If audio channel requirements weren't given, assume a default value of 6channel audio
+if [ $audio_2ch -eq 0 ] && [ $audio_6ch -eq 0 ]; then
+  audio_6ch=1
+  custom_audio_6ch=1
+fi
+
+# Make makemkv the default ripper if not specified and we can find it
+if [ -z "$ripper" ]; then
+  ripper="makemkv"
+  [[ ! -x `which $makemkv` ]] && ripper="ddrescue";
+fi
+
 # Make handbrake the default encoder if not specified and we can find it
 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" =~ "ip" ]] && encoder="handbrake";
   [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
 fi
@@ -242,6 +272,7 @@ fi
 # Sanity check the profile selection
 if [[ "$encoder" == "mencoder" ]]; then
   [[ "$profile" =~ "mp4" ]] && echo "-E- invalid encoder $encoder selected for mp4 profile: $profile" && exit
+  [[ "$profile" =~ "mkv" ]] && echo "-E- invalid encoder $encoder selected for mkv profile: $profile" && exit
 fi
 
 if [[ "$encoder" != "mencoder" ]] && [[ "$encoder" != "handbrake" ]]; then
@@ -281,10 +312,15 @@ fi
 [[ ! -x `which mkisofs` ]] && echo "-E- missing dependency: mkisofs" && exit
 [[ -n "$handbrake_xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing handbrake xvid encoder, set in rip_dvd.conf to: $handbrake_xvid" && exit
 [[ -n "$handbrake_mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing handbrake mp4 encoder, set in rip_dvd.conf to: $handbrake_mp4" && exit
-[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing encoder: $handbrake_xvid" && exit
-[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing encoder: $handbrake_mp4" && exit
-[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "ip" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing encoder: $handbrake_mp4" && exit
-[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing encoder: $handbrake_mp4" && exit
+[[ -n "$makemkv" ]] && [[ ! -x `which $makemkv` ]] && echo "-E- missing makemkv ripper, set in rip_dvd.conf to: $makemkv" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing HandBrake encoder: $handbrake_xvid" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "ip" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvextract` ]] && echo "-E- missing mkvextract: $mkvextract" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvpropedit` ]] && echo "-E- missing mkvpropedit: $mkvpropedit" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvmerge` ]] && echo "-E- missing mkvmerge: $mkvmerge" && exit
+[[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $mp4box` ]] && echo "-E- missing mp4box: $mp4box" && exit
 [[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
 
 ##############################################################################################
@@ -329,6 +365,7 @@ else
     file "$dvdname" | grep -q "MPEG"
     if [ $? == 0 ]; then
       # It is a valid MPEG2 file, now strip the extension off our dvdname
+      # An MPEG file is like a VOB file (intermediate format of the DVD movie)
       vobfile="$dvdname"
       dvdname=`basename "$dvdname"`
       dvdname=${dvdname%.[^.]*}
@@ -341,6 +378,8 @@ else
     file "$dvdname" | grep -q -e "ISO" -e "UDF"
     if [ $? == 0 ]; then
       # It is a valid ISO file, now strip the extension off our dvdname
+      # An ISO file is treated as a disc or DVD image that needs to be converted
+      # to an intermediate vobfile before it can be compressed.
       isofile="$dvdname"
       dvdname=`basename "$dvdname"`
       dvdname=${dvdname%.[^.]*}
@@ -348,11 +387,25 @@ else
       valid_file=1
     fi    
 
-    # check to see if dvdname is a full path to an ISO file
+    # check to see if dvdname is a full path to a VOB file
     # if it is, set dvdname and isofile appropriately
     file "$dvdname" | grep -q -e "VOB"
     if [ $? == 0 ]; then
       # It is a valid VOB file, now strip the extension off our dvdname
+      # A VOB file is an intermediate format of the DVD movie
+      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 MKV file
+    # if it is, set dvdname and isofile appropriately
+    file "$dvdname" | grep -q -e "mkv"
+    if [ $? == 0 ]; then
+      # It is a valid MKV file, now strip the extension off our dvdname
+      # An MKV file is like a VOB file (intermediate format of the DVD movie)
       vobfile="$dvdname"
       dvdname=`basename "$dvdname"`
       dvdname=${dvdname%.[^.]*}
@@ -360,6 +413,7 @@ else
       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"
@@ -466,6 +520,153 @@ trap cleanup EXIT
 # processing functions
 ##############################################################################################
 
+function alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }
+
+function lsdvd_longest_title {
+  dev="$1"
+
+  # Only use lsdvd to extract the longest feature title if we didn't detect an incompatibility earlier
+  if [ $lsdvd_incompatibility -eq 1 ]; then
+    invalid_feature_title=1
+    return
+  fi
+
+  # Try the normal lsdvd command to see if it works
+  alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
+  if [ $? != 0 ]; then
+    # lsdvd didn't work
+    invalid_feature_title=1
+    lsdvd_incompatibility=1
+    return
+  fi
+  # lsdvd is good to go, use it
+  feature_title=`lsdvd $dev 2>/dev/null | awk '/Longest/ { print $NF }'`
+}
+
+function lsdvd_css {
+  dev="$1"
+
+  # Try the normal lsdvd command to see if it works
+  alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
+
+  if [ $? != 0 ]; then
+    # lsdvd didn't work. Try VLC.
+    lsdvd_incompatibility=1
+    if [[ -x `which cvlc` ]]; then 
+      echo "-> lsdvd failed to DeCSS the DVD. Trying VLC: $dev" | tee -a "$logfile"
+      alarm $lsdvd_timeout cvlc --no-video --no-audio $dev  >> "$logfile" 2>&1
+      # Once you get a DVD that VLC can't handle, figure out how 
+      # to detect that here and abort.
+      #if [ $? != 1 ]; then
+      #  echo "-> VLC failed to decss the DVD."
+      #  return 1
+      #fi
+    else
+      fatal_and_exit "-E- lsdvd failed to DeCSS the DVD. VLC not found, but required to rip this DVD."
+    fi
+  fi
+  return 0
+}
+
+# reduce the size of the mkv src file using handbrake
+function encode_mkv_file_makemkv {
+    src="$1"
+    dst="$2"
+    tmpdst=""
+
+    # Convert our ISO to a MKV if it is a real iso
+    if [[ $real_isofile -eq 1 ]]; then
+       tmpdst="$tmpdir/$dvdname.mkv"
+       make_dvd_mkv_image "$tmpdst" "iso" "$src"
+       src="$tmpdst"
+    fi
+
+    # Set a variable that we will use later to determine if we found a handler for $profile or not
+    typeset -i found_profile=0 
+  
+    # Globals
+    typeset quality=""
+    typeset handbrake_audio_opts=""
+    typeset handbrake_cli=""
+
+    # Predefined Handbrake Profile Handling
+    if [[ "$profile" =~ "mkvvhq" ]]; then
+       found_profile=1
+       handbrake_cli="$handbrake_mp4"
+       final_output_file="$dst"
+       quality="0.9"
+    fi
+    if [[ "$profile" =~ "mkvhq" ]]; then
+       found_profile=1
+       handbrake_cli="$handbrake_mp4"
+       final_output_file="$dst"
+       quality="0.85"
+    fi
+
+    # setup our audio option
+    if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
+       handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
+    fi
+    if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
+       handbrake_audio_opts="-E ac3 -6 auto"
+    fi
+    if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
+       handbrake_audio_opts="-E faac -6 dpl2"
+    fi
+
+    # find out what our audio track is
+    get_audio_track_from_mkv "$src" "$handbrake_cli"
+    if [ -n "$track" ]; then
+       handbrake_audio_opts="$handbrake_audio_opts -a $track"
+       track="" 
+    fi
+    
+    # Execute the handbrake command to encode the video
+    echo -e "\n   Encoding: $handbrake_cli -i \"$src\" -o \"$dst\" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 -m"
+    $handbrake_cli -i "$src" -o "$dst" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 | tee -a $encodelog 2>&1
+    handbrake_retval=$?
+
+    if [ $handbrake_retval != 0 ]; then
+       fatal_and_exit "-E- Unhandled handbrake error"
+    fi
+
+    # Extract the chapters from the src and apply to the dst
+    chapter_tmpfile="$tmpdir/$dvdname.xml"
+    $mkvextract chapters "$src" > "$chapter_tmpfile"
+    $mkvpropedit -c "$chapter_tmpfile" "$dst"
+
+    # find out what our default_alang subtitle track is in the src
+    get_subtitle_track_from_mkv "$src" "$handbrake_cli"
+
+    # Extract the subtitles from the src and apply to the dst
+    srt_tmpfile="$tmpdir/$dvdname.srt"
+    sub_tmpfile="$tmpdir/$dvdname.sub"
+    idx_tmpfile="$tmpdir/$dvdname.idx"
+    $mkvextract tracks "$src" $track:$srt_tmpfile
+    $mkvmerge -o "$dst.subs" "$dst" $idx_tmpfile
+    mv "$dst.subs" "$dst"
+
+    # Set the default subtitle track to "no subtitles" in the dst
+    $mkvpropedit "$dst" --edit track:s1 --set flag-default=0
+
+    # Concatenate the encode log to our main log file, greping out unwanted lines
+    cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile" 
+    cat $encodelog | grep "Encoding:" | sed 's/\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
+    cat $encodelog | grep "Encoding:" | sed 's/\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"  
+
+    # Remove intermediary file if we created one   
+    if [ $keep_intermediate_files -eq 0 ]; then
+       [[ -e "$tmpdst" ]] && rm -f "$tmpdst"
+       rm "$chapter_tmpfile"
+       rm "$idx_tmpfile" "$sub_tmpfile"
+    else
+       echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
+       echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
+       [[ -e "$tmpdst" ]] && echo "-> Keeping intermediate MKV file: $tmpdst" | tee -a "$logfile"
+    fi
+}
+
 # encode the vob file into a compressed file format using handbrake
 function encode_vob_file_handbrake {
   
@@ -481,10 +682,11 @@ function encode_vob_file_handbrake {
   # Set a variable that we will use later to determine if we found a handler for $profile or not
   typeset -i found_profile=0    
 
-  # For a given profile, to override the 2 pass behavior to be single pass,
-  # simply set the PASSES variable below to "2" instead of "1 2" inside your profile handler. 
-  # It indicates which PASS numbers to loop over. PASSES="2" means just do pass 2 => single pass mode.
+  # Default is 2 pass mode. Profiles can override this. Command line can override this as well.
   PASSES="-2"
+  if [ $force_onepass_mode -eq 1 ]; then
+      PASSES=""
+  fi
 
   # Set our DRC option
   DRC="-D $drc"
@@ -501,8 +703,12 @@ function encode_vob_file_handbrake {
     PASSES=""
   fi
 
-  # get our audio track from the VOB file (requires mp4 version of handbrake to extract)
-  get_audio_track_from_vob "$vobfile" "$handbrake_mp4"
+  # get our audio track from the vobfile (requires mp4 version of handbrake to extract)
+  if [[ "$vobfile" =~ /mkv/ ]]; then
+      get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
+  else
+      get_audio_track_from_vob "$vobfile" "$handbrake_mp4"
+  fi
 
   # XVID profile
   if [[ "$profile" =~ "xvid" ]]; then
@@ -542,7 +748,7 @@ function encode_vob_file_handbrake {
     found_profile=1
     handbrake_cli="$handbrake_mp4"
     final_output_file="$dest/$dvdname.mp4"
-    PASSES=""
+    PASSES="$PASSES -T"
 
     # Handle custom parameter overrides
     if [ $custom_video_bitrate == 1 ]; then
@@ -569,6 +775,43 @@ 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
@@ -603,7 +846,6 @@ function encode_vob_file_handbrake {
     found_profile=1
     handbrake_cli="$handbrake_mp4"
     final_output_file="$dest/$dvdname.mp4"
-    PASSES=""
 
     # Handle custom parameter overrides
     if [ $custom_video_bitrate == 1 ]; then
@@ -651,7 +893,7 @@ function encode_vob_file_handbrake {
   done
 
   # Append "global" command line options
-  handbrake_cmd_line_opts="$handbrake_cmd_line_opts -v 1"
+  handbrake_cmd_line_opts="$handbrake_cmd_line_opts -v 1 -m"
   handbrake_cmd_line_opts="$handbrake_cmd_line_opts $PASSES"
   handbrake_cmd_line_opts="$handbrake_cmd_line_opts $DRC"
   handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SCALE"
@@ -674,6 +916,69 @@ function encode_vob_file_handbrake {
     fatal_and_exit "-E- Unhandled handbrake error"
   fi
 
+  
+  # SRC = ISO and FINAL = MP4
+  # Extract the chapters from the ISO/VOB so we can add them to the MP4 file. 
+  # We can only do this if we have an ISO file and we made an MP4 file.
+  if [[ "$final_output_file" =~ "mp4" ]] && [[ -e "$isofile" ]]; then
+    chapter_tmpfile="$tmpdir/$dvdname.chapters"
+    $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
+    $mp4box -chap "$chapter_tmpfile" "$final_output_file"
+
+    # We could also add subtitles, but we would need to follow this guide to do it:
+    # http://ubuntuforums.org/showthread.php?t=1253635
+    # http://en.gentoo-wiki.com/wiki/Ripping_DVD_to_Matroska_and_H.264
+
+  fi
+
+  # SRC = ISO and FINAL = MKV
+  if [[ "$final_output_file" =~ "mkv" ]] && [[ -e "$isofile" ]]; then
+    chapter_tmpfile="$tmpdir/$dvdname.chapters"
+    $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
+    $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
+  fi
+
+  # SRC = MKV and FINAL = MP4
+  if [[ "$final_output_file" =~ "mp4" ]] && [[ "$vobfile" =~ "mkv" ]]; then
+    # Extract the chapters from the src and apply to the dst
+    chapter_tmpfile="$tmpdir/$dvdname.xml"
+    $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
+    $mp4box -chap "$chapter_tmpfile" "$final_output_file"
+  fi
+
+  # SRC = MKV and FINAL = MKV
+  # If our original src file was an MKV, extract the chapters and subtitles from it
+  if [[ "$final_output_file" =~ "mkv" ]] && [[ "$vobfile" =~ "mkv" ]]; then
+    # Extract the chapters from the src and apply to the dst
+    chapter_tmpfile="$tmpdir/$dvdname.xml"
+    $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
+    $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
+
+    # find out what our default_alang subtitle track is in the src
+    get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
+
+    # Extract the subtitles from the src and apply to the dst
+    srt_tmpfile="$tmpdir/$dvdname.srt"
+    sub_tmpfile="$tmpdir/$dvdname.sub"
+    idx_tmpfile="$tmpdir/$dvdname.idx"
+    $mkvextract tracks "$vobfile" $track:$srt_tmpfile
+    $mkvmerge -o "$final_output_file.subs" "$final_output_file" "$idx_tmpfile"
+    mv "$final_output_file.subs" "$final_output_file"
+
+    # Set the default subtitle track to "no subtitles" in the dst
+    $mkvpropedit "$final_output_file" --edit track:s1 --set flag-default=0 
+  fi
+
+  # Remove intermediary file if we created one   
+  if [ $keep_intermediate_files -eq 0 ]; then
+      [[ -e "$chapter_tmpfile" ]] && rm "$chapter_tmpfile"
+      [[ -e "$idx_tmpfile" ]] && rm "$idx_tmpfile" 
+      [[ -e "$sub_tmpfile" ]] && rm "$sub_tmpfile"
+  else
+      [[ -e "$chapter_tmpfile" ]] && echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
+      [[ -e "$idx_tmpfile" ]] && echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
+  fi
+
   # Concatenate the encode log to our main log file, greping out unwanted lines
   cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile" 
   cat $encodelog | grep "Encoding:" | sed 's/\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
@@ -843,6 +1148,58 @@ function encode_vob_file_mencoder {
   done
 }
 
+function make_dvd_mkv_image {
+    dstmkv="$1"
+    srctype="$2"
+    srcname="$3"
+    dstdir=${dstmkv%.[^.]*}
+    ((min_length = minimum_feature_title_length * 60))
+
+    echo -e "\n   Ripping: $makemkv --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=$?
+    
+    if [ $makemkv_retval != 0 ]; then
+       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 -rS "$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 [ $? != 0 ]; then
+       fatal_and_exit "-E- Unhandled mv error"
+    fi
+    if [ $keep_intermediate_files -eq 0 ]; then
+       [[ -d "$dstdir" ]] && rm -rf "$dstdir"
+    fi
+
+    # Concatenate the encode log to our main log file, greping out unwanted lines
+    cat "$ddrescuelog" | grep -v "Current progress:" >> "$logfile"    
+}
+
+function make_dvd_mkv_image_from_folder {
+    src="$1"
+    dst="$2"
+    handle_error=$3
+    dstdir=${dst%.[^.]*}
+    make_dvd_mkv_image "$dst" "file" "$src"
+}
+
 function make_dvd_iso_image {
 
   isofile="$1"
@@ -850,7 +1207,7 @@ function make_dvd_iso_image {
   # check to see if we have a dvdpath to rip from instead of $dev
   if [ -z "$dvdpath" ]; then
     # load the CSS codes in the DVD drive 
-    lsdvd $dev >> "$logfile"
+    lsdvd_css "$dev"
     if [ $? != 0 ]; then
       fatal_and_exit "-E- lsdvd $dev failed" 
     fi
@@ -951,9 +1308,9 @@ function get_feature_title {
   fi
   # otherwise, use lsdvd to figure it out
   if [ $ripdvd -eq 1 ]; then
-    feature_title=`lsdvd $dev | awk '/Longest/ { print $NF }'`
+    lsdvd_longest_title "$dev" 
   else 
-    feature_title=`lsdvd "$source" 2>/dev/null | awk '/Longest/ { print $NF }'`
+    lsdvd_longest_title "$source"
   fi
 }
 
@@ -1076,6 +1433,45 @@ function get_crop_from_vob {
   echo "   Setting mencoder crop filter to: $CROP"
 }
 
+function get_audio_track_from_mkv { 
+  mkv="$1"
+  handbrake_cli="$2"
+  aidcheck=`tempfile`
+  alang=$default_alang
+
+  # Find out what audio track matches our alang
+  $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
+  stream=`grep "$alang" $aidcheck | grep "Audio:"`
+
+  # extract the track number that handbrake uses
+  track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
+  
+  if [ -n "$track" ]; then   
+      echo "   Setting the audio track to $track." | tee -a "$logfile"
+  fi
+}
+
+function get_subtitle_track_from_mkv { 
+  mkv="$1"
+  handbrake_cli="$2"
+  aidcheck=`tempfile`
+  alang=$default_alang
+
+  # Find out what audio track matches our alang
+  $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
+  stream=`grep "$alang" $aidcheck | grep "Subtitle:" | head -n 1`
+
+  # extract the track number that handbrake uses
+  track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
+
+  # add 1 to the track number since mkv tools start at 1, and HandBrake starts at 0
+  ((track = track + 1))
+
+  if [ -n "$track" ]; then   
+      echo "   Setting the subtitle track to $track." | tee -a "$logfile"
+  fi
+}
+
 function get_audio_track_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.
@@ -1322,6 +1718,15 @@ function check_for_mplayer_dumpstream_incompatibility {
 
   echo "-> Checking for mplayer dumpstream incompatibilities" | tee -a "$logfile"
 
+  # Check to see if the DVD had any lsdvd incompatibilities
+  if [ $lsdvd_incompatibility -eq 1 ]; then
+    invalid_feature_title=1
+    echo "-E- Unable to determine the feature title due to an lsdvd inability to DeCSS" | 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"    
+    return
+  fi
+
   if [ ! -e "$vobfile" ]; then
     # mplayer dumpstream does not work on DVDs that obscure the feature title.
     # A DVD that has 99 titles, where the longest title isn't the main feature
@@ -1329,9 +1734,9 @@ function check_for_mplayer_dumpstream_incompatibility {
     # out what the feature title is. This script will run through that flow if we
     # set use_mplayer_dumpstream to 0. Check for this here.
     if [ $ripdvd -eq 1 ]; then
-      lsdvd $dev | grep -q "Title: 99"
+      alarm $lsdvd_timeout lsdvd $dev | grep -q "Title: 99"
     else
-      lsdvd "$isofile" | grep -q "Title: 99"
+      alarm $lsdvd_timeout lsdvd "$isofile" | grep -q "Title: 99"
     fi
     # If we have 99 titles and a feature title wasn't given on the command line, switch modes.
     if [ $? == 0 ] && [ $feature_title_override -eq 0 ]; then
@@ -1371,13 +1776,16 @@ function check_for_mplayer_dumpstream_incompatibility {
   # The resulting VOB file looks complete, but has something in it that causes mplayer/mencoder 
   # to be unable to encode it since it thinks the entire VOB is only a few minutes long in length.
   # Using HandBrake with an MP4 type profile can work around this, but mencoder or Handbrake with XVID profile won't.
-  vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
-  ((min_length = minimum_feature_title_length * 60))
-  if [[ $vob_length -lt $min_length ]]; then 
-    if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
-      echo "-E- The main feature title that was ripped from the DVD has an invalid movie length." | tee -a "$logfile"
-      echo "    You'll need rip this DVD with the handbrake encoder and an MP4 type profile instead." | tee -a "$logfile"
-      mplayer_dumpstream_incompatibility=1
+  if [ -e "$vobfile" ]; then
+    vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
+    ((min_length = minimum_feature_title_length * 60))
+    if [[ $vob_length -lt $min_length ]]; then 
+      if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
+        echo "-E- The main feature title that was ripped from the DVD has an invalid movie length." | tee -a "$logfile"
+        echo "    You'll need rip this DVD with the handbrake encoder and an MP4 or MKV type profile instead." | tee -a "$logfile"
+        mplayer_dumpstream_incompatibility=1
+       fatal_and_exit "-E- Aborting encoding step due to invalid movie length."
+      fi
     fi
   fi
 }
@@ -1446,11 +1854,21 @@ echo "$cmd" >> "$logfile"
 if [ $mirror_mode -eq 1 ]; then
   echo "-> Ripping DVD $dvdname to $dest"
 
-  # use ddrescue to make an ISO image of the disk
-  make_dvd_iso_image "$dest/$dvdname.iso"
-
-  # add this video data to the mythtv DB
-  fill_mythvideo_metadata "$dest/$dvdname.iso" 
+  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"
+      # 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"
+      mv "$tmpdir/$dvdname.iso" "$dest/$dvdname.iso"
+      # add this video data to the mythtv DB
+      fill_mythvideo_metadata "$dest_filename"
+  fi
 
   # eject the disk upon completion
   if [ $eject_disk -ne 0 ]; then
@@ -1472,29 +1890,41 @@ if [ $mirror_mode -eq 0 ]; then
   # Rip image from DVD
   if [ $ripdvd -eq 1 ]; then
     echo "-> Ripping DVD $dvdname to $dest" | tee -a "$logfile"
-    # use ddrescue to make an ISO image of the disk
-    make_dvd_iso_image "$tmpdir/$dvdname.iso"
+    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"
+       vobfile="$tmpdir/$dvdname.mkv"
+    else 
+        # use ddrescue to make an ISO image of the disk
+       make_dvd_iso_image "$tmpdir/$dvdname.iso"
+       isofile="$tmpdir/$dvdname.iso"
+    fi
   fi
 
   # Rip image from DVD path
   if [ -n "$dvdpath" ]; then
     echo "-> Ripping DVD $dvdpath to $dest" | tee -a "$logfile"
-    make_dvd_iso_image_from_folder "$dvdpath" "$tmpdir/$dvdname.iso" 1
-  fi
-
-  # make sure our isofile value is set
-  if [ -z "$isofile" ]; then
+    make_dvd_iso_image_from_folder "$dvdpath" "$tmpdir/$dvdname.iso" 1  
     isofile="$tmpdir/$dvdname.iso"
   fi
-     
   if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
 
-    if [ ! -e "$vobfile" ]; then
-      echo "-> Creating DVD video $vobfile" | tee -a "$logfile"
-      
-      # get the feature title from the ISO
-      get_feature_title "$isofile"
+    # Create our "vobfile" using makemkv if told to do so
+    if [[ "$isofile" =~ "iso" ]] && [[ "$ripper" == "makemkv" ]]; then
+       vobfile="$tmpdir/$dvdname.mkv"
+       make_dvd_mkv_image "$vobfile" "iso" "$isofile"
+    fi
+
+    # get the feature title from the ISO
+    if [[ "$isofile" =~ "iso" ]]; then
+       get_feature_title "$isofile"
+    fi
+
+    # Create our "vobfile" using mplayer if needed
+    if [[ ! -e "$vobfile" ]] && [[ "$isofile" =~ "iso" ]]; then
+      echo "-> Creating DVD video $vobfile from $isofile" | tee -a "$logfile"
+       
       # get the crop value from the ISO
       get_crop_from_iso
 
@@ -1551,7 +1981,7 @@ if [ $mirror_mode -eq 0 ]; then
       remove_intermediate_iso_file
 
     else
-      if [ "$encoder" != "handbrake" ]; then 
+      if [[ -e "$vobfile" ]] && [[ "$encoder" != "handbrake" ]]; then 
         echo "-> Skipping VOB creation. VOB DVD video already exists: $vobfile" | tee -a "$logfile"
         # get our audio id from the VOB file
         get_audio_id_from_vob "$vobfile"