A Python library for streaming audio to LiveKit voice channels using FFmpeg
Project description
LiveKit Audio Source Streaming
A Python library for streaming audio to LiveKit voice channels using FFmpeg.
Overview
This library provides a simple way to play audio files (MP3, WAV, OGG, streaming URLs, etc.) into LiveKit voice channels. It uses FFmpeg to decode audio and streams the raw PCM data to LiveKit's audio infrastructure.
Features
- Multiple audio format support - Play any audio format supported by FFmpeg (MP3, WAV, OGG, FLAC, AAC, streaming URLs, etc.)
- High-quality audio - Configurable sample rate (default 48kHz) and channel count (mono/stereo)
- Multiple simultaneous sources - Play multiple audio sources simultaneously with named identifiers
- Easy API - Simple async/await API for playing and stopping audio
- Proper cleanup - Graceful handling of FFmpeg processes and track publication
Requirements
- Python 3.10+
- FFmpeg installed on the system
- LiveKit SDK (
livekit)
Installation
From PyPI
pip install livekit-simple-audio-source-streaming
From source
git clone https://github.com/ANBAL534/livekit-simple-audio-source-streaming.git
cd livekit-simple-audio-source-streaming
pip install -e .
Quick Start
See: example.py
API Reference
VoiceClient
The main class for managing audio playback in a LiveKit room.
Constructor
VoiceClient(room: Room)
Parameters:
room(Room): A connected LiveKit Room instance. https://docs.livekit.io/python/livekit/rtc/room.html#livekit.rtc.room.Room
Methods
play(source: str, name: str = 'audio') -> FFmpegAudio
Play audio from a source and publish it to the room.
Parameters:
source(str): URL or file path to the audio source.name(str): Identifier for this audio source (default: 'audio'). Used to reference the source when stopping.
Returns:
- FFmpegAudio: The audio source instance.
Example:
# Play a simple audio file
audio = await voice_client.play("music.mp3")
# Play from a URL with custom name
background = await voice_client.play(
"https://example.com/background.mp3",
name="background"
)
stop(name: str = 'audio') -> None
Stop playing audio from a specific source.
Parameters:
name(str): Name of the audio source to stop (default: 'audio').
Example:
await voice_client.play("music.mp3", name="music")
await asyncio.sleep(10)
await voice_client.stop("music")
stop_all() -> None
Stop all currently playing audio sources.
Example:
await voice_client.play("music.mp3", name="music")
await voice_client.play("sfx.mp3", name="sfx")
await voice_client.stop_all()
disconnect() -> None
Stop all audio and disconnect from the room.
Example:
await voice_client.disconnect()
FFmpegAudio
Audio source class that streams audio using FFmpeg.
Constructor
FFmpegAudio(
source: str,
sample_rate: int = 48000,
num_channels: int = 1
)
Parameters:
source(str): URL or file path to the audio source.sample_rate(int): Output sample rate in Hz (default: 48000).num_channels(int): Number of audio channels (default: 1 for mono).
Example:
audio = FFmpegAudio(
source="music.mp3",
sample_rate=48000,
num_channels=1 # 1 = mono, 2 = stereo
)
Methods
create_track() -> LocalAudioTrack
Create a LiveKit audio track from this audio source.
Returns:
- LocalAudioTrack: A LiveKit local audio track.
start() -> None
Start streaming audio from FFmpeg to LiveKit.
Note: Must be called after the track is published to a room.
stop() -> None
Stop the FFmpeg process and audio streaming.
close() -> None
Close and clean up the FFmpeg audio source.
Using with Stoat Bot Libraries
This library is to be integrated with Stoat bot libraries that support LiveKit:
import stoat # or your preferred library (In the example: stoat.py lib)
# Get the LiveKit room from your bot
room = await channel.connect(node=node_name)
# Use VoiceClient to play audio
voice_client = VoiceClient(room)
await voice_client.play("notification.mp3")
Requirements
System Requirements
- FFmpeg: Must be installed on the system. Install via:
- Ubuntu/Debian:
sudo apt install ffmpeg - macOS:
brew install ffmpeg - Windows: Download from ffmpeg.org or use a package manager
- Ubuntu/Debian:
Python Dependencies
See: requirements.txt
Error Handling
The library raises exceptions for various error conditions:
try:
audio = await voice_client.play("invalid-file.mp3")
except Exception as e:
print(f"Failed to play audio: {e}")
# Handle the error appropriately
Common exceptions:
RuntimeError: If the audio source is already closedasyncio.TimeoutError: If track publication times out- Various FFmpeg errors if the audio file is invalid
Logging
The library uses Python's standard logging module. Enable debug logging for detailed information:
import logging
logging.basicConfig(level=logging.DEBUG)
# Now you'll see detailed logs about FFmpeg operations
voice_client = VoiceClient(room)
License
MIT License - See LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Keep in mind that this is a very simple library to enable the creation of quick voice bots such as Youtube Music bots, etc.
Other use cases as audio consumption, multi-channel, etc. are out-of-scope of this project.
Support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file livekit_simple_audio_source_streaming-0.1.2.tar.gz.
File metadata
- Download URL: livekit_simple_audio_source_streaming-0.1.2.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bca7a01bf16c0eb03ecee2cf65fe5f4002ed74ac6dc7f9f713af8ffd9be7d69
|
|
| MD5 |
845d325c9598b6063842039ac9818915
|
|
| BLAKE2b-256 |
5e653c48786d596930278c3db60366a8e1f08e993224fc148841be523f206f3a
|
File details
Details for the file livekit_simple_audio_source_streaming-0.1.2-py3-none-any.whl.
File metadata
- Download URL: livekit_simple_audio_source_streaming-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9a12113f841ede7e8e04b3c86b17d26c80904aa4079ff94272d6e700554d184
|
|
| MD5 |
89cacf7a49fb65132571f91358fa9532
|
|
| BLAKE2b-256 |
595e85343b15a3368e862f58155f9637928b9afc0275951c06bd7ea3ce052c96
|