LAME MP3 Encoder Bindings
lameenc is a Python library providing bindings for the LAME MP3 encoding library. It simplifies the process of encoding PCM audio data into MP3 format without requiring users to compile the LAME binaries themselves, as it distributes pre-compiled wheels for various operating systems and Python versions. The library is actively maintained, with frequent updates to support new Python versions and add features.
Warnings
- breaking Python version support changes frequently. For example, v1.8.2 dropped support for Python 3.8 and 3.9, and v1.6.2 dropped support for Python 3.7. Always check the latest release notes for compatible Python versions.
- breaking The library's license switched from MIT to LGPLv3 in v1.5.0. This is a significant change with implications for how projects using `lameenc` can be licensed and distributed.
- gotcha The `Encoder` expects raw PCM data in 16-bit interleaved format. Incorrect input format (e.g., 8-bit, 32-bit float, non-interleaved, or different channel ordering) will lead to corrupted output or errors.
- gotcha Older versions of `lameenc` (prior to v1.6.0) had optimization issues on macOS and Linux, which meant significantly slower encoding performance compared to later versions or direct LAME command-line usage.
- gotcha Variable Bit Rate (VBR) encoding support was added in v1.8.0. If you require VBR, ensure you are using this version or newer.
- gotcha The ability to set the output sample rate was introduced in v1.7.0. Prior versions only allowed setting the input sample rate.
- gotcha While `lameenc` provides Python bindings, some external tools and libraries have observed it to be 'slow' due to factors like writing temporary files to disk or introducing encoder/decoder delays. This may affect real-time or performance-critical applications.
Install
-
pip install lameenc
Imports
- Encoder
from lameenc import Encoder
Quickstart
import lameenc
import array
# Example: Generate some dummy 16-bit PCM stereo audio (e.g., a sine wave)
# For a real application, this would come from an audio input/file
sample_rate = 44100 # Hz
channels = 2 # Stereo
duration = 3 # seconds
num_samples = sample_rate * duration
# Create a bytearray for 16-bit interleaved PCM data
# Each sample is 2 bytes (16-bit), stereo means 2 samples per frame
pcm_data_raw = array.array('h', [0] * (num_samples * channels))
# Fill with a simple sine wave (for demonstration purposes)
import math
for i in range(num_samples):
amplitude = int(32767 * math.sin(2 * math.pi * 440 * i / sample_rate))
pcm_data_raw[i * channels] = amplitude # Left channel
pcm_data_raw[i * channels + 1] = amplitude # Right channel
pcm_bytes = pcm_data_raw.tobytes()
# Initialize the encoder
encoder = lameenc.Encoder()
encoder.set_bit_rate(128) # 128 kbps
encoder.set_in_sample_rate(sample_rate)
encoder.set_channels(channels)
encoder.set_quality(2) # 2 = highest, 7 = fastest
# Encode the audio
mp3_data = encoder.encode(pcm_bytes)
mp3_data += encoder.flush() # Flush any remaining buffered data
# Save to a file (optional)
with open('output.mp3', 'wb') as f:
f.write(mp3_data)
print(f"MP3 data generated and saved to output.mp3 (size: {len(mp3_data)} bytes)")