Digifesto


Home | About | Archives


Bash script for converting all .wav files in a directory to .mp3

April 16, 2013 1:33 am

I’ve been working with music files lately trying to get Steve Morrell‘s music online. In the process I’ve had to convert his albums, which I’ve ripped in .wav format, to .mp3.

To accomplish this, I’ve written a short bash script. It’s requires a number of tricks I wasn’t familiar with and had to look up.

#!/bin/bash

SAVEIF=$IFS
IFS=$(echo -en "\n\b")

for file in $(ls *wav)
do
  name=${file%%.wav}
  lame -V0 -h -b 160 --vbr-new $name.wav $name.mp3
done


IFS=$SAVEIFS

Though it isn’t recommended, I did the for loop on ls because I wanted to limit it to .wav files. But that means the script chokes on file names with spaces unless you swap out the IFS variable.

I used LAME for the conversion.

Posted by Sebastian Benthall

Categories: programming

Tags: , , , ,

4 Responses to “Bash script for converting all .wav files in a directory to .mp3”

  1. You don’t need IFS or `ls`. You get what you want by just doing: for file in *mp3

    Thanks for the %% tip, that’s a new one on me! Reading `man bash` now.

    By slinkp on April 17, 2013 at 12:32 am

    1. Thanks, Paul!

      By Sebastian Benthall on April 17, 2013 at 12:43 am

  2. You can do it with a one-liner on the command-line
    $ for f in /dir/*.wav; do lame $f $f.mp3; done

    By lightdee on July 3, 2013 at 4:27 pm

  3. Thanks friend, this is other solution:

    find . -depth -iname “*.wav” -exec sh -c ‘lame -V0 -h -b 160 –vbr-new “$1” “${1%.wav}.mp3″‘ _ {} \;

    By Tecnoplaga on October 8, 2013 at 10:00 am

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.