Skip to Content

Converting an audio CD to MP3's

I recently needed to convert a number of files in multiple directories from WAV format to MP3 format. I opted to do this with a script, seeing as most of the directories contained 99 files. Here is the bash script I came up with:

#!/bin/bash

SRCDIR=`pwd`
MP3DIR=$SRCDIR/mp3

for d in Disk*;
do
        WORKDIR=$MP3DIR/"$d"
        mkdir -p "$WORKDIR"
        cd "$d"
        for f in *.wav
        do
                b=`basename "$f" .wav`.mp3
                lame -V2 "$f" "$WORKDIR/$b"
        done 

        cd $SRCDIR
done 

And a quick review of what we are doing:

  • First we create a couple of variables. The SRCDIR variable stores what directory we are running the script in. The second stores the destination directory for our converted files. (we want to keep the same directory structure, but under a different directory for easy of use)
  • Next we find all the directories that contain our WAV files and loop over that list. In my case they were all in the form of "Disk 1", with only the number changing.
  • Then we take our current directory and combine it with the MP3DIR variable to come up with our current working directory. And then we create that working directory. (Er, in short, we are creating our current directory under the MP3DIR directory...)
  • And we change into our current directory. I could have done this without changing directories, but this helps keep the paths clean.
  • Next we loop over all the WAV files in the current directory.
  • We take the current file (represented by the $f variable) and determine it's name without the .wav extension, and add .mp3 onto the end (so instead of FILE.wav we get FILE.mp3). This is the destination filename.
  • Then we run the command to do the format conversion. I opted to use the Lame MP3 Encoder. There are many ways to do this, and other options besides lame though.
  • Once all the files in the current directory are done, we change directories back into our original directory (so that the "cd" command will work as expected when it is hit next time through the loop).

And that is it. There are some flaws/limitations to keep in mind here.

  • This script does not handle nested directories at all. At best, it looks at the directories in the current location, but no deeper than that.
  • The script as written expects directory names starting with "Disk".
  • The output goes into a new directory called MP3.

With those limitations in mind, you should be able to massage the script to suit your needs.

As for WHY I did this, well, let's just say that putting 12 audio disks onto a single data CD is so much more convenient than lugging around and possibly wrecking 12 disks that would be a pain to replace. this way the originals get put on a shelf only to ever be used again IF the data CD is damaged and the converted files have been lost.

Copyright law is a finicky beast. This process may or may not be "legal" in your area. Use at your own risk. I however consider this a reasonable backup process which *should* be permissible under copyright law. Format shifting for personal use is perfectly OK in my view of the world.