Additional tweaks
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index 7158101ea1475b41244ba1c92d4261172e2590ae..ce9f0c767502a92f75f46635467bf8d6d9d8c1be 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
@@ -38,11 +38,12 @@ typeset dvdpath=""
 typeset aspect=""
 typeset SCALE=""
 typeset CROP=""
-typeset profile="xvidvhq"
+typeset profile="xvidhq"
 typeset extension=""
 typeset mailto=""
 typeset encoder=""
 typeset default_alang="en"
+typeset track=""
 typeset -i default_aid=128
 typeset -i aid_override=-1
 typeset -i force_onepass_mode=0
@@ -100,7 +101,7 @@ fi
 ##############################################################################################
 # Command line processing
 ##############################################################################################
-while (($#)) && getopts 162mvifkzx:ht:n:d:b:s:t:a:p:e:j:l: opt "$@"
+while (($#)) && getopts 162wmvifkzx:ht:n:d:b:s:t:a:p:e:j:l: opt "$@"
 do
     case $opt in
         (n)     dvdname=$OPTARG;;
@@ -123,7 +124,7 @@ do
        (x)     extension=$OPTARG;;
        (j)     eject_disk=$OPTARG;;
        (l)     aid_override=$OPTARG;;
-        (w)     set -$opt;;
+        (w)     set -x;;
         (h)     show_usage=1;;
         (:)     echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
         (\?)    echo >&2 "$0: invalid option '$OPTARG'"; errors=errors+1;;
@@ -145,8 +146,8 @@ function usage() {
     echo >&2 "                 5) If dvdname is a full path to a VOB file, it will be ripped as a DVD instead of $dev"
     echo >&2 "   -p <profile>  Specify which encoding profile to use in -x mode as shown below:"
     echo >&2 "                 Mencoder and Handbrake Encoder Profiles:"
-    echo >&2 "                 - xvidvhq = AVI, very high quality encoding, Xvid codec, 2 pass encoding (default)"
-    echo >&2 "                 - xvidhq = AVI, high quality encoding, Xvid codec, 2 pass encoding"
+    echo >&2 "                 - xvidvhq = AVI, very high quality encoding, Xvid codec, 2 pass encoding"
+    echo >&2 "                 - xvidhq = AVI, high quality encoding, Xvid codec, 2 pass encoding (default)"
     echo >&2 "                 - xvid = AVI, fast encoding, Xvid codec, 2 pass encoding"
     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"
@@ -165,7 +166,7 @@ function usage() {
     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 "   -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"
+    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)"
     echo >&2 "   -b <bitrate>  Set the bitrate desired in the compressed 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)"
@@ -229,6 +230,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" =~ "ip" ]] && encoder="handbrake";
   [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
 fi
 
@@ -268,7 +270,9 @@ fi
 [[ ! -x `which mkisofs` ]] && echo "-E- missing dependency: mkisofs" && 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
+[[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
 
 ##############################################################################################
 
@@ -457,6 +461,7 @@ function encode_vob_file_handbrake {
   typeset handbrake_video_encoder_opts=""
   typeset filetype=""
   typeset handbrake_audio_opts=""
+  typeset hb_profile=""
 
   # Set a variable that we will use later to determine if we found a handler for $profile or not
   typeset -i found_profile=0    
@@ -472,6 +477,9 @@ 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"
+
   # XVID profile
   if [[ "$profile" =~ "xvid" ]]; then
     found_profile=1
@@ -483,7 +491,7 @@ function encode_vob_file_handbrake {
     if [ "$profile" == "xvidvhq" ]; then
       handbrake_opts[0]="-f avi"
       handbrake_opts[1]="-b $target_bitrate"
-      handbrake_opts[2]="-e ffmpeg"
+      handbrake_opts[2]="-e xvid"
       handbrake_opts[3]="-T"
       handbrake_opts[4]="-5"
       handbrake_opts[5]="-8"
@@ -552,7 +560,6 @@ function encode_vob_file_handbrake {
     
     # extract the HandBrake Profile name from $profile
     hb_profile=`echo "$profile" | sed 's/hb_//g' | sed 's/_/ /g'`
-    handbrake_opts[0]="-Z \"$hb_profile\""
   fi
 
   # Make sure we found a handler for the given profile
@@ -562,14 +569,17 @@ function encode_vob_file_handbrake {
 
   # setup our audio option
   if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
-    handbrake_audio_opts="-E faac,ac3 -6 dpl2,6ch"
+    handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
   fi
-  if [ $audio_6ch -eq 1 ]; then
-    handbrake_audio_opts="-E ac3 -6 6ch"
+  if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
+    handbrake_audio_opts="-E ac3 -6 auto"
   fi
-  if [ $audio_2ch -eq 1 ]; then
+  if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
     handbrake_audio_opts="-E faac -6 dpl2"
   fi
+  if [ -n "$track" ]; then
+    handbrake_audio_opts="$handbrake_audio_opts -a $track"
+  fi
 
   # Convert our array of opts into a string
   for OPTS in "${video_encoder_opts[@]}"; do 
@@ -583,15 +593,21 @@ function encode_vob_file_handbrake {
   done
   
   # Execute the handbrake command to encode the video
-  echo -e "\n   Encoding: $handbrake_cli -i \"$vobfile\" -o \"$final_output_file\" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1" >> "$logfile"
-  $handbrake_cli -i "$vobfile" -o "$final_output_file" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1
-  handbrake_retval=$?
+  if [ -n "$hb_profile" ]; then
+    echo -e "\n   Encoding: $handbrake_cli -i \"$vobfile\" -o \"$final_output_file\" -Z \"$hb_profile\" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1" >> "$logfile"
+    $handbrake_cli -i "$vobfile" -o "$final_output_file" -Z "$hb_profile" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1
+    handbrake_retval=$?
+  else 
+    echo -e "\n   Encoding: $handbrake_cli -i \"$vobfile\" -o \"$final_output_file\" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1" >> "$logfile"
+    $handbrake_cli -i "$vobfile" -o "$final_output_file" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts $PASSES >> $encodelog 2>&1
+    handbrake_retval=$?
+  fi
   if [ $handbrake_retval != 0 ]; then
     fatal_and_exit "-E- Unhandled handbrake error"
   fi
 
   # Concatenate the encode log to our main log file, greping out unwanted lines
-  cat $encodelog | grep -v "Encoding" >> "$logfile" 
+  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"
 }
@@ -992,6 +1008,47 @@ function get_crop_from_vob {
   echo "   Setting mencoder crop filter to: $CROP"
 }
 
+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.
+  vob="$1"
+  handbrake_cli="$2"
+  aidcheck=`tempfile`
+  aid=$default_aid
+  alang=$default_alang
+  if [ $aid_override -lt 0 ]; then 
+    mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
+    grep -q "Found audio stream: $aid" $aidcheck
+    while [ $? == 1 ] && [ $aid -lt 159 ]; do
+      (( aid = aid + 1 ))
+      grep -q "Found audio stream: $aid" $aidcheck
+    done
+    [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
+  else 
+    aid=$aid_override
+  fi
+  # Now that we've found the right audio id, find the corresponding audio track in HandBrake
+
+  # convert the aid we found into hex
+  aid_hex=`echo "obase=16; $aid" | bc`
+  
+  # extract the audio streams in the vob
+  #ffmpeg -i "$vob" > $aidcheck 2>&1
+  $handbrake_cli --stop-at 0 -i "$vob" -o /dev/null -v 100 > $aidcheck 2>&1
+  
+  # find the stream that matches our aid
+  #stream=`grep "Stream.*\[0x$aid_hex\]" $aidcheck`
+  stream=`grep "scan: audio" $aidcheck | grep -n "" | grep "scan: audio 0x$aid_hex"`
+  # extract the track number that handbrake uses
+  #track=`expr match "$stream" '.*#[0-9]\.\([0-9]*\)'`
+  track=`expr match "$stream" '^\([0-9]*\):'`
+
+  if [ -n "$track" ]; then   
+    echo "-> Setting the audio ID to $aid. Setting the audio track to $track." | tee -a "$logfile"
+  fi
+}
+
 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.