Batch MP3 Encoding with Linux and LAME
If you have a number of audio files that you would like to convert to mp3, but don’t want to hassle with graphical applications, there is a simple way to accomplish the task using Linux, LAME and a little shell scripting. By performing a basic “for loop” to invoke LAME, you can easily convert any group of audio files using one line of shell code.
Here is an example of a “for loop” that runs the command “lame” against a set of files in your current working directory with the .wav file extension.
$ for f in *.wav ; do lame $f ; done
If your audio file names have spaces in them, then you will need to use quotation marks around “$f” variable.
$ for f in *.wav ; do lame "$f" ; done
I typically create my original audio files without the file name extension of .wav or .au. This is because when you run lame against a file name, and do not omit the extension in the output option, the resulting file will have two extensions in the file name. e.g. filename.wav.mp3. Yes, I can use sed or basename in the for loop to prevent this, but to keep it simple, I just choose to not use the file extension to begin with.
If you are working with a group of files that have all been named using the convention of “Artist – Album – ## – Track Title”, (notice the spaces in the name), the following will work.
$ for f in Artist\ -\ Album* ; do lame "$f" ; done
Once the job is finished, you will be left with a directory full of your original audio files, and your newly created mp3′s.
Extra Credit
Okay, since we’re on the topic of shell scripting. If you want to delete all the original audio files (the ones without any file name extensions), and without first moving the new mp3′s to a different directory, one overly complicated example would be the following.
for f in *.mp3 ; do AUFILE=`basename "$f" .mp3` ; rm "$AUFILE"; done
This would have been easier if the original files could have been identified with .wav extensions, (rm *.wav), but since they had no file extensions to begin with, a wild card alone would not work. Now folks, this is just an example, and there’s a million other ways you can go about this. But in any case, I hope it helps you start exploring on your own!
Comments
Daniel said,
Thanks! That was very helpful.
Bill said,
Awesome… thanks for publishing this!
Bill said,
Using your technique to mv the .wav.mp3 named files to *.mp3:
for f in *.mp3 ; do AUFILE=`echo "$f" | sed 's/.wav//g'` ; mv "$f" "$AUFILE"; done
gmendoza said,
Hey there, Bill. Thanks for contributing! I’m also very happy that this post was useful for you.
midlife_crisis said,
this has inspired me to write this script, it the first time I’ve written a bash script, so I’m sure it could be improved:
#!/bin/bash
TARGET_DIR=”ipod/”
mkdir -p $TARGET_DIR
for f in *.mp3 ; do
fn=${f%*.mp3}”_pod.mp3″
lame -b 96 “$f” “$fn”
mv “$fn” $TARGET_DIR/
done
this resamples mp3′s to 96kbps, this is for my ipod shuffle. It renames as _ipod.mp3, creates an ipod folder and moves the resampled file to it.
Cheers
Vyvyan said,
So far, I have been following to automatically remove ‘.wav’ from mp3 files.
for file in *.wav; do echo “${file%.wav}.mp3″; done
To remove wav’s later after the conversion is completed without any errors and with satisfaction is a good idea rather than deleting in same long one line command.
rm -rf *.wav
It isn’t much of an effort. :P
Vyvyan said,
for file in *.flac; do lame -q 0 -v –vbr-new –noreplaygain –id3v2-only “$file” “${file%.wav}.mp3″; done
In the last command I told I just echoed the filenames. Here is the complete command. :)
kash said,
can any one tell how to convert mp3 to rm?
skizm said,
Heh, maybe my script was a bit overkill then. I made one for running after a whole cd has been ripped with cdparanoia, and assuming cdparanoia’s default output file names ‘trackXX.cdda.wav”, ‘XX’ being the tracknumbers.
gmendoza said,
@skism
Hey, very cool! Thanks for contributing! Can’t wait to try it out.
skizm said,
@gmendoza: Any time mate. Hope you like it, it’s my first bash script ever. (=
gmendoza said,
@skism
By the way, your syntax was messed up by my websites auto-formatting. So I edited your post so that it shows proper syntax highlighting and fixed the text where my site messed it up. Thanks again.
Vyvyan said,
Well people are still commenting on this page :)
I was looking for something and stumbled across this page again so I find it worth mentioning that a couple of days after I last commented here I found a WAY BETTER solution for this problem.
1. Install shntool. It’s a wonderful tool for a lot of things. http://etree.org/shnutils/shntool/
2. export the following:
export ST_CUST_ENC=’ext=mp3 lame -V 0 -q 0 –vbr-new –noreplaygain – %f’
You can put the above line in your .bashrc (remember to source .bashrc to continue using same terminal for current session). Modify lame options as per your needs, look man for more info.
3. run the following command to convert all your wav to mp3
shnconv -o cust *.wav
If you have single wav file an album and a cue file, use:
cuebreakpoints album.cue | shnsplit -o cust album.wav
cuetools is another utility.
You can do all sort of conversion with shntool. I prefer using picard (search musicbrainz picard) for tagging because I am paranoid about keeping my music arranged and well tagged.
gmendoza said,
Nice tool suggestions. Thanks!
Liam said,
Thanks very much! I was looking for a solution to batch converting an album to a new folder.
In the end I used the following two lines:
$ mkdir new
$ for f in *.mp3 ; do lame –preset standard “$f” “new/$f” ; done
Hennie said,
Hello,
I think this little piece of script solve’s *.cdda.wav to *.mp3 problem at one’s;
for f in *.wav ; do AUFILE=`echo “$f” | sed ‘s/.cdda.wav/.mp3/g’` ; lame $f $AUFILE ; done
mlok said,
Hello, this command that will convert all .wav files in a folder and put the right .mp3 extension instead of .wav or .wav.mp3 :
$ for f in *.wav ; do NEWNAME=`echo “$f” | sed ‘s/.wav/.mp3/g’` ; lame “$f” “$NEWNAME”; done
mdwstmusik said,
My solution for the .wav.mp3 problem…
for f in *.wav ; do lame -h $f `echo $f | cut -d. -f1`.mp3 ; done
Add A Comment