+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
+}
+