How to use RecorderIO
# ask-ai
a
How to use RecorderIO
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.
The
RecorderIO
class in LiveKit is a wonderful tool designed to help you capture full-session audio recordings within LiveKit Agents (Python). It neatly records both the audio input (your user's voice) and the output (the agent's response) into a perfectly synchronized audio file. Let me walk you through how to use it: 1. Initialize RecorderIO: First, create an instance using your running `AgentSession`:
recorder_io = RecorderIO(agent_session=agent_session)
2. Wrap Audio Streams: Then, use
record_input()
and
record_output()
to wrap your input and output audio streams:
Copy code
self._input_audio = recorder_io.record_input(self._input_audio)
self._output_audio = recorder_io.record_output(self._output_audio)
3. Start Recording: When you're ready, start the recording by specifying an output file path:
await recorder_io.start(output_path="my_recording.ogg")
4. Stop Recording: Finally, wrap things up by stopping the recording with:
await recorder_io.aclose()
The resulting file will be in OGG format with the OPUS codec, beautifully mixing input (left channel) and output (right channel) as a stereo track. If you’d like any help implementing this or want to explore other options like Egress for room-level recording, please don’t hesitate to reach out—I’m here to help! Sources: livekit-agents/livekit/agents/voice/recorder_io/__init__.py | src/room/track/record.ts | Capturing the First Few Seconds of a Call for Processing | Session recording and transcripts | LiveKit Docs
a
Give me exam ple how to use it with my agent
t
Here’s a friendly guide on how to integrate
RecorderIO
with your agent: 1. Initialize RecorderIO: First, create an instance of
RecorderIO
and pass your active
AgentSession
to it:
Copy code
from livekit import RecorderIO

# Assuming you have an active AgentSession
recorder_io = RecorderIO(agent_session=agent_session)
2. Wrap Input and Output Streams: Next, wrap your agent's input and output audio streams so they can be recorded:
Copy code
# Wrap the input and output audio streams
self._input_audio = recorder_io.record_input(self._input_audio)
self._output_audio = recorder_io.record_output(self._output_audio)
3. Start Recording: To begin recording, specify where you'd like to save the file:
Copy code
import datetime

# Generate a timestamped filename
filename = f"recording_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')}.ogg"
await recorder_io.start(output_path=filename)
4. Stop Recording: When your session wraps up, stop the recording to finalize the file:
await recorder_io.aclose()
5. Link to Shutdown: If you'd like, you can connect the recorder's closure with your agent's shutdown process:
job_ctx.add_shutdown_callback(recorder_io.aclose)
This way, your entire session will be recorded and saved as an OGG audio file. If you have any questions or need a hand with any step, just let me know—I’m here to help!
a
how and where to warup the input and output stream Wrap Input and Output Streams
t
Let’s walk through how to wrap the input and output streams together, shall we? You’ll want to do this in the part of your agent’s code where the audio streams are being initialized or processed. Here’s a simple step-by-step guide to help you out: 1. Locate the Audio Streams: First, find where your agent’s input and output audio streams are set up. This is usually where you manage user audio input and create the agent’s responses. 2. Wrap the Streams: Next, use the
record_input()
and
record_output()
methods from
RecorderIO
to wrap these streams. For example:
Copy code
# Assuming self._input_audio and self._output_audio are your audio streams
self._input_audio = recorder_io.record_input(self._input_audio)
self._output_audio = recorder_io.record_output(self._output_audio)
3. Timing: Just make sure to do this wrapping before the streams are actively used for processing audio. Usually, this happens during the initialization phase of your agent session. If you’d like, feel free to share more details about your agent’s setup—I’d be happy to provide a more personalized example to guide you further!
a
give me example with an Agent. How to acess the audio stream
t
Here’s a friendly example to help you integrate
RecorderIO
with your agent and access the audio streams: 1. Set Up the Agent: Let's start by assuming you already have an agent session running, and the audio streams for input and output are ready to go. 2. Initialize RecorderIO: Next, create an instance of
RecorderIO
and pass your agent session to it:
Copy code
from livekit import RecorderIO

# Initialize RecorderIO with the agent session
recorder_io = RecorderIO(agent_session=agent_session)
3. Access and Wrap Audio Streams: Now, take your agent's audio streams (like
self._input_audio
and
self._output_audio
) and wrap them using
record_input()
and `record_output()`:
Copy code
# Wrap the input and output audio streams
self._input_audio = recorder_io.record_input(self._input_audio)
self._output_audio = recorder_io.record_output(self._output_audio)
4. Start Recording: You’re all set to start recording! Just specify where you'd like to save the file:
Copy code
import datetime

# Generate a timestamped filename
filename = f"recording_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')}.ogg"
await recorder_io.start(output_path=filename)
5. Stop Recording: When your session wraps up, simply stop the recording to finalize the file:
await recorder_io.aclose()
Just a quick note:
self._input_audio
and
self._output_audio
are placeholders for the audio streams your agent uses. If your setup involves different methods or libraries for handling audio, you might need to tweak this example a bit to fit your needs. Feel free to reach out if you’d like me to clarify anything or help further!
a
I am getting
# av.error.MemoryError: [Errno 12] Cannot allocate memory
when using RecorderIO
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!