Calum wrote:on the other hand, void main, does this actually allow a whole directory of files to be done in one go?
No, the example script will not do that, you didn't ask for that. :)
i know, i know, but i do know nothing about scripting really, it's not logical to me yet.
could i just call this script 'wma2mp3' and then do "wma2mp3 *" and it would take care of all the files in the directory? or would i have to envoke it six times with six different filenames (assuming i have a half dozen files in the current directory). i know this is a really dumb question but i can't see how to distinguish this from the script.
Ooops, just realized I have used "wma" extensions rather than "wmv". I will change the examples. You could use the existing script to do this if you wrap it in a for loop on the command line like this:
$ for file in *.wma; do wma2mp3 $file; done
Or if you wanted to add that functionality right into the script you could:
wma2mp3:
- Code: Select all
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Syntax: `basename $0` wmvFilename(s)"
exit
fi
if [ ! -f "$1" ]; then
echo "File $1 not found!"
echo "Syntax: `basename $0` wmvFilename(s)"
exit
fi
for file in $*
do
mplayer -ao pcm -aofile ${file%%.wma}.wav $file
lame -h -b 192 ${file%%.wma}.wav ${file%%.wma}.mp3
done
Notice I changed the first error check to make sure there are "at least" 1 parameter. I also changed the "wmv" to "wma" wherever it occurred. I also added a for loop to take each parameter passed and convert it to mp3 so you should now be able to do:
$ wma2mp3 *.wma
The above command will only work on files that do not contain spaces in their names and the extesions must all be lower case "wma".
How does the above script work? Let's say you have 3 wma files in your directory called "file1.wma", "file2.wma", and "file3.wma". At the bash command line when you type "wma2mp3 *.wma" it (bash) will automatically expand the wildcard before executing the script, so as far as the script is concerned here is how you executed it:
$ wma2mp3 file1.wma file2.wma file3.wma
Now in the script $# would resolve to the number of args which in this case is "3". $0 resolves to the full name of the script, in this case might be "/usr/bin/wma2mp3". The "basename" command strips off the directory components from that file name so you are left with "wma2mp3".
The "$*" would be replaced with "file1.wma file2.wma file3.wma" so your for loop would end up looking like "for file in file1.wma file2.wma file3.wma" and execute the commands within the loop for each of the parameters individually placing each param in the variable called "file".
The "${file%%.wma}.mp3" is a bash trick to change the ".wma" portion of the variable "file" to ".mp3" (if what is contained in the variable ends with ".wma"). That way when the "lame" command is executed for instance, it has the proper parameters.
I think that's pretty much it, do you understand everything?