+# reduce the size of the mkv src file using handbrake
+function encode_mkv_file_makemkv {
+ src="$1"
+ dst="$2"
+ tmpdst=""
+
+ # Convert our ISO to a MKV if it is a real iso
+ if [[ $real_isofile -eq 1 ]]; then
+ tmpdst="$tmpdir/$dvdname.mkv"
+ make_dvd_mkv_image "$tmpdst" "iso" "$src"
+ src="$tmpdst"
+ fi
+
+ # Set a variable that we will use later to determine if we found a handler for $profile or not
+ typeset -i found_profile=0
+
+ # Globals
+ typeset quality=""
+ typeset handbrake_audio_opts=""
+ typeset handbrake_cli=""
+
+ # Predefined Handbrake Profile Handling
+ if [[ "$profile" =~ "mkvvhq" ]]; then
+ found_profile=1
+ handbrake_cli="$handbrake_mp4"
+ final_output_file="$dst"
+ quality="0.9"
+ fi
+ if [[ "$profile" =~ "mkvhq" ]]; then
+ found_profile=1
+ handbrake_cli="$handbrake_mp4"
+ final_output_file="$dst"
+ quality="0.85"
+ fi
+
+ # setup our audio option
+ if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
+ 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 auto"
+ fi
+ if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
+ handbrake_audio_opts="-E faac -6 dpl2"
+ fi
+
+ # find out what our audio track is
+ get_audio_track_from_mkv "$src" "$handbrake_cli"
+ if [ -n "$track" ]; then
+ handbrake_audio_opts="$handbrake_audio_opts -a $track"
+ track=""
+ fi
+
+ # Execute the handbrake command to encode the video
+ echo -e "\n Encoding: $handbrake_cli -i \"$src\" -o \"$dst\" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 -m"
+ $handbrake_cli -i "$src" -o "$dst" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 | tee -a $encodelog 2>&1
+ handbrake_retval=$?
+
+ if [ $handbrake_retval != 0 ]; then
+ fatal_and_exit "-E- Unhandled handbrake error"
+ fi
+
+ # Extract the chapters from the src and apply to the dst
+ chapter_tmpfile="$tmpdir/$dvdname.xml"
+ $mkvextract chapters "$src" > "$chapter_tmpfile"
+ $mkvpropedit -c "$chapter_tmpfile" "$dst"
+
+ # find out what our default_alang subtitle track is in the src
+ get_subtitle_track_from_mkv "$src" "$handbrake_cli"
+
+ # Extract the subtitles from the src and apply to the dst
+ srt_tmpfile="$tmpdir/$dvdname.srt"
+ sub_tmpfile="$tmpdir/$dvdname.sub"
+ idx_tmpfile="$tmpdir/$dvdname.idx"
+ $mkvextract tracks "$src" $track:$srt_tmpfile
+ $mkvmerge -o "$dst.subs" "$dst" $idx_tmpfile
+ mv "$dst.subs" "$dst"
+
+ # Set the default subtitle track to "no subtitles" in the dst
+ $mkvpropedit "$dst" --edit track:s1 --set flag-default=0
+
+ # Concatenate the encode log to our main log file, greping out unwanted lines
+ 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"
+
+ # Remove intermediary file if we created one
+ if [ $keep_intermediate_files -eq 0 ]; then
+ [[ -e "$tmpdst" ]] && rm -f "$tmpdst"
+ rm "$chapter_tmpfile"
+ rm "$idx_tmpfile" "$sub_tmpfile"
+ else
+ echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
+ echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
+ [[ -e "$tmpdst" ]] && echo "-> Keeping intermediate MKV file: $tmpdst" | tee -a "$logfile"
+ fi
+}
+