<https://docs.livekit.io/home/egress/api/> I want...
# ask-ai
p
https://docs.livekit.io/home/egress/api/ I want to record only the Agent's audio. Is that possible?
g
Yes, you can record only the Agent's audio using LiveKit's Participant Egress. This allows you to record a participant's audio and video tracks by providing the participant's identity. You can configure the Egress to focus on audio recording for the agent conversation. For more details, would you like a step-by-step guide on setting this up? Sources: - Egress API 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
p
Yes. I have 1 agent and 1 participant. I want to have 1 recording for the agent channel and 1 recording for the participant channel. Can you give me documentation + an implementation to do this in Python? @millions-hair-24956
g
To achieve separate recordings for the agent and participant channels, you can use LiveKit's Participant Egress to record each participant individually. Here's a step-by-step guide on how to implement this in Python: 1. Set Up LiveKit API: Initialize the LiveKit API client with your credentials. 2. Start Participant Egress: For each participant (agent and user), start a separate egress session to record their audio. 3. Monitor Egress Status: Continuously check the status of the egress to ensure it completes successfully. Here's a Python implementation:
Copy code
from livekit import api

# Initialize LiveKit API
lkapi = api.LiveKitAPI(LIVEKIT_API_URL, api_key=LIVEKIT_API_KEY, api_secret=LIVEKIT_API_SECRET)

async def start_audio_egress(room_name: str, participant_identity: str, filepath: str):
    req = api.ParticipantEgressRequest(
        room_name=room_name,
        identity=participant_identity,
        file_outputs=[
            api.EncodedFileOutput(
                filepath=filepath,
                s3=api.S3Upload(
                    bucket="your_bucket",
                    region="your_region",
                    access_key="your_access_key",
                    secret="your_secret",
                    force_path_style=True,
                ),
            )
        ]
    )

    try:
        egress_info = await lkapi.egress.start_participant_egress(req)
        print(f"Egress started: {egress_info.egress_id}")
        return egress_info
    except Exception as e:
        print(f"Failed to start egress: {e}")
        raise

async def monitor_egress(egress_id: str):
    try:
        while True:
            egress_list = await lkapi.egress.list_egress(api.ListEgressRequest(egress_id=egress_id))
            if egress_list.items:
                egress = egress_list.items[0]
                status_name = api.EgressStatus.Name(egress.status)
                print(f"Egress status: {status_name}")
                if egress.status in [api.EgressStatus.EGRESS_COMPLETE, api.EgressStatus.EGRESS_FAILED]:
                    if egress.status == api.EgressStatus.EGRESS_COMPLETE:
                        print("Egress completed successfully.")
                    else:
                        print("Egress failed.")
                    break
            else:
                print("Egress not found.")
                break
            await asyncio.sleep(5)  # Poll every 5 seconds
    except Exception as e:
        print(f"Error while monitoring egress: {e}")
        raise

# Example usage
async def main():
    agent_egress_info = await start_audio_egress("room_name", "agent_identity", "agent_audio.mp4")
    participant_egress_info = await start_audio_egress("room_name", "participant_identity", "participant_audio.mp4")

    await monitor_egress(agent_egress_info.egress_id)
    await monitor_egress(participant_egress_info.egress_id)

# Run the main function
import asyncio
asyncio.run(main())
This code sets up egress for both the agent and participant, recording their audio separately to specified file paths. Make sure to replace placeholders like
your_bucket
,
your_region
, etc., with your actual configuration details. Sources: - Recording Participants 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
p
Does the agent have an identity though? @millions-hair-24956
g
Yes, in LiveKit, an agent does have an identity. When an agent is instantiated and joins a room, it is assigned a unique identity, similar to any other participant. This identity can be used to manage and interact with the agent within the room. Would you like more details on how to set or use the agent's identity? Sources: - Participant Identity 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
p
Can you give me additional details on how to get the agent's identity? @millions-hair-24956
g
To get the agent's identity in LiveKit, you can access it through the
JobRequest
object when setting up the agent. The identity is typically set when the agent accepts a job request. Here's how you can dynamically set and retrieve the agent's identity: 1. Set the Agent's Identity: When you accept a job request, you can specify the agent's identity using the
accept
method of the
JobRequest
object. 2. Access the Identity: Once the agent is connected to a room, you can access its identity through the
participant
object. Here's an example in Python:
Copy code
from livekit import rtc
from livekit.agents import JobContext, JobRequest

async def request_fnc(req: JobRequest):
    # Set the agent's identity dynamically
    agent_identity = f"agent_{req.room.name}"
    await req.accept(
        name="agent_name",
        identity=agent_identity,
    )

async def entrypoint(ctx: JobContext):
    # Connect to the room
    await ctx.connect(auto_subscribe=rtc.AutoSubscribe.AUDIO_ONLY)
    # Wait for the first participant to connect
    participant = await ctx.wait_for_participant()
    print(f"Connected to room {ctx.room.name} with participant {participant.identity}")

# Initialize the worker with entrypoint and request_fnc
opts = rtc.WorkerOptions(
    entrypoint_fnc=entrypoint,
    request_fnc=request_fnc
)

# Start the worker
rtc.cli.run_app(opts)
In this example, the agent's identity is set based on the room name, and you can access it through the
participant.identity
attribute once the agent is connected to the room. Sources: - Setting Agent Identity 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.