No description
  • Python 96.5%
  • Shell 3.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2024-05-19 07:10:58 -07:00
.github fix x86_64 arch for macos wheels (#197) 2024-05-01 22:16:26 +02:00
examples remove whisper example (#166) 2024-02-14 21:53:18 +01:00
livekit-api livekit-rtc v0.10.0; livekit-protocol v0.5.0; livekit-api v0.5.0 (#193) 2024-04-18 20:10:24 +02:00
livekit-protocol livekit-rtc v0.10.0; livekit-protocol v0.5.0; livekit-api v0.5.0 (#193) 2024-04-18 20:10:24 +02:00
livekit-rtc fix: allow listeners to be edited inside called callbacks (#202) 2024-05-19 07:10:58 -07:00
.gitattributes Update protocol definitions, version bump (#182) 2024-03-31 14:52:55 -07:00
.gitignore feat: end-to-end encryption (#48) 2023-09-04 18:25:02 -07:00
.gitmodules move protocol to livekit-protocol package (#89) 2023-11-07 14:27:59 -08:00
dev-requirements.txt add other api services, token verifier & webhooks (#98) 2023-11-13 22:02:24 -08:00
LICENSE initial sdk 2023-05-21 15:20:01 +02:00
NOTICE add NOTICE file (#47) 2023-08-27 10:30:41 -07:00
README.md remove whisper example (#166) 2024-02-14 21:53:18 +01:00
ruff.toml better export names for protocol (#90) 2023-11-08 17:23:55 -08:00

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

from livekit import api
import os

# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars
token = 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.

from livekit import api
import asyncio

async def main():
    lkapi = api.LiveKitAPI(
        'http://localhost:7880',
    )
    room_info = await lkapi.room.create_room(
        api.CreateRoomRequest(name="my-room"),
    )
    print(room_info)
    results = await lkapi.room.list_rooms(api.ListRoomsRequest())
    print(results)
    await lkapi.aclose()

asyncio.get_event_loop().run_until_complete(main())

Using Real-time SDK

$ pip install livekit

Connecting to a room

from livekit import rtc

async def main():
    room = rtc.Room()

    @room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
        logging.info(
            "participant connected: %s %s", participant.sid, participant.identity)

    async def receive_frames(stream: rtc.VideoStream):
        async for frame in video_stream:
            # received a video frame from the track, process it here
            pass

    # track_subscribed is emitted whenever the local participant is subscribed to a new track
    @room.on("track_subscribed")
    def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
        logging.info("track subscribed: %s", publication.sid)
        if track.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 room
    await room.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 them
    for participant in room.participants.items():
        for publication in participant.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