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.
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