Wed Aug 12 20:13:26 PDT 2015

Script to Convert WMA Files to MP3 Format

I changed work computers recently and on my new computer I needed to convert 16 CDs to .mp3 format. I didn't want to install new programs in the process, I just wanted to painlessly carry out the conversion. Putting the first CD into the drive led to Windows Media Player being launched and offering to 'Copy from CD' - so I accepted that offer.

After a little whirring, what I ended up with was a new 'Unknown Artist', and the content of the CDs as '.wma' files under "My Documents\My Music". Well, not exactly perfect because I prefer .mp3 files (because these are the most likely to work in any given mp3 player). Looking around at the options in Windows Media Player interface indicated that Microsoft were being typically unhelpful in not allowing users to store their music in formats other than .wma. However, using the various tools added to this machine's cygwin to deal with video, etc., I found it was easy to carry out the conversion. The conversion is a little slow, so rather than navigate all those silly directory names (containing irritating spaces...) I wrote a short script which traverses the "My Music" folder and converts .wma files to their more useful .mp3 cousins. Here is the script.

scandir () {
for item in *
  if [ -f "$item" ] ; then
    nfile=`expr $nfile + 1`
    EXT=`echo "$item" | rev | cut -c 1-3 | rev`
    if [ $EXT = "wma" ]
      ld2=`echo "$curdir"/"$item" | wc -c`
      ld2=`expr $ld2 - 5`
      echo "Operating on:"
      echo "$curdir"/"$item"
      NEWNAME=`echo "$curdir"/"$item" | cut -c-${ld2}`.mp3
      echo "Will create:"
      echo $NEWNAME
      if [ -f "$NEWNAME" ]
        echo "$NEWNAME exists - no action taken"
        ffmpeg -i "$curdir"/"$item" tmp.wav
        lame -h tmp.wav "$NEWNAME"
        rm -f tmp.wav
  elif [ -d "$item" ] ; then
    cd "$item"
    ndirs=`expr $ndirs + 1`
    cd ..
ld1=`echo $startdir | wc -c`
ld1=`expr $ld1 + 1`
echo "Initial directory = $startdir"
echo "Total directories searched = $ndirs"
echo "Total files = $nfile"

Comments are closed

If you would like to get in touch with me, please mail zfs at

recent comments

Posted by ZFS | Permanent link | File under: bash
[StumbleUpon] [Digg] [Reddit] [Facebook] [Google]