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!
on January 10th, 2008 at 10:39 am
Thanks! That was very helpful.
on February 20th, 2008 at 9:16 pm
Awesome… thanks for publishing this!
on February 21st, 2008 at 8:01 am
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
on February 21st, 2008 at 8:23 am
Hey there, Bill. Thanks for contributing! I’m also very happy that this post was useful for you.
on March 30th, 2008 at 5:49 am
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
on August 31st, 2008 at 9:22 am
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
on August 31st, 2008 at 9:24 am
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. :)