Stop Microsoft

Operating Systems => Linux and UNIX => Topic started by: Agent007 on 28 January 2003, 07:39

Title: Converting multiple files to mp3
Post by: Agent007 on 28 January 2003, 07:39
hi all,

I have about 100 wav files in a folder....Need to convert them to mp3. I'm using lame to do the conversion.

The systax used is:-

#lame   --preset extreme artist.wav artist.mp3

After each song is over, I have to keep changing the song names. Is it possible to make lame automatically goto the next wav file and select the same name for the mp3 file? until all the wav files are over?

thanks,
007
Title: Converting multiple files to mp3
Post by: Master of Reality on 28 January 2003, 08:16
a simple bash script will do it for you. I'm not very experienced with bash scripts so i'm not sure how to do it.
Title: Converting multiple files to mp3
Post by: Stryker on 28 January 2003, 08:37
how about:

find -name "*.mp3" | while read item; do lame --preset "$item" `echo "$item" | sed 's/.mp3/.wav/g'`;done

will that work?

or maybe:

find -name "*.mp3" | while read item; do lame --preset extreme "$item" `echo "$item" | sed 's/.mp3/.wav/g'`;done

depending on the parameters of the lame command

[ January 28, 2003: Message edited by: Stryker ]

Title: Converting multiple files to mp3
Post by: voidmain on 28 January 2003, 08:41
quote:
Originally posted by Agent007:
hi all,

I have about 100 wav files in a folder....Need to convert them to mp3. I'm using lame to do the conversion.

The systax used is:-

#lame   --preset extreme artist.wav artist.mp3

After each song is over, I have to keep changing the song names. Is it possible to make lame automatically goto the next wav file and select the same name for the mp3 file? until all the wav files are over?

thanks,
007



How about:

$ for i in *.wav; do lame --preset $i ${i%%.wav}.mp3; done
Title: Converting multiple files to mp3
Post by: Stryker on 28 January 2003, 08:54
quote:
Originally posted by void main:


How about:

$ for i in *.wav; do lame --preset $i ${i%%.wav}.mp3; done



