0

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.

6
  • If you're using basename, the directory components will be lost. It would be created in which ever directory the command ran in.
    – muru
    Dec 17, 2015 at 5:46
  • According to the script shouldn't the script be running in each recursive directory?
    – Spency
    Dec 17, 2015 at 5:59
  • Since you haven't used cd anywhere, nope.
    – muru
    Dec 17, 2015 at 6:00
  • The script is able to convert and delete files in directories it hasn't cd'd into. Is there some line of code I can use to move the newly created files into the directories of the files that were used to make the converted files?
    – Spency
    Dec 17, 2015 at 6:07
  • Well, 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. If you just want to strip off a .wma suffix, use "${f%.wma}.mp3". That should retain the path.
    – muru
    Dec 17, 2015 at 6:09

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .