straight-jelly-92105
11/01/2025, 5:18 PMtall-airline-16756
11/01/2025, 5:18 PMtall-airline-16756
11/01/2025, 5:18 PMroom_composite_egress isn’t directly available on the LiveKitAPI object. No worries though! To fix this, you’ll want to access the egress service and use the method start_room_composite_egress instead.
Here’s a friendly example to help you get started with room composite egress:
from livekit import api
# Create the egress request
req = api.RoomCompositeEgressRequest(
room_name="my-room",
layout="speaker",
preset=api.EncodingOptionsPreset.H264_720P_30,
audio_only=False,
segment_outputs=[api.SegmentedFileOutput(
filename_prefix="my-output",
playlist_name="my-playlist.m3u8",
live_playlist_name="my-live-playlist.m3u8",
segment_duration=5,
gcp=api.GCPUpload(
credentials=file_contents,
bucket="<my-bucket>",
),
)],
)
# Create LiveKitAPI instance and start egress
lkapi = api.LiveKitAPI()
res = await lkapi.egress.start_room_composite_egress(req)
# Don’t forget to close the API client
await lkapi.aclose()
Just make sure you’re calling the egress service (lkapi.egress) and the right method name (start_room_composite_egress). If you run into any more questions or need a hand, I’m here to help!
Sources: livekit-api/livekit/api/egress_service.py | egress/recording_agent.pystraight-jelly-92105
11/01/2025, 5:20 PMtall-airline-16756
11/01/2025, 5:20 PMentrypoint function! To make sure everything runs smoothly, it's important to handle egress properly and manage any potential errors. Here's an improved version of your function that uses the egress service correctly and includes solid error handling:
async def entrypoint(ctx: JobContext):
lkapi = api.LiveKitAPI(
url=os.getenv("LIVEKIT_URL"),
api_key=os.getenv("LIVEKIT_API_KEY"),
api_secret=os.getenv("LIVEKIT_API_SECRET")
)
egress_info = None
try:
# Define the recording request
recording_request = api.RoomCompositeEgressRequest(
room_name=ctx.room.name,
file=api.EncodedFileOutput(
file_type=api.EncodedFileType.MP4,
filepath=f"recordings/{ctx.job.id}_{ctx.room.name}.mp4"
)
)
# Start the recording
egress_info = await lkapi.egress.start_room_composite_egress(recording_request)
print(f"Started recording with ID: {egress_info.egress_id}")
# Connect agent to room
await ctx.connect()
# Agent session continues...
except Exception as e:
print(f"Failed to start recording: {e}")
if egress_info:
print(f"Egress ID: {egress_info.egress_id}")
raise
finally:
# Ensure the API client is closed
await lkapi.aclose()
async def finalize_session():
# Stop the recording
try:
if egress_info:
await lkapi.egress.stop_egress(egress_info.egress_id)
print(f"Stopped recording with ID: {egress_info.egress_id}")
except Exception as e:
print(f"Failed to stop recording: {e}")
# Add shutdown callback to finalize the session
ctx.add_shutdown_callback(finalize_session)
Here are some key improvements we've made:
• Using lkapi.egress.start_room_composite_egress properly to kick off the recording.
• Wrapping egress operations in try-except blocks to catch and handle errors gracefully.
• Making sure the LiveKitAPI client is always closed with a finally block, so resources are cleaned up.
• Adding a shutdown callback that stops the recording and tidies up when the session ends.
If you have any questions or want to customize this further, just let me know—I'm here to help!
Sources: Universal Egress | docs/index.yaml | Custom recording templates | LiveKit docs | livekit-protocol/livekit/protocol/egress.py