Hello everybody!
I just started Bash-scripting because I'm too lazy to the following by hand:
My music-collection contains several hundreds of CDs - already named & sorted correctly...BUT some of them are *.wma and I want them in *.mp3...so I thought about scripting...after some work (and with some Google-study ^^) I figured out a script doing what I want: renaming the file without uppercases & spaces, converting (Mplayer + LAME) and renaiming it again to the originial name but with *.mp3-ending instead of *.wma...I even found kinda workaround for smaller problems like mv-errors if the file was already small and without spaces or if there wasn't any *.wma at all...now I want to enhance functionality by running through subdirectories as well...Google often presented me solutions using find -type d and the exec-command to run my working script on every subdirectory, but to be honest: now I'm too aspiring to just give up!
I wrote the following:
Code:
#!/bin/bash
sdir=$PWD # storing start-directory,
find ./ -type d | # identifying all subdirectories
while read cdir # and parsing them
do # by
cd "$sdir${cdir:1}" # entering them (from startdirectory),
for wma in *.[Ww][Mm][Aa # extracting wmas
do # after
if [ "$wma" != "*.[Ww][Mm][Aa]" ] # checking for their existance
then # and
basename=`basename "$wma" .wma` # storing the orig. name without ending,
workname=`echo $wma | tr '[A-Z]' '[a-z]' | tr ' ' '_'` # removing uppercases & spaces
if [ "$workname" != "$wma" ] # to check wether
then # or not
mv "$wma" "$workname" # to set a workname
fi # before
# echo "converting $workname ..."
mplayer -vo null -vc dummy -af resamp le=44100 -ao pcm:waveheader $workname && lame -m s audiodump.wav $workname # ripping with Mplayer & encoding with LAME,
mv "$workname" "$basename.mp3" # restoring the orig name with mp3-ending
fi # and
done # after
if [ -e audiodump.wav ] # checking for its existance
then
rm audiodump.wav # removing the therefore used wav-file
fi
done
I'm testing on one folder ~/test
(2) with subdirs ~/test/Artist A
(0) ~/test/Artist B
(2) ~/test/Artist A/Disc 1
(>10) ~/test/Artist A/Disc 2
(4) ~/test/Artist B/Disc 1
(5) ~/test/Artist B/Disc 2
(10) and ~/test/Artist B/Disc 3
(>10).
(number of *.wma-files)If I'm using the echo "converting $workname ..."
instead of "mplayer - vo ..." it seems to work: every *.wma is found and echoed to get converted (even renaming is done)...but when I actually try the script as seen above, it stops after converting the two *.wma-files in ~/test as if the converting of those 2 files ends the reading from the find-piping...
How is that possible?
As you see I'm actual entering the directories, so the directory-names shouldn't cause any problem while converting...there isn't even any error-message: It just quits after the first while-loop...
