how can I record and save the audio locally?
# ask-ai
a
how can I record and save the audio locally?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you with recording and saving audio locally in LiveKit! Depending on the SDK you're working with and your specific needs, here are some friendly options you can explore: • Using Client SDKs: You can directly record audio tracks to a local file. For example: ◦ Rust SDK: You might want to try using
WavWriter
to save audio as a WAV file. ◦ Swift SDK:
AudioManager.shared
is great for starting local recordings and saving audio buffers. ◦ Android SDK: Using
MediaCodec
and
MediaMuxer
can help you save audio and video right on your device. ◦ Node.js SDK: You can use
AudioStream
to write audio frames directly to a WAV file. • Using Egress: LiveKit’s Egress system offers server-side recording of room audio and video. While recordings are saved to cloud storage initially, you can easily download them to your local device once the process is done. If you let me know which SDK or method you're using, I’d be more than happy to walk you through the next steps with some tailored guidance! Sources: Processing raw media tracks | LiveKit Docs | examples/save_to_disk/src/main.rs | Tests/LiveKitTests/AudioEngineTests.swift | sample-app-record-local/README.md | examples/receive-audio/index.ts
a
give me a python example
t
In Python, you can use the
RecorderIO
class from the
livekit.agents.voice.recorder_io
module to record audio during an agent session and save it locally in OGG/Opus format. This way, you’ll capture both the input (user audio) and output (agent responses) streams within the session, making it easy to keep track of everything. If you’re interested in recording the entire room’s audio, LiveKit’s Egress feature is a great option. Just a heads-up, Egress recordings are meant to be saved to cloud storage like S3, GCP, or Azure. While it doesn’t support saving directly to your local device, you can always download the recordings from the cloud once the process is done. Feel free to reach out if you’d like more details or examples on how to use
RecorderIO
or set up Egress—I’m here to help! Sources: livekit-api/livekit/api/egress_service.py | Processing raw media tracks | LiveKit Docs | Session recording and transcripts | LiveKit Docs | docs/index.yaml | livekit-agents/livekit/agents/voice/recorder_io/recorder_io.py
a
I want to download the audio locally not to send it to any other cloud
t
I'd love to help you save audio locally in Python using LiveKit! You can do this by processing raw audio frames from a subscribed track and writing them directly to a file. Let me walk you through it step-by-step: 1. Subscribe to the audio track: When a participant's audio track is subscribed, make sure to check if it’s an audio track. 2. Create an AudioStream: Use
rtc.AudioStream(track)
to receive audio frames asynchronously, which is super handy. 3. Write to a WAV file: Convert the frame data to
int16
format and write it to a file with a properly formatted WAV header. Here’s a Python example to get you started:
Copy code
import asyncio
import wave
from livekit import rtc

async def save_audio_to_wav(audio_stream: rtc.AudioStream, file_path: str):
    with wave.open(file_path, 'wb') as wav_file:
        # Assume 48kHz, 1 channel, 16-bit samples
        sample_rate = 48000
        num_channels = 1
        sample_width = 2  # bytes per sample (16-bit)

        wav_file.setframerate(sample_rate)
        wav_file.setnchannels(num_channels)
        wav_file.setsampwidth(sample_width)

        async for frame in audio_stream:
            # Write raw PCM data to WAV
            wav_file.writeframes(frame.data)

# In your event handler:
@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, publication, participant):
    if track.kind == rtc.TrackKind.KIND_AUDIO:
        audio_stream = rtc.AudioStream(track)
        asyncio.ensure_future(save_audio_to_wav(audio_stream, "output.wav"))
This script sets up a WAV file and writes the raw PCM data from each audio frame it receives directly into it. If you have any questions or want me to help you with a complete working script, just let me know—I’m here to help! Sources: Processing raw media tracks | LiveKit Docs | examples/save_to_disk/src/main.rs | Session recording and transcripts | LiveKit Docs | Track egress | LiveKit Docs
a
Is there any other simpler method?
t
I'm happy to share a simple and effective way to save audio locally in Python using LiveKit! You can use
rtc.AudioStream
to grab raw audio frames and then write them straight to a WAV file with Python's handy built-in
wave
module. This method keeps things straightforward and is perfect if you're working with short audio clips. Here’s a clear example to get you started:
Copy code
import wave
from livekit import rtc

async def save_audio_simple(audio_stream: rtc.AudioStream, file_path: str):
    with wave.open(file_path, 'wb') as wav_file:
        wav_file.setframerate(48000)  # 48kHz sample rate
        wav_file.setnchannels(1)      # Mono audio
        wav_file.setsampwidth(2)      # 16-bit samples

        async for frame in audio_stream:
            wav_file.writeframes(frame.data)

@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, publication, participant):
    if track.kind == rtc.TrackKind.KIND_AUDIO:
        audio_stream = rtc.AudioStream(track)
        asyncio.ensure_future(save_audio_simple(audio_stream, "output.wav"))
This approach writes audio frames directly to a WAV file without any extra processing. If you have any questions or want help customizing this further, just let me know—I’m here to help! Sources: Capturing the First Few Seconds of a Call for Processing | Session recording and transcripts | LiveKit Docs | examples/save_to_disk/src/main.rs | examples/filesaver/main.go | list_devices.py