How to create multichannel (5.1) AC3 audio (and video) in Linux

December 12, 2007

AC3 is a compressed (lossy) audio format that allows multichannel (from 1 to 6 channels) and is very used in DVDs and video (mpeg2 and mpeg4) files.

Surprisingly, I found creating multichannel ac3 files in Linux much harder that expected. This is basically because most of my searches led to experiences of ffmpeg and no other tools. However, the fact is that encoding more than 2 channels don’t work (at least I wasn’t able) using the ffmpeg version that comes with Ubuntu Gutsy.

For example, this seemingly correct ffmpeg command produced a ac3 file with correct metadata, but totally silent and useless:

ffmpeg -i l.wav -i r.wav -i sl.wav -i sr.wav -i c.wav -ab 192k -ar 48000 -ac 5 -acodec ac3 -y surround.ac3

Luckily there is an alternative to ffmpeg in the open-source/linux arena which is aften. And it is, actually, very flexible and overall better ac3 encoder.

It is still not packaged for Ubuntu, so you’ll need to download and build it from sources

svn co https://aften.svn.sourceforge.net/svnroot/aften aften

Change to the aften director,
create a new directory, e.g. default, and change into it.
Then compile:

cmake .. -DCMAKE_INSTALL_PREFIX:STRING="/usr/local"
make
sudo make install

To create a multichannel .ac3 you need to create first a multichannel wav.
Of course sox comes very handy:

sox -M L.wav R.wav C.wav SL.wav SR.wav surround.wav

And now the ac3 encoding

aften -b 448 -cmix 0 -smix 0 -dsur 2 -acmod 7 surround.wav surround.ac3

That’s all. Now you’re ready to use the created 5.0 ac3 file.

Since the options are not self explaining here goes a brief explanation of the used options:

  • -b 448 — Bitrate in kbps (4 channels: 384 kbps, 5 channels: 448 kbps)
  • -smix 2 — Surround mix level ( 0: -3dB, 1: -6dB, 2: 0 )
  • -dsur 2 — (2: Dolby surround encoded, 0: not indicated, 1: not Dolby surround encoded)
  • -acmod 7 — Audio coding mode, or channel order (7: L,R,C,SL,SR)

Now let’s put some images to the sound.

Embed the created .ac3 audio to a video

It is very easy to do using avidemux, a very nice graphical user interface application designed to do encoding and cutting video tasks. Use the menus to Open and choose the video file. Choose Audio, Main Track choose external AC3 and Save Video

Or, alternatively, you can use a quick command line with mencoder:

mencoder -ovc copy video.avi -audiofile surround.ac3 -oac copy -o newvideo.avi

Now it’s ready to play the video with the 6 audio channels. For example with a (jack enabled) mplayer

mplayer newvideo.avi -ao jack -channels 6

Changes: small update on Des 16

About these ads

7 Responses to “How to create multichannel (5.1) AC3 audio (and video) in Linux”


  1. [...] 16, 2007 In my last how-to we played a multichannel video with surround audio (coded with ac3) using mplayer and jack. Jack [...]


  2. [...] Artikel in meinen Blod darüber zu schreiben. Die letzten Informationen über AC3 habe ich einem ausführlicheren Artikel zu aften [...]

  3. gdk1 Says:

    Thanks for the information.

  4. bob Says:

    I was just wondering where you got the 5 wav files:
    L.wav R.wav C.wav SL.wav SR.wav and what is surround.wav

    thanks from Canada
    Bob

  5. Jean Says:

    Hi,

    just a little side note: This might be wrong. Because: You haven’t added a LFE Channels (5.1 = 6 Channels(5+1)) and the Channel Order should be equal to what Dolby uses. Thus you would come up with:

    Left Front, Center, Right Front, Surround Left, Surround Right, LFE

    Thus i’m wondering whether you ever tried that yourself in a 5.1 System. However – Apart from that, nice article. Didn’t knew about whats possible with sox and didn’t knew aften, cool tools.

    Cheers


  6. Your webpage is a very good summary of your activities. great!

    Wiko looks like very interesting.

  7. a Says:

    Thanks. you also can use separate mono channels:

    aften -b 448 -cmix 0 -smix 0 -dsur 2 -acmod 7 -lfe 1 -ch_fc front_C.wav -ch_fl front_L.wav -ch_fr front_R.wav -ch_lfe sub.wav -ch_sl surround_L.wav -ch_sr surround_R.wav surround.ac3


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: