Opuslib Python Bindings (Original)

3.0.1 · maintenance · verified Thu Apr 16

Opuslib provides Python bindings to the `libopus` IETF low-delay audio codec. It allows Python applications to encode and decode Opus audio streams. The current version, 3.0.1, was released in January 2018. Note that this specific `opuslib` package is no longer actively maintained; users are encouraged to consider the `opuslib-next` fork for ongoing support and compatibility with newer Python versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing an Opus encoder and decoder, then encoding a block of dummy PCM audio data and decoding it back. Remember to adjust `pcm_data_in` with actual audio samples for real-world use cases.

import opuslib

# Encoder parameters
SAMPLE_RATE = 48000  # Hz
CHANNELS = 1
APPLICATION = opuslib.APPLICATION_AUDIO
FRAME_SIZE_MS = 20 # milliseconds
FRAME_SIZE_SAMPLES = int(SAMPLE_RATE * FRAME_SIZE_MS / 1000)

# Create an encoder instance
encoder = opuslib.Encoder(SAMPLE_RATE, CHANNELS, APPLICATION)

# Example PCM audio data (replace with actual audio)
pcm_data_in = b'\x00' * FRAME_SIZE_SAMPLES * 2 * CHANNELS # 16-bit mono, silent

# Encode the audio
encoded_data = encoder.encode(pcm_data_in, FRAME_SIZE_SAMPLES)
print(f"Encoded {len(pcm_data_in)} bytes of PCM into {len(encoded_data)} bytes of Opus.")

# Create a decoder instance
decoder = opuslib.Decoder(SAMPLE_RATE, CHANNELS)

# Decode the audio
decoded_data = decoder.decode(encoded_data, FRAME_SIZE_SAMPLES)
print(f"Decoded {len(encoded_data)} bytes of Opus into {len(decoded_data)} bytes of PCM.")

# It's good practice to free resources if not using context managers (though opuslib handles it)
del encoder
del decoder

view raw JSON →