{"id":3531,"library":"lameenc","title":"LAME MP3 Encoder Bindings","description":"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.","status":"active","version":"1.8.2","language":"en","source_language":"en","source_url":"https://github.com/chrisstaite/lameenc","tags":["audio","mp3","encoding","lame","bindings"],"install":[{"cmd":"pip install lameenc","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary class for MP3 encoding is `Encoder`.","symbol":"Encoder","correct":"from lameenc import Encoder"}],"quickstart":{"code":"import lameenc\nimport array\n\n# Example: Generate some dummy 16-bit PCM stereo audio (e.g., a sine wave)\n# For a real application, this would come from an audio input/file\nsample_rate = 44100  # Hz\nchannels = 2         # Stereo\nduration = 3         # seconds\nnum_samples = sample_rate * duration\n\n# Create a bytearray for 16-bit interleaved PCM data\n# Each sample is 2 bytes (16-bit), stereo means 2 samples per frame\npcm_data_raw = array.array('h', [0] * (num_samples * channels))\n\n# Fill with a simple sine wave (for demonstration purposes)\nimport math\nfor i in range(num_samples):\n    amplitude = int(32767 * math.sin(2 * math.pi * 440 * i / sample_rate))\n    pcm_data_raw[i * channels] = amplitude  # Left channel\n    pcm_data_raw[i * channels + 1] = amplitude # Right channel\n\npcm_bytes = pcm_data_raw.tobytes()\n\n# Initialize the encoder\nencoder = lameenc.Encoder()\nencoder.set_bit_rate(128)  # 128 kbps\nencoder.set_in_sample_rate(sample_rate)\nencoder.set_channels(channels)\nencoder.set_quality(2)     # 2 = highest, 7 = fastest\n\n# Encode the audio\nmp3_data = encoder.encode(pcm_bytes)\nmp3_data += encoder.flush() # Flush any remaining buffered data\n\n# Save to a file (optional)\nwith open('output.mp3', 'wb') as f:\n    f.write(mp3_data)\n\nprint(f\"MP3 data generated and saved to output.mp3 (size: {len(mp3_data)} bytes)\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the `lameenc.Encoder`, configure its parameters like bit rate, sample rate, channels, and quality, and then encode raw 16-bit interleaved PCM audio data into an MP3 byte stream. The example includes generating dummy stereo sine wave PCM data for a runnable demonstration."},"warnings":[{"fix":"Ensure your project's Python environment aligns with the `lameenc` version's supported Python range. Upgrade Python or pin `lameenc` to an older compatible version.","message":"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.","severity":"breaking","affected_versions":">=1.6.2"},{"fix":"Review the LGPLv3 license requirements and ensure your project's licensing and distribution comply with its terms if using versions 1.5.0 or newer.","message":"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.","severity":"breaking","affected_versions":">=1.5.0"},{"fix":"Pre-process your audio data to ensure it's in the required 16-bit signed integer, interleaved PCM format. Libraries like `soundfile` or `scipy.io.wavfile` can help read/convert audio files to this format.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade to `lameenc` version 1.6.0 or newer to benefit from improved encoding speeds on macOS and Linux platforms.","message":"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.","severity":"gotcha","affected_versions":"<1.6.0"},{"fix":"To use VBR encoding, upgrade to `lameenc` version 1.8.0 or later. Consult the `Encoder` API for the new methods to configure VBR.","message":"Variable Bit Rate (VBR) encoding support was added in v1.8.0. If you require VBR, ensure you are using this version or newer.","severity":"gotcha","affected_versions":"<1.8.0"},{"fix":"If you need to explicitly control the output sample rate, upgrade to `lameenc` version 1.7.0 or later and use the `set_out_sample_rate()` method.","message":"The ability to set the output sample rate was introduced in v1.7.0. Prior versions only allowed setting the input sample rate.","severity":"gotcha","affected_versions":"<1.7.0"},{"fix":"For highly performance-sensitive applications, benchmark `lameenc` against other available audio processing libraries or consider directly invoking the LAME command-line tool via `subprocess` if intermediate file I/O is acceptable.","message":"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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}