bashshellmp3wma

Converting *.wma to *.mp3 by SHELL-script with mplayer, lame, and find


I want to convert my older *.wma files into *.mp3. For that purpose I found a short script to convert with using mplayer + lame (found here: https://askubuntu.com/questions/508625/python-v2-7-requires-to-install-plugins-to-play-media-files-of-the-following-t).

This works fine in a single directory. Now I wanted to improve it that way, that it's able to work with 'find'. Its intended to find a *.wma-file and then calling the script to convert that file to *.mp3.

Here is the script:

FILENAME=$1
FILEPATH="$(dirname $1)"
BASENAME="$(basename $1)"

mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader "$FILENAME"
lame -m j -h --vbr-new -b 320 audiodump.wav -o "`basename "$FILENAME" .wma`.mp3"
echo "Path: $FILEPATH" # just to see if its correct
echo "File: $BASENAME" # just to see if its correct
rm -f audiodump.wav
rm -f "$FILENAME"

At the moment I'm dealing with the issue, that the script put the converted *.mp3 in the directory which the console is working with (e.g. /home/user/ instead of /home/user/files/ where the *.wma comes from).

What can I do to let the script putting the new *.mp3 into the same directory as the *.wma? If I want to use 'mv' within the script I get trouble with embedded spaces in the *.wma-filenames.

Thanks for any hints. I thought about setting the IFS to tab or newline, but I wounder if there is a better way to deal with this.


Solution

  • The problem is that basename is stripping both the .wma extension and the path leading to the file. And you only want the .wma stripping.

    So the answer is not to use basename and instead just do the .wma stripping yourself (with Parameter Expansion).

    outfile=${FILENAME%.wma}
    lame -m j -h --vbr-new -b 320 audiodump.wav -o "$outfile.mp3"
    

    (Note that I used lowercase $outfile. Generally $ALL_CAPS variables are reserved for the shell/terminal/environment and should be avoided in scripts.)