One of my clients has a site where they offer their archived radio shows for downloading. The files they provide us come from directly from the radio station. They are already in MP3 format, but they are encoded at an insanely high bitrate for a talk show, and are in stereo to boot. This can eat up a lot of bandwidth unnecessarily.

There are over 100 files, so opening each one in an audio tool such as Audacity or SoundBooth to re-compress would be quite tedious. Here is a short script I wrote to re-encode the files. Note that I did this on our Linux server, but since Perl and lame/notlame are both available for Windows you shouldn’t have a problem doing it there either.

Perl was already installed, and I installed notlame by downloading the RPM and running rpm -i notlame-3.96.1-1.i686.rpm.

Here is the script:

#!/usr/bin/perl

$sourcedir = '/tmp/audio/input';
$destdir = '/tmp/audio/output';

opendir(DIR,$sourcedir);
@files = grep !/^\./, readdir(DIR);
closedir(DIR);

foreach $file (@files) {
  if ($file =~ /mp3/i) {
    $cmd = "nice notlame -h -b 32 -m m '$sourcedir/$file' '$destdir/$file'";
    `$cmd`;
    }

  }

The script reads in files from the sourcedir, and creates new compressed files in the destdir. Note the $cmd towards the end – the nice part of the command tells the system to lower the CPU priority on this task, so it doesn’t effect ColdFusion or anything else running on the server. The -h tells lame to go slower, for a higher quality end result, the -b 32 indicates a bitrate of 32kbps, and the -m m sets the mode to mono.

In the end a 43MB file gets reduced to an 8.5MB file, with almost no noticeable loss in quality.