FFmpeg MP3 Encoding Guide with libmp3lame

This article provides the standard FFmpeg command-line syntax required to encode audio files into the MP3 format using the libmp3lame library. It covers the basic command structure, demonstrates how to configure Constant Bitrate (CBR) and Variable Bitrate (VBR) encoding, and explains the essential parameters needed to control audio quality.

The Basic Syntax

To encode any audio file to MP3 using the libmp3lame encoder without any additional quality configurations, use the following basic command:

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

Constant Bitrate (CBR) Encoding

Constant Bitrate keeps the data rate consistent throughout the entire audio file. This is useful for streaming compatibility. To set a specific bitrate, use the -b:a option followed by the desired bitrate (e.g., 192k, 256k, or 320k for maximum quality).

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

Variable Bitrate (VBR) Encoding

Variable Bitrate allows the encoder to dynamically adjust the bitrate based on the complexity of the audio, resulting in better quality-to-file-size efficiency. In FFmpeg, VBR is enabled using the -q:a option.

The value for -q:a ranges from 0 (highest quality, highest bitrate) to 9 (lowest quality, lowest bitrate). A value of 2 or 4 is standard for high-quality audio.

ffmpeg -i input.wav -c:a libmp3lame -q:a 2 output.mp3
-q:a Value Target Bitrate Range Quality Level
0 220–260 kbps Extremely High
2 170–210 kbps Very High (Recommended)
4 140–185 kbps Good (Default)
6 100–130 kbps Acceptable

Adjusting Channels and Sampling Rate

You can further control the output by specifying the audio sampling rate and the number of audio channels.

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