Converting .wav to .mp3

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
gary101
Posts: 555
Joined: Sun 08 Oct 2006, 09:51
Location: Boston, Lincs. UK

Converting .wav to .mp3

#1 Post by gary101 »

Hi

I have had a whole bunch of wav files on the computer that I use Beep media player with and this has been fine.

However, recently I needed to play them on my palm TX which doesn't like .wav files - - So off to google!

I found this little script which converts wav to mp3 and does the job just fine. All you have to do is create the script and run it from the folder where your .wavs are and it will convert all wavs in the folder. You can run it from the command line if you want to see what is going on or just click on the script and let it do the job (code below).

Hope someone finds this as useful as I do.
Gary

Create a new script with the contents:

#!/bin/bash
#
# wav2mp3
#
for i in *.wav; do
#out=$(ls $i | sed -e 's/.wav//g')
#out=$(echo $i | sed -e 's/.wav$//')
#lame -h -b 192 "$i" "$out.mp3"
lame -h -b 192 "$i" "${i%.wav}.mp3"
done

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#2 Post by Flash »

Why go to all that work when you could just use Puppy's Pawdioconverter. :lol: (In Menu > Multimedia)

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#3 Post by Pizzasgood »

Because it's faster in the end. Open the directory in Rox. Press the backquote/tilde key to open a terminal there. Type wav2mp3 and hit enter.


My process is a little different. I like to normalize things first, so I installed a package called "normalize" that I found somewhere. And I use Ogg, not Mp3, and also make a copy with flac for archival. So I usually type something like this:

for i in *.wav; do normalize "$i"; oggenc "$i"; flac "$i"; done;

Sometimes I write an actual script out of that, so that I can just run that rather than re-typing everything. But I'm usually too lazy, or else I do make the script and then forget about it the next time I need it. :roll:

Also, you could modify that script a little so that you could call it like this:
wav2mp3 *.wav
That would allow you to specify which files to convert. If you only wanted to convert certain files, you could type wav2mp3 followed by a space, then select the files in Rox (left-click drag lasso, and remember that middle-click drag lasso works the same as holding [ctrl] with the left-click version). Now middle-click in the terminal to paste the absolute paths of each file (WARNING: you'll have to manually \escape any spaces, or put any path with spaces in "quotes"). Then hit enter.

I don't remember the exact process you'd need to use. It involves using shift to change which parameter is referenced by $1. The Rute Guide explains it, or just google or look through some scripts that take multiple parameters in that way. I've uploaded some things that do it too (including an audio converter similar to yours, but that depends on mplayer to convert just about anything into a wav).
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

Bruce B

#4 Post by Bruce B »

Great little script, does just what its supposed to do. I have had a chance to use it, but have done a little testing by echoing the output. It seems to do three things that are important:

* get rid of the .wav portion of the output file

* handle spaces in input and output files

* allow for easy user defined modification of lame parameters.

------------

Not to mention easy to run.

------------

Here is the echoed output:

lame -h -b 192 test1.wav test1.mp3
lame -h -b 192 test2.wav test2.mp3
lame -h -b 192 test3.wav test3.mp3
lame -h -b 192 test4.wav test4.mp3
lame -h -b 192 test 5.wav test 5.mp3
lame -h -b 192 test 6.wav test 6.mp3
lame -h -b 192 test 7.wav test 7.mp3

Thanks a lot.

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#5 Post by MU »

for i in *.wav;do
this will not read files with spaces in the filename, like collections you created in Windows.

For this reason it is recommended, to do this instead:

Code: Select all

ls *.wav | while read i;do
Then it works.
Mark

Bruce B

#6 Post by Bruce B »

Mark,

I didn't have any .wav files to work with at present.

I was curious about the handling of spaces, especially because we are likely to encounter them in .wav files.

My test procedure for the spaces was to 'touch' files 5 thru 7 and view the output, by placing echo at the beginning of the lame command line.

In this case the spaces showed in the input files and the output files.

Probably, what I'll do next is find a cd to rip.

Adjust as necessary.

Thanks,


Bruce

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#7 Post by MU »

hm, maybe it was fixed in newer versions of bash.
Until maybe 1 year ago that was an often reported request in my german Linuxboard.
I did not try the "for i" yet in Puppy, as I always used " | while" since then.

Mark

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#8 Post by Pizzasgood »

So long as you remember to use "$i" rather than $i, all will be well.
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

Bruce B

#9 Post by Bruce B »

Just to make sure the record is straight.

#1 I don't purport or pretend to have bash scripting skills equal to Mark's.

# 2 My preliminary 'echo' tests worked, I decided to try some actual tests. Still not having .wav files to work with I wrote a minimally modified version of wav2mp3 called mp32wav. The purpose being to actually test for real the handling of spaces in the filenames. This is the actual script:

Code: Select all

#!/bin/sh
# for batch decoding mp3 files to wav format.
for i in *.mp3
do lame --decode "$i" "${i%.mp3}.wav"
done
The in-files are mp3 with multiple spaces, the out-files are wav with the same multiple spaces. At the end of the decoding, the only difference in the filenames are the extensions.

============

I guess the net result at this point is we have two simple and useful scripts:

1) for .wav to .mp3 batch conversion using lame
2) for .mp3 to .wav batch conversion using lame

