FFmpeg - Encode MP3

From PiRho Knowledgebase
Revision as of 14:36, 14 March 2026 by Dex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This article explains how to encode MP3 audio using FFmpeg with the libmp3lame encoder. It includes recommended variable‑bitrate (VBR) settings, transparency considerations, and references for deeper reading.

FFmpeg does not include a native MP3 encoder. Instead, it relies on the high‑quality and widely respected LAME encoder.

Basic Command (VBR – Recommended)

Use the following command to encode a high‑quality VBR MP3 file:

ffmpeg -i <input.file> -codec:a libmp3lame -qscale:a 2 <output.file>

The option -qscale:a (alias: -q:a) controls the quality level instead of specifying a fixed bitrate.

  • Lower value = higher quality
  • Range for LAME: 0–9
  • 0–3 → usually transparent
  • 4 → close to transparency
  • 6 → acceptable

Using -qscale:a 2 corresponds to LAME’s -V 2 setting, producing a typical average bitrate of ~170–210 kbit/s at 44.1 kHz.

Transparency and Recommended Bitrates

The transparency threshold for MP3 is widely reported as being in the region of 175–245 kbit/s (VBR), 44.1 kHz.

Constant Bitrate (CBR) Encoding

If you need a fixed bitrate (e.g., for hardware compatibility), use:

-b:a 128k

Example:

ffmpeg -i <input.file> -codec:a libmp3lame -b:a 256k <output.file>

Available MP3 CBR bitrates (kbit/s): 8, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320.

Troubleshooting

Sometimes output bitrate is lower than expected due to bitrate boundaries, low‑bitrate input sources, or VBR optimisation.

Practical Examples

Convert 320k MP3 → Transparent VBR

ffmpeg -i in.mp3 -codec:a libmp3lame -qscale:a 2 out.mp3

Encode to low‑bitrate CBR

ffmpeg -i input.file -map 0:a:0 -b:a 96k output.mp3

When to Use VBR vs CBR

  • Use VBR when quality matters or transparency is desired.
  • Use CBR when device compatibility or predictable file size is required.

References

  • FFmpeg MP3 Encoding Guide
  • FFmpeg High‑Quality Audio Encoding Guidelines