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.
basename
, the directory components will be lost. It would be created in which ever directory the command ran in.cd
anywhere, nope."$f"
withoutbasename
, so that path to the file is given. It's only in thelame
command that you're usingbasename
. If you just want to strip off a.wma
suffix, use"${f%.wma}.mp3"
. That should retain the path.