User avatar
gary101
Posts: 555
Joined: Sun 08 Oct 2006, 09:51
Location: Boston, Lincs. UK

#10 Post by gary101 »

This seems to have generated more interest than I thought.

Here is the link to the page I found on goole for the script above (I just changed the order of wav and mp3 to convert the other way) which includes stuff like removing spaces, converting to lower case and normalising.
Not that I need to do any of that stuff, but it is handy to know.
http://www.faqs.org/docs/Linux-mini/MP3-CD-Burning.html

Enjoy!

Gary

Bruce B

#11 Post by Bruce B »

Gary,

Puppy contains spacereplace for purpose of replacing spaces with underscores. Works in current directory and / or in subdirectories.

To initiate help say spacereplace -h or say spacereplace

Caution: the program actually works! Meaning on the whole directory. There is no undo, but one could be written easy enough to replace underscores with spaces.

Here is a script called upperfn, it converts filenames to uppercase in the whole directory

Code: Select all

#!/bin/sh
for f in *; do
g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[a-z]' '[A-Z]'`
mv "$f" "$g"
done
The opposite script I call lowerfn

Code: Select all

#!/bin/sh
for f in *; do
g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
mv "$f" "$g"
done
I didn't write these scripts and don't remember where I picked them up.

-----------------
I think both programs need a little work because they act too effectively, changing all file names in the current directory without a prewarning or confirmation.

The operational difference between these and the ones at the link you posted is they change filenames with spaces in them.

What I want is a program that converts to Capital case.

Thanks again,

Bruce

I had a similar mp32wav program of my own, but it didn't do it in a single command, not even close.

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#12 Post by Flash »

What would the script be for convering .wav to .mp3 at 32kbps mono?

User avatar
gary101
Posts: 555
Joined: Sun 08 Oct 2006, 09:51
Location: Boston, Lincs. UK

#13 Post by gary101 »

Hi Flash

After looking at the lame man page: http://linux.die.net/man/1/lame

I reckon it would be something like this:
lame -h -b 32 -m m "$i" "${i%.wav}.mp3"

within the loop as specified in previous posting

But I haven't tried it out yet.

Gary

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#14 Post by Flash »

Thanks, I hope I can find the time to try it out soon. :)

User avatar
gary101
Posts: 555
Joined: Sun 08 Oct 2006, 09:51
Location: Boston, Lincs. UK

#15 Post by gary101 »

tested it out and it seems to produce the desired mp3 type, 32kbs mono

#!/bin/bash
#
# wav2mp3
#
for i in *.wav; do
#out=$(ls $i | sed -e 's/.wav//g')
#out=$(echo $i | sed -e 's/.wav$//')
#lame -h -b 192 "$i" "$out.mp3"
lame -h -b 32 -m m "$i" "${i%.wav}.mp3"
done

Gary

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#16 Post by zigbert »

pizzagood wrote:My process is a little different. I like to normalize things first, so I installed a package called "normalize" that I found somewhere. And I use Ogg, not Mp3, and also make a copy with flac for archival. So I usually type something like this:

for i in *.wav; do normalize "$i"; oggenc "$i"; flac "$i"; done;
Do anyone know where to find a normalize pet-package. This could be great in Pburn when encoding a audio-CD.

Sigmund

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#17 Post by Pizzasgood »

Okay, here's a package of the one I've been using (taken from an rpm):
normalize-0.7.6-2-i386.pet
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#18 Post by zigbert »

Thank you, Pizzagood.

Post Reply