How to Use FFmpeg libmp3lame for MP3 Encoding

This article provides a concise guide on how to properly invoke the libmp3lame encoder using the FFmpeg command line. It covers the basic command structure, setting constant bitrate (CBR), utilizing variable bitrate (VBR) presets, adjusting audio quality, and configuring sample rates to achieve high-quality MP3 compression.

Basic Command Structure

To encode an audio file to MP3 using the LAME encoder, you must specify libmp3lame as the audio codec using the -c:a (or -codec:a) flag. The simplest command looks like this:

ffmpeg -i input.wav -c:a libmp3lame output.mp3

Encoding with Variable Bitrate (VBR)

Variable Bitrate is the recommended method for MP3 encoding as it produces the best quality-to-file-size ratio. In FFmpeg, VBR is controlled using the -q:a option. The scale ranges from 0 (highest quality, largest file) to 9 (lowest quality, smallest file), corresponding to LAME’s native V-scale:

An example command for high-quality VBR encoding:

ffmpeg -i input.wav -c:a libmp3lame -q:a 2 output.mp3

Encoding with Constant Bitrate (CBR)

If you require a specific, unchanging bitrate—often used for streaming compatibility—you can define it using the -b:a option. Standard target bitrates include 128k, 192k, 256k, and 320k.

An example command for maximum quality CBR encoding at 320 kbps:

ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3

Adjusting Channels and Sample Rate

You can further customize your output by forcing a specific sample rate using the -ar flag or defining the number of audio channels with the -ac flag:

ffmpeg -i input.wav -c:a libmp3lame -q:a 2 -ar 44100 -ac 2 output.mp3

In this command, -ar 44100 forces the output sample rate to 44.1 kHz, and -ac 2 ensures the output is stereo.