Skip to content

Repository files navigation

The LiveKit icon, the name of the repository and some sample code in the background.

pypi-v

📹🎙️🐍 Python SDK for LiveKit

Use this SDK to add real-time video, audio and data features to your Python app. By connecting to a self- or cloud-hosted LiveKit server, you can quickly build applications like interactive live streaming or video calls with just a few lines of code.

This repo contains two packages

  • livekit: Real-time SDK for connecting to LiveKit as a participant
  • livekit-api: Access token generation and server APIs

Using Server API

$ pip install livekit-api

Generating an access token

fromlivekitimportapiimportos# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env varstoken=api.AccessToken() \
.with_identity("python-bot") \
.with_name("Python Bot") \
.with_grants(api.VideoGrants(
room_join=True,
room="my-room",
)).to_jwt()

Creating a room

RoomService uses asyncio and aiohttp to make API calls. It needs to be used with an event loop.

fromlivekitimportapiimportasyncioasyncdefmain():
lkapi=api.LiveKitAPI(
'http://localhost:7880',
)
room_info=awaitlkapi.room.create_room(
api.CreateRoomRequest(name="my-room"),
)
print(room_info)
results=awaitlkapi.room.list_rooms(api.ListRoomsRequest())
print(results)
awaitlkapi.aclose()
asyncio.get_event_loop().run_until_complete(main())

Using Real-time SDK

$ pip install livekit

Connecting to a room

fromlivekitimportrtcasyncdefmain():
room=rtc.Room()
@room.on("participant_connected")defon_participant_connected(participant: rtc.RemoteParticipant):
logging.info(
"participant connected: %s %s", participant.sid, participant.identity)
asyncdefreceive_frames(stream: rtc.VideoStream):
asyncforframeinvideo_stream:
# received a video frame from the track, process it herepass# track_subscribed is emitted whenever the local participant is subscribed to a new track@room.on("track_subscribed")defon_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
logging.info("track subscribed: %s", publication.sid)
iftrack.kind==rtc.TrackKind.KIND_VIDEO:
video_stream=rtc.VideoStream(track)
asyncio.ensure_future(receive_frames(video_stream))
# By default, autosubscribe is enabled. The participant will be subscribed to# all published tracks in the roomawaitroom.connect(URL, TOKEN)
logging.info("connected to room %s", room.name)
# participants and tracks that are already available in the room# participant_connected and track_published events will *not* be emitted for themforparticipantinroom.participants.items():
forpublicationinparticipant.tracks.items():
print("track publication: %s", publication.sid)

Examples

Getting help / Contributing

Please join us on Slack to get help from our devs / community members. We welcome your contributions(PRs) and details can be discussed there.


LiveKit Ecosystem
Real-time SDKsReact Components · JavaScript · iOS/macOS · Android · Flutter · React Native · Rust · Python · Unity (web) · Unity (beta)
Server APIsNode.js · Golang · Ruby · Java/Kotlin · Python · Rust · PHP (community)
Agents FrameworksPython · Playground
ServicesLivekit server · Egress · Ingress · SIP
ResourcesDocs · Example apps · Cloud · Self-hosting · CLI

About

LiveKit real-time SDK and server API for Python

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages