Converting .m4a files to mp3 (batch scripting)

Execute (3) scripts. I suppose this could be done all in one script, but sometimes one wants to examine the results after each phase. This will actually work (in principle) to do any command on all files in a directory that have a predictable pattern.

Don’t forget to chmod +x ./script-name on each.

Script 1

#!/bin/bash
#
# Dump m4a to wav (first step in conversion)
for i in *.m4a
do
mplayer -ao pcm "$i" -aofile "$i.wav"
done

Script 2

#!/bin/bash
#
#Second step... use lame to convert into .mp3

for i in *.wav
do
lame -h -b 192 "$i" "$i.mp3"
done

Script 3

#!/bin/bash
#All the m4a's are now mp3's, however..
#the file will look like "filename.m4a.wav.mp3"
#So, to clean that up we remove extraneous extensions.

for i in *.mp3
do
x=`echo "$i"|sed -e 's/m4a.wav.mp3/mp3/'`
mv "$i" "$x"
done