From 07092917b95a335109b0118d95a9e1b5c58eebfb Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <ajp@pippins.net>
Date: Mon, 8 Feb 2010 10:08:12 -0700
Subject: [PATCH] Another round of tweaks to the handbrake integration.

---
 README            |  9 ++++++---
 rip_dvd           | 24 +++++++++++++++---------
 rip_dvd.conf.dist |  1 +
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/README b/README
index e53819b..0f2d206 100644
--- a/README
+++ b/README
@@ -11,6 +11,9 @@ volname makexml lsdvd dvdauthor gddrescue dvdbackup mencoder mplayer mkisofs
 
 Optional Dependencies:
 lookup imdb info/posters for mythvideo: http://www.mythtv.org/wiki/Fill_mythvideo_metadata.pl
+HandBrake CLI video encoder (version 0.9.3 required for xvid encoding): google to find it
+HandBrake CLI video encoder (version >=0.9.4 required for mp4 encoding): http://handbrake.fr/
+You can also get these HandBrake versions from here: https://www.pippins.net/cgi-bin/index.pl?/www/files/video_utils/handbrake
 
 Windows Dependencies:
 To play back the videos in windows that use the divx encoding profiles, you'll
@@ -33,9 +36,9 @@ To pull updates in the future of this project into your local directory:
 To see the changelog for what you have pulled, run this command:
 > git whatchanged
 
-----------------------------------------------
-ENABLING XVID TO ENCODE USING MULTICORES
-----------------------------------------------
+-------------------------------------------------
+ENABLING MENCODER XVID TO ENCODE USING MULTICORES
+-------------------------------------------------
 The libxvidcore4 package that you can get through ubuntu or debian
 is version 1.1.0. They didn't enable multi-core encoding using this
 library until version 1.2.0. Since you can't get that in a package,
diff --git a/rip_dvd b/rip_dvd
index 601bde7..77edf33 100755
--- a/rip_dvd
+++ b/rip_dvd
@@ -166,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)"
@@ -230,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
 
@@ -269,6 +270,7 @@ 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
 
@@ -475,8 +477,8 @@ function encode_vob_file_handbrake {
     PASSES=""
   fi
 
-  # get our audio track from the VOB file
-  get_audio_track_from_vob "$vobfile"
+  # 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
@@ -567,10 +569,10 @@ 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,none"
+    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 none"
+    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"
@@ -605,7 +607,7 @@ function encode_vob_file_handbrake {
   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/
/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
   cat $encodelog | grep "Encoding:" | sed 's/
/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
 }
@@ -1010,6 +1012,7 @@ 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
@@ -1030,13 +1033,16 @@ function get_audio_track_from_vob {
   aid_hex=`echo "obase=16; $aid" | bc`
   
   # extract the audio streams in the vob
-  ffmpeg -i "$vob" > $aidcheck 2>&1
+  #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 "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]\.\([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"
diff --git a/rip_dvd.conf.dist b/rip_dvd.conf.dist
index 4c7d0da..6f6b80f 100644
--- a/rip_dvd.conf.dist
+++ b/rip_dvd.conf.dist
@@ -56,6 +56,7 @@ mencoder_threads=2
 # handbrake parameters
 # handbrake version 0.9.3 was the last one to support xvid encoding
 # handbrake version 0.9.4 is all about mp4 encoding
+# handbrake executable name (must be found within your PATH)
 handbrake_xvid="HandBrakeCLI-0.9.3"
 handbrake_mp4="HandBrakeCLI-0.9.4"
 
-- 
2.34.1