Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I connect my PC to my home theater via SPDIF, so when watching video files that uses AAC 5.1 channels, it is downmixed to stereo and the surround channels are lost.

I would like to be able to convert all 5.1 AAC output to 5.1 AC3, so my HT properly plays 6 discrete chanels.

My current approach is to use avconv to convert my files's audio streams like this:

avconv -i <inputfile> -codec copy -codec:a ac3_fixed -b:a 448k <outputfile>

It works, but it has a few drawbacks:

  • All audio streams are converted to AC3, even the ones I don't need or want to, like stereo, DTS or AC3. I would like to convert only the AAC 5.1 ones. Command should be generic enough so I don't have to inspect the file or pick streams manually.

  • While conversion works fine, there are a few issues: volume is huge, loud as I've never seen before, and I fear some metadata is lost on conversion.

  • I must run this for every video I want to watch. It takes time and waste a lot of HDD space.

So, is there a solution that fixes at least one of these drawbacks?

Possible approaches:

  • Set OS (pulse audio?) to encode multichannel PCM to AC3 on-the-fly? (that would be amazing, since it would not only with videos but also for games.

  • Fix my command line with some smart parameters


share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can set codecs for individual streams in avconv with -c:n - where n is the number of the stream. Let's say that your AAC stream is stream 2 (so, the third stream in the container):

avconv -i input.mkv -c copy -c:2 ac3_fixed -b:2 448k output.mkv

Here's a quick & dirty script to find the stream number of an AAC stream, and feed that into avconv:


foo=$(avconv -i "$1" 2>&1 | grep aac | grep 5.1 | sed -e 's/    Stream #0.//' -e 's/(.*//')

avconv -i "$1" -c copy -c:$foo ac3_fixed -b:$foo 448k "AC3-$1"

exit 0

This will not work if you have more than one 5.1 AAC stream, and it will only work with file names that have spaces if you quote them. To use it, paste that code scrap into gedit & save it as 'aac-only-to-ac3', somewhere in your $PATH (like ~/bin). Make sure you make it executable.

Then, to use the script,

aac-only-to-ac3 input.mkv

and to do every MKV in a directory

for i in *mkv; do aac-only-to-ac3 "$i"; done
share|improve this answer
+1: Interesting approach of parsing avconv itself to find the stream number of a AAC 5.1 audio (if any). Multiple AAC 5.1 streams are not common, but I assume your script can be rearranged to use a loop to concatenate multiple -c:$foo ac3_fixed -b:$foo 448K statements? (as for the quotes, that's a shell implicit requirement, so don't worry). – MestreLion Mar 25 '13 at 12:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.