+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 duration:1 -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"`
+ stream=`grep "add_audio_to_title:" $aidcheck | grep -n "" | grep "add_audio_to_title:.* stream 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
+}
+