make me look stupid   (http://tongue.gif)  
i think either one will work, but i am unfamiliar with void main's methods.
Title: Converting multiple files to mp3
Post by: voidmain on 28 January 2003, 21:06
Yeah, the way I usually do it is using bash's builtin text replacement. It would be valuable to get familiar with this as it makes life really easy for many situations like this. See the section on "Parameter Expansion" in the bash man page (http://voidmain.kicks-ass.net/man/?parm=bash&docType=man). This way you call no external programs except for the one you want to run to perform the actual task you need to have done.
Title: Converting multiple files to mp3
Post by: Agent007 on 28 January 2003, 11:36
Void Main,

A minor glitch...The wav files which have names separated by spaces do not get processed.

Example,
this is a song.wav

where should i make the changes?

thanks,
007

 
quote:
Originally posted by void main:


How about:

$ for i in *.wav; do lame --preset $i ${i%%.wav}.mp3; done

Title: Converting multiple files to mp3
Post by: Stryker on 28 January 2003, 11:37
i believe my example solves that problem, although i am curious to void main's solution.
Title: Converting multiple files to mp3
Post by: flap on 28 January 2003, 12:51
quote:
Originally posted by Agent007:
Void Main,

A minor glitch...The wav files which have names separated by spaces do not get processed.

Example,
this is a song.wav

where should i make the changes?

thanks,
007

 



Use Stryker's solution instead. The difference between that one and Void's is that it will work with files whose names contain spaces.
Title: Converting multiple files to mp3
Post by: beltorak0 on 28 January 2003, 16:46
yeah, unfortunately, the "for name in *" command will separate the input list on newlines and spaces; unfortunately I have found no workaround for this limitation....  even setting $IFS to a newline doesn't do it.

And i think, stryker, that you have your names backwards; your commands find all mp3's and rename them to wav's (not sure how lame would react if it was given an mp3 as the imput and you told it to recode to mp3);  shouldn't it be:
Code: [Select]

or, this might be a little simpler and not have to call sed each time:
Code: [Select]
Title: Converting multiple files to mp3
Post by: flap on 28 January 2003, 17:50
Actually a better solution again would be:

find -name "*.wav" | while read item; do oggenc "$item" -o "${item/.wav/}.ogg";done
Title: Converting multiple files to mp3
Post by: voidmain on 28 January 2003, 19:12
How can anyone not like a thread like this.  (http://smile.gif)
Title: Converting multiple files to mp3
Post by: Master of Reality on 28 January 2003, 22:25
would this work?
I just modified a script i had for converting things to thingers.
Code: [Select]

[ January 28, 2003: Message edited by: The Master of Reality / Bob ]

Title: Converting multiple files to mp3
Post by: flap on 28 January 2003, 23:15
Try it.

No it won't, btw.

This bit

#replace spaces with underscores
for i in *.wav; do mv "$i" `echo $i | tr ' ' '_'`; done

won't work, for reasons stated above.
Title: Converting multiple files to mp3
Post by: voidmain on 29 January 2003, 00:23
Actually the spaces are a problem in the lame command, not the "for" loop. This works for me:

$ for i in *.[Ww][Aa][Vv]; do lame --preset extreme "$i" "${i%%.[Ww][Aa][Vv]}.mp3"; done

Which also accounts for case. The quotes should fix the problem with spaces in the parameters for lame.

[ January 28, 2003: Message edited by: void main ]

Title: Converting multiple files to mp3
Post by: voidmain on 29 January 2003, 00:29
quote:
Originally posted by void main:
Actually the spaces are a problem in the lame command, not the "for" loop. This works for me:

$ for i in *.[Ww][Aa][Vv]; do lame --preset extreme "$i" "${i%%.[Ww][Aa][Vv]}.mp3"; done

Which also accounts for case. The quotes should fix the problem with spaces in the parameters for lame.



Or to do everything in the current directory and all subdirectories:

$ for i in `find -iname '*.wav'`; do lame --preset extreme "$i" "${i%%.[Ww][Aa][Vv]}.mp3"; done

[edit]
This last example doesn't work. Sorry about that!
[/edit]

[ January 28, 2003: Message edited by: void main ]

Title: Converting multiple files to mp3
Post by: flap on 29 January 2003, 00:38
I don't think that last one works with spaces.
 You're right though that "for i in *.wav" will work if you enclose the "$i" in quotes, but that won't work if you're using "for i in `find..."
Title: Converting multiple files to mp3
Post by: voidmain on 29 January 2003, 00:51
You are right, and the bad part is I actually knew that wouldn't work, and I actually tested it and proved that it didn't work, and then I posted it anyway as I was distracted on another forum...

[ January 28, 2003: Message edited by: void main ]

Title: Converting multiple files to mp3
Post by: Agent007 on 30 January 2003, 12:04
hi all,

Thanks a tonne to beltorak, the following syntax works superbly!! Encoding wav files to mp3 is just a breeze!

 
quote:

find -name "*.wav" | while read item; do lame --preset extreme "$item" "${item/.wav/}.mp3";done




Could someone pls explain as to what this portion of the syntax means? Whats the dollar sign for?
"$item" "${item/.wav/}.mp3"

thanks,
007

PS:- I try to stay away from sed, since its a bit complicated to remember the characters and stuff.....

[ January 30, 2003: Message edited by: Agent007 ]

Title: Converting multiple files to mp3
Post by: flap on 30 January 2003, 21:56
$ is used to identify a variable. In this case $item will refer to each wav file returned by the 'find' command.
The "${item/.wav/}.mp3" bit is used to specify the output filename. This removes ".wav" from the filename and then adds ".mp3" onto the end.
Title: Converting multiple files to mp3
Post by: beltorak0 on 31 January 2003, 21:29
sure man.  glad it worked  (http://smile.gif) .

Until Stryker posted his answer tho, i coudn't figure out how to get around the whole "for FILE in *" problem....  ty Stryker.
Title: Converting multiple files to mp3
Post by: Stryker on 31 January 2003, 10:16
quote:
Originally posted by beltorak:
sure man.  glad it worked    (http://smile.gif)   .

Until Stryker posted his answer tho, i coudn't figure out how to get around the whole "for FILE in *" problem....  ty Stryker.



no problem man, that's what i'm here for. it was strange too, this was posted just a few hours after i was just done learning some of the sed command and the while command.

and yes, i was thinking you were going from mp3 to wav, my mistake, but you get the idea.

[ January 31, 2003: Message edited by: Stryker ]

Title: Converting multiple files to mp3
Post by: LorKorub on 31 January 2003, 14:15
Speaking of going from *.mp3 to *.wav, I decided to play:

for i in `find -name "*.mp3"`; do mpg123 -w "${i/.mp3}.wav" $i; done

This is a good thread. We need to archive this one.
Title: Converting multiple files to mp3
Post by: Calum on 31 January 2003, 14:26
hey! that looks like a nice simple script! i found another script to do this but it didn't work  :(
i've been using xmms to do this job but will try that now you posted it. thanks!
Title: Converting multiple files to mp3
Post by: LorKorub on 31 January 2003, 15:59
OK..now I have I question.

I have a shitload of *.mp3 files that are saved on CDROMs that I want to relocate to a single directory on my Linux drive. The files are sorted in their respective directories by band-names, and most of them have spaces (They are from my windoid days on Kazaa and Napster.) I cannot use the tr command, as they are on a read only filesystem, and I can't copy them to a temp directory, as bash gives me a bunch of error messages pertaining to the spaces in the files.  

Is there a workaround to this?
Title: Converting multiple files to mp3
Post by: flap on 31 January 2003, 16:19
Just do

find /cdromdirectory -name '*.mp3' | while read item; do cp "$item" /destinationdirectory; done
Title: Converting multiple files to mp3
Post by: LorKorub on 31 January 2003, 17:17
Nice... that was quite easy. Like always, I am making things 100x more difficult than they should be. Here I am on the friggin' bash man page all night....

Thanks flap...
Title: Converting multiple files to mp3
Post by: Agent007 on 31 January 2003, 20:53
I just envy u guys who r expert with BASH...  (http://smile.gif)  How do I get started? Are the man pages enough?

some pointers to a newbie wud be a gr8 help!

thanks,
007
Title: Converting multiple files to mp3
Post by: flap on 31 January 2003, 22:24
http://www.tldp.org/LDP/abs/html/index.html (http://www.tldp.org/LDP/abs/html/index.html)

http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=bash+tutorial (http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=bash+tutorial)