Ubuntu – Script: Recursively convert wma files to MP3, then remove WMA files

bashconvertmp3scriptswma

I'm trying to fix a script that recursively searches a directory tree for WMA files – converting them to MP3 files, and then removing the WMA files, leaving the converted MP3 files in their place. With some help and research this is where I'm at:

   #!/bin/bash


shopt -s globstar


for f in **/*.[Ww][Mm][Aa]
do
    path=${f%/*}
    filename=${f##*/}
    new=${filename// /_}  # space -> underscore
    new=${new,,}             # lowercase
    mv -v -- "$f" "$path/$new"
done


for f in **/*.wma
do
    mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader "$f" && lame -m j -h --vbr-new -b 320 audiodump.wav -o "`basename "$f" .wma`.mp3"
    rm -f audiodump.wav
    rm -f "$f" "${f/%.wma}"
done 

This script seems to only work on 14.04 (not on 15.10). At this point it is able to traverse sub directories, convert wma files to MP3 and delete the respective WMA files. The issue is that the MP3 files are created in the MAIN directory and not in the directories of the respective WMA files.

Best Answer

  • Elsewhere you're using "$f" without basename, so that path to the file is given. It's only in the lame command that you're using basename. With basename, the directory components will be lost. The output file would be created in which ever directory the command ran in.

    If you just want to replace a .wma extension with .mp3, use "${f%.wma}.mp3". That should retain the path.