Skip to main content

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:

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

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 closed
  • asyncio.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

livekit_simple_audio_source_streaming-0.1.2.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file livekit_simple_audio_source_streaming-0.1.2.tar.gz.

File metadata

File hashes

Hashes for livekit_simple_audio_source_streaming-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3bca7a01bf16c0eb03ecee2cf65fe5f4002ed74ac6dc7f9f713af8ffd9be7d69
MD5 845d325c9598b6063842039ac9818915
BLAKE2b-256 5e653c48786d596930278c3db60366a8e1f08e993224fc148841be523f206f3a

See more details on using hashes here.

File details

Details for the file livekit_simple_audio_source_streaming-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for livekit_simple_audio_source_streaming-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9a12113f841ede7e8e04b3c86b17d26c80904aa4079ff94272d6e700554d184
MD5 89cacf7a49fb65132571f91358fa9532
BLAKE2b-256 595e85343b15a3368e862f58155f9637928b9afc0275951c06bd7ea3ce052c96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page