Manually Encoding Audio

If the built-in codec (see news) don't cut it for you, this is how you encode the audio and video separately. FFMpeg has a list of the order of quality they consider should be used.

# Encode the video with --audio none
HandBrakeCLI -i source.mkv -o output.mkv -e x265 --encoder-preset slower -q 25 --nlmeans --nlmeans-tune film --subtitle 1 --audio none

# Extract the raw ac3 or dts track
mkvextract tracks source.mkv 1:output.ac3

# Convert to wav format for compatibility with encoders
ffmpeg -i output.ac3 output.wav

# Encode with fdkaac for example
fdkaac -m 4 output.wav

# Add the track
mkvmerge -o output.aac.mkv output.mkv --language 0:eng --track-name 0:English output.m4a

Now in most cases, you'll use Opus. 

ffmpeg -i source.dts -c:a libopus -af "channelmap=channel_layout=5.1" -vbr on -b:a 256k source.opus

mkvmerge -o final_output.mkv vide_no_sound.mkv --language 0:eng --track-name 0:English source.opus

Plex currently doesn't hanlde 5.1 opus on fire tv, so you may want to jump to fdk_aac or use stereo with pro logic

ffmpeg -i source.dts -c:a libopus -ac 2 -af "aresample=matrix_encoding=dplii" -vbr on -b:a 192k source.opus

'opusenc' doesn't accept DTS  directly, so you have to use something to convert and explicitly state the rear channels are 5.1

Error Message

Invalid channel layout 5.1(side) for specified mapping family -1.

This has to do with rear vs side channels and is a recent issue for ffmpeg or opus not just using some defaults.

Error Messages

Error: Extraction of track ID 1 with the CodecID 'A_MS/ACM' is not supported.

This happens when you have a .mkv with uncompressed surround sound - LPCM. It seems that MakeMKV and mkvtoolnix don't agree on the most correct way to handle it.  You can modify the header though and ffprobe will still report it as 6 channels

mkvpropedit CrimsonTide.mkv --edit track:=2 --set codec-id=A_PCM/INT/LIT