Reducing the AC3 bitrate of a video file

I recently tried unsuccessfully to watch an HD movie on my MythTV box, and while the video was fine the audio (Dolby Digital) kept dropping out. The CPU load on my server was pretty low so this wasn’t the problem and I tried copying the file to the server itself rather than accessing it via NFS from my file server to rule out network bandwidth problems.

I then decided that maybe my Amp couldn’t handle the high AC3 bitrate (640kbps) of this file so I looked into how I could reduce it. After much googling and experimenting I finally managed to create a version of the file with a reduced AC3 bitrate, and thankfully, it played fine. As it turned out the movie was pretty crap, but you get that sometimes.

I put together this script that uses various freely available Linux tools to split the video file into separate audio and video components, re-encodes the audio and then puts it back together in case I need to do this again in the future.



cd /media/Temp

tcprobe -i $infile

echo Extracting AC3 soundtrack ...

tcextract -d2 -i $infile -a0 -x ac3 | tcextract -d2 -x ac3 -t raw > soundtrack.ac3

echo Extracting individual audio channels ...

export AC3_6CH=en
/share/bin/ac3dec -o null soundtrack.ac3

echo Converting individual audio files to WAV ...

for i in *.pcm
echo $i
sox -t raw -r 48000 -w -s $i `basename $i .pcm`.wav

echo Recreating AC3 soundtrack ...

rm -f 6ch.ac3
multimux -f en_l.wav en_c.wav en_r.wav en_ls.wav en_rs.wav en_lfe.wav

echo Creating Xvid AVI file ...

transcode -i $infile -p 6ch.ac3 -y xvid -e 48000,16,6 -E 48000,16,6 -A -N 0x2000 -o $outfile

rm -f *.pcm *.wav

The hardest part was extracting the individual audio tracks from AC3 and I eventually found this site which had a patched version of an AC3 decoder. I had to do a bit of work to get it to compile on a recent Linux system.

1 Comment

  • Steve says:

    I recently went to watch a video file that appeared to be corrupted and failed to play under Mplayer on Linux despite initial checking it with VLC on Windows. I ran this script on the avi file to see if it had any problems with it but it didn't.

    The good news was that the resultant file, whilst being smaller than the original, worked fine with Mplayer!

Leave a Reply