Bash script for converting all .wav files in a directory to .mp3
by Sebastian Benthall
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.
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.
Thanks, Paul!
You can do it with a one-liner on the command-line
$ for f in /dir/*.wav; do lame $f $f.mp3; done
Thanks friend, this is other solution:
find . -depth -iname “*.wav” -exec sh -c ‘lame -V0 -h -b 160 –vbr-new “$1” “${1%.wav}.mp3″‘ _ {} \;