Saturday, September 20, 2008

WMA to MP3 Script for Linux

I ran into a Rap artist who has his files as *.wma files for download.
So I created a bash/zenity script to convert those files to *.mp3s
#!/bin/bash
choice="`zenity --file-selection --directory --title="Select the directory with your wma files"`"
artist="`zenity --entry --title="Artist Name" --text="Enter Artist Name"`"
genre="`zenity --entry --title="Genre" --text="Enter Genre" --entry-text="Rap"`"
album="`zenity --entry --title="Album Name" --text="Enter Album Name"`"
year="$(zenity --entry --title="Album Year" --text="Enter Album Year" --entry-text="`date +%Y`")"


case $? in
0)
cd "$choice"
if [ $PWD = "$choice" ]; then
if [ ! -d 'workdir' ]; then
mkdir workdir
fi
if [ ! -d 'mp3s' ]; then
mkdir mp3s
fi
if [ ! -d 'wavs' ]; then
mkdir wavs
fi
zenity --info --text="This will take some time. Please be patient."

for file in *.wma
do cp "$file" ./workdir/`echo $file | sed -e 's/ */_/g' -e 's/[-]/_/g' -e 's/[.][.]//'`
done

cd workdir
for j in *.wma
do mplayer -quiet -vo null -vc dummy -af resample=44100 -ao pcm:waveheader:file=../wavs/`basename $j .wma`.wav $j &> /dev/null
done
for k in ../wavs/*.wav
do lame -b 192 $k ../mp3s/`basename $k .wav`.mp3 2>&1 | (zenity --progress --auto-close --pulsate --percentage=0 --text=`basename $k .wav`)
done
curdir=`basename $PWD`
if [ $curdir = 'workdir' ]; then

cd ../
count=`ls -1 mp3s/*mp3|wc -l`
#rm -fR workdir
#rm -fR wavs
cd mp3s
for l in *.mp3;do mp3info -a "$artist" -g "$genre" -l "$album" -y "$year" -n "$(echo $l|awk -F'_' '{print $1}')" -t "$(echo $l|sed -e 's/.._//' -e 's/.mp3//g' -e 's/[_]/ /g')" $l ;done
zenity --info --text=$count" files converted"

fi

cd
fi


;;
1)
zenity --error --text "No Directory Selected"
;;
-1)
zenity --error --text "No Directory Selected"
;;
esac






Needed packages:
  • zenity
  • mplayer (with windows codecs/etc)
  • lame
  • mp3info

1 comment:

RandyS said...

put the location of bash at the top of your script as follows:

#!/bin/bash

(change the location to the location of your bash `which bash`)

chmod your script to 755

and it will run without source:

./myscript.sh