Help with 'batch conversion using lame' shell script

Hi.

I am trying to write an sh script that will:

  1. take each wav file in ~/Documents
  2. convert each into mp3 format using "lame" encoder
  3. save the new mp3 in ~/Documents/newmp3s.

It has to follow the 3 steps in this order for each wav file before taking the next file.

I tried a for loop taken from another forum, but not only does the script still saves the new mp3 files in ~/Documents but the loop also doesn't end.

Any help would be much appreciated. Right now I'm using Audacity for manual conversion :frowning:

Personally, I'm thinking that the use of lame, Audacity and whether they're mp3 files is irrelevant to your problem. How about posting the stricken script so that someone can review/correct the loop that's misbehaving...?

Here is the code:

#!/bin/bash

cd ~/Documents

for file in *.wav; 
do lame "$file"; 
done

for i in *.mp3; do
mv "$i" ~/Documents/newmp3s
done

okay, so the sequence you'd said was required earlier is not what the script you've posted is actually doing. You have them separated into two loops, which is fine, but might be off in terms of timing...maybe you're checking in on the results before the first loop is completed...

Assuming that lame takes your hello.WAV file and outputs it as hello.MP3, try:

#!/bin/bash

cd ~/Documents

for file in *.wav 
do 
   lame "$file" 
   mv ${file%.wave}.mp3 ~/Documents/newmp3s/. 
done

cd - 

I tried your solution and it only saved my 2 test files (Hello1.wav and Hello2.wav) in ~/Documents as *.wav.mp3 files, still in ~/Documents.

I tried putting the idea from your code into my previous code and it worked.

Here is the new code:

#!/bin/bash

cd ~/Documents

for file in *.wav; 
do lame "$file"; 

for i in *.wav.mp3 ; do
mv "$i" ~/Documents/'newmp3s'
cd ~/Documents/'newmp3s'
rename 's/.wav//' *.mp3
cd ..
done
done 

Thanks for that.

But now if i want to convert mp3s into mp3s (i do that to decrease file size), using exactly the same code, the 'rename' command will remove the 'mp3' extension in all but the last new converted file in ~/Documents/newmp3s. Any workaround for that?

#!/bin/bash

cd ~/Documents

for file in *.mp3; 
do lame "$file"; 

for i in *.mp3.mp3 ; do
mv "$i" ~/Documents/newmp3s
cd ~/Documents/newmp3s
rename 's/.mp3//' *.mp3
cd ..
done
done

Does lame care about the input file's extension; ie, takes your input file as is and bases processing on file structure instead of extensions? If so, why not strip it off (or do a temporary cp) prior to passing it to lame, and then do the rename to .MP3 as per usual?

Yes it cares about the extension. I tried this:

lame hello hello.mp3

and i got 'Warning: Unsupported audio format' even though 'hello' is of mp3 format.

I would just like to replicate the 'one-click conversion' from the audio converters in Windows, without installing software.

I can still add the 'mp3' extension in all files after conversion, that's no biggie.

---------- Post updated at 04:02 PM ---------- Previous update was at 11:58 AM ----------

I also noticed that all the songs in my Music Library does not include the '.mp3' extension in their filename :S

I copied 2 songs to ~/Documents and my script bypassed those 2 files because lame does not recognize them as mp3s. Audacity does though and it still uses lame for encoding.

I think it's getting too complicated now. I'll just look for a program that does batch encoding in *nix.

--mp3input
        Assume the input file is a MP3 file. Useful for downsampling from one mp3 to
        another. As an example, it can be useful for streaming through an IceCast server.
        If the filename ends in ".mp3" LAME will assume it is an MP3 file. For stdin or
        MP3 files which do not end in .mp3 you need to use this switch.

That option may help you out. If you want further help with your script, add "set -x" as the first command and then post the trace output that's generated.

Regards,
Alister

So i entered this in the command line:

lame --mp3input hello hello1

and it worked. The output file was fine when i put back the mp3 extension. Thanks a lot.

But, in my script, i already specified in the for loop to only consider '.mp3' files. It will still ignore 'non-mp3' files. How do i make it detect any audio file of mp3 format in ~/Documents?

As for adding 'set -x', my script closes itself when all conversion is done. How do i stop that?

#!/bin/bash

set -x

cd ~/Documents

for file in *.mp3; 
do lame --mp3input "$file"; 

for i in *.mp3.mp3 ; do
mv "$i" ~/Documents/newmp3s
cd ~/Documents/newmp3s
rename 's/.mp3//' *.mp3
cd ..
done
done