Fixed bug in specifying audio tracks for Handbrake
[rip_dvd/.git] / rip_dvd
diff --git a/rip_dvd b/rip_dvd
index 6846ba39c61276c6b2aedfadde15fa5d457959df..ab0d4155c4e1a8383876ecccbd054968fb20fe57 100755 (executable)
--- a/rip_dvd
+++ b/rip_dvd
@@ -43,6 +43,7 @@ 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
@@ -142,6 +143,7 @@ function usage() {
     echo >&2 "                 2) If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
     echo >&2 "                 3) If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
     echo >&2 "                 4) If dvdname is a full path to an ISO file, it will be ripped as a DVD instead of $dev"
+    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)"
@@ -268,6 +270,7 @@ fi
 [[ "$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" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing encoder: $handbrake_mp4" && exit
+[[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
 
 ##############################################################################################
 
@@ -471,6 +474,9 @@ function encode_vob_file_handbrake {
     PASSES=""
   fi
 
+  # get our audio track from the VOB file
+  get_audio_track_from_vob "$vobfile"
+
   # XVID profile
   if [[ "$profile" =~ "xvid" ]]; then
     found_profile=1
@@ -561,14 +567,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,none"
   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 none"
   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 
@@ -991,6 +1000,43 @@ 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"
+  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
+  
+  # find the stream that matches our aid
+  stream=`grep "Stream.*\[0x$aid_hex\]" $aidcheck`
+  # extract the track number that handbrake uses
+  track=`expr match "$stream" '.*#[0-9]\.\([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.