Batch MP3 Encoding with Linux and LAME
published 2007-12-02 · refreshed 2026-10-20 · tested on Ubuntu 26.04 · LAME 3.101 · ffmpeg 8.0.1 · tags: batch, encoding, lame, linux, mp3, shell
The 2007 version of this page was a one-line for loop and a workaround for the double .wav.mp3 extension. The loop is still the right tool; the workaround is not needed, and LAME’s VBR presets make the output better than the 128 kbps CBR default of that era.
Install
sudo apt install lame
Encode one file
lame -V2 track.wav track.mp3
-V2 is VBR at roughly 190 kbps, transparent for almost everyone; -V0 is the highest VBR quality, -V4 is the default, -b 320 forces constant 320 kbps if a device insists on CBR. --preset standard is the same as -V2.
Encode a directory
for f in *.wav; do lame --quiet -V2 "$f" "${f%.wav}.mp3"; done
"${f%.wav}.mp3" strips the .wav and adds .mp3, so track.wav becomes track.mp3 and not track.wav.mp3; the quotes keep names with spaces (Artist - Album - 01 - Title.wav) intact. For a mix of extensions, "${f%.*}.mp3" strips whatever extension is there.
Use all the cores
LAME is single-threaded. Run one encoder per core with xargs -P, and -print0/-0 so odd file names survive:
find . -name '*.wav' -print0 | xargs -0 -P "$(nproc)" -I{} sh -c 'lame --quiet -V2 "$1" "${1%.wav}.mp3"' _ {}
This also recurses into subdirectories; add -maxdepth 1 to find if you do not want that.
Inputs LAME cannot read
LAME reads WAV, AIFF and raw PCM. For FLAC, Ogg, M4A or anything else, let ffmpeg decode and hand the audio to the same encoder (libmp3lame); -q:a 2 is the ffmpeg spelling of -V2, and tags are copied across:
ffmpeg -i track.flac -codec:a libmp3lame -q:a 2 track.mp3
In a loop:
for f in *.flac; do ffmpeg -loglevel error -i "$f" -codec:a libmp3lame -q:a 2 "${f%.flac}.mp3"; done
Clean up the originals
When every WAV has an MP3 next to it, the originals are the files whose .mp3 twin exists:
for f in *.wav; do [ -f "${f%.wav}.mp3" ] && rm -- "$f"; done
Run it without the rm first (replace it with echo) if the directory matters to you.
AS PUBLISHED, 2007
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!