|
1 | | -<!--BEGIN_BANNER_IMAGE--> |
2 | | - |
3 | | -<picture> |
4 | | - <source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png"> |
5 | | - <source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png"> |
6 | | - <img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/python-sdks/main/.github/banner_light.png"> |
7 | | -</picture> |
8 | | - |
9 | | -<!--END_BANNER_IMAGE--> |
10 | | - |
11 | | -[](https://pypi.org/project/livekit/) |
12 | | - |
13 | | -# 📹🎙️🐍 Python SDK for LiveKit |
14 | | - |
15 | | -<!--BEGIN_DESCRIPTION--> |
16 | | -Use this SDK to add real-time video, audio and data features to your Python app. By connecting to a self- or cloud-hosted <a href="https://livekit.io/">LiveKit</a> server, you can quickly build applications like interactive live streaming or video calls with just a few lines of code. |
17 | | -<!--END_DESCRIPTION--> |
18 | | - |
19 | | -This repo contains two packages |
20 | | - |
21 | | -- [livekit](https://pypi.org/project/livekit/): Real-time SDK for connecting to LiveKit as a participant |
22 | | -- [livekit-api](https://pypi.org/project/livekit-api/): Access token generation and server APIs |
23 | | - |
24 | | -## Using Server API |
25 | | - |
26 | | -```shell |
27 | | -$ pip install livekit-api |
28 | | -``` |
29 | | - |
30 | | -### Generating an access token |
31 | | - |
32 | | -```python |
33 | | -from livekit import api |
34 | | -import os |
35 | | - |
36 | | -# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars |
37 | | -token = api.AccessToken() \ |
38 | | - .with_identity("python-bot") \ |
39 | | - .with_name("Python Bot") \ |
40 | | - .with_grants(api.VideoGrants( |
41 | | - room_join=True, |
42 | | - room="my-room", |
43 | | - )).to_jwt() |
44 | | -``` |
45 | | - |
46 | | -### Creating a room |
47 | | - |
48 | | -RoomService uses asyncio and aiohttp to make API calls. It needs to be used with an event loop. |
49 | | - |
50 | | -```python |
51 | | -from livekit import api |
52 | | -import asyncio |
53 | | - |
54 | | -async def main(): |
55 | | - lkapi = api.LiveKitAPI( |
56 | | - 'http://localhost:7880', |
57 | | - ) |
58 | | - room_info = await lkapi.room.create_room( |
59 | | - api.CreateRoomRequest(name="my-room"), |
60 | | - ) |
61 | | - print(room_info) |
62 | | - results = await lkapi.room.list_rooms(api.ListRoomsRequest()) |
63 | | - print(results) |
64 | | - await lkapi.aclose() |
65 | | - |
66 | | -asyncio.get_event_loop().run_until_complete(main()) |
67 | | -``` |
68 | | - |
69 | | -## Using Real-time SDK |
70 | | - |
71 | | -```shell |
72 | | -$ pip install livekit |
73 | | -``` |
74 | | - |
75 | | -### Connecting to a room |
76 | | - |
77 | | -```python |
78 | | -from livekit import rtc |
79 | | - |
80 | | -async def main(): |
81 | | - room = rtc.Room() |
82 | | - |
83 | | - @room.on("participant_connected") |
84 | | - def on_participant_connected(participant: rtc.RemoteParticipant): |
85 | | - logging.info( |
86 | | - "participant connected: %s %s", participant.sid, participant.identity) |
87 | | - |
88 | | - async def receive_frames(stream: rtc.VideoStream): |
89 | | - async for frame in video_stream: |
90 | | - # received a video frame from the track, process it here |
91 | | - pass |
92 | | - |
93 | | - # track_subscribed is emitted whenever the local participant is subscribed to a new track |
94 | | - @room.on("track_subscribed") |
95 | | - def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant): |
96 | | - logging.info("track subscribed: %s", publication.sid) |
97 | | - if track.kind == rtc.TrackKind.KIND_VIDEO: |
98 | | - video_stream = rtc.VideoStream(track) |
99 | | - asyncio.ensure_future(receive_frames(video_stream)) |
100 | | - |
101 | | - # By default, autosubscribe is enabled. The participant will be subscribed to |
102 | | - # all published tracks in the room |
103 | | - await room.connect(URL, TOKEN) |
104 | | - logging.info("connected to room %s", room.name) |
105 | | - |
106 | | - # participants and tracks that are already available in the room |
107 | | - # participant_connected and track_published events will *not* be emitted for them |
108 | | - for participant in room.participants.items(): |
109 | | - for publication in participant.tracks.items(): |
110 | | - print("track publication: %s", publication.sid) |
111 | | -``` |
112 | | - |
113 | | -## Examples |
114 | | - |
115 | | -- [Facelandmark](https://github.com/livekit/python-sdks/tree/main/examples/face_landmark): Use mediapipe to detect face landmarks (eyes, nose ...) |
116 | | -- [Basic room](https://github.com/livekit/python-sdks/blob/main/examples/basic_room.py): Connect to a room |
117 | | -- [Publish hue](https://github.com/livekit/python-sdks/blob/main/examples/publish_hue.py): Publish a rainbow video track |
118 | | -- [Publish wave](https://github.com/livekit/python-sdks/blob/main/examples/publish_wave.py): Publish a sine wave |
119 | | - |
120 | | -## Getting help / Contributing |
121 | | - |
122 | | -Please join us on [Slack](https://livekit.io/join-slack) to get help from our devs / community members. We welcome your contributions(PRs) and details can be discussed there. |
123 | | - |
124 | | -<!--BEGIN_REPO_NAV--> |
125 | | -<br/><table> |
126 | | -<thead><tr><th colspan="2">LiveKit Ecosystem</th></tr></thead> |
127 | | -<tbody> |
128 | | -<tr><td>Real-time SDKs</td><td><a href="https://github.com/livekit/components-js">React Components</a> · <a href="https://github.com/livekit/client-sdk-js">Browser</a> · <a href="https://github.com/livekit/client-sdk-swift">iOS/macOS</a> · <a href="https://github.com/livekit/client-sdk-android">Android</a> · <a href="https://github.com/livekit/client-sdk-flutter">Flutter</a> · <a href="https://github.com/livekit/client-sdk-react-native">React Native</a> · <a href="https://github.com/livekit/rust-sdks">Rust</a> · <a href="https://github.com/livekit/node-sdks">Node.js</a> · <b>Python</b> · <a href="https://github.com/livekit/client-sdk-unity-web">Unity (web)</a> · <a href="https://github.com/livekit/client-sdk-unity">Unity (beta)</a></td></tr><tr></tr> |
129 | | -<tr><td>Server APIs</td><td><a href="https://github.com/livekit/node-sdks">Node.js</a> · <a href="https://github.com/livekit/server-sdk-go">Golang</a> · <a href="https://github.com/livekit/server-sdk-ruby">Ruby</a> · <a href="https://github.com/livekit/server-sdk-kotlin">Java/Kotlin</a> · <b>Python</b> · <a href="https://github.com/livekit/rust-sdks">Rust</a> · <a href="https://github.com/agence104/livekit-server-sdk-php">PHP (community)</a></td></tr><tr></tr> |
130 | | -<tr><td>Agents Frameworks</td><td><a href="https://github.com/livekit/agents">Python</a> · <a href="https://github.com/livekit/agent-playground">Playground</a></td></tr><tr></tr> |
131 | | -<tr><td>Services</td><td><a href="https://github.com/livekit/livekit">Livekit server</a> · <a href="https://github.com/livekit/egress">Egress</a> · <a href="https://github.com/livekit/ingress">Ingress</a> · <a href="https://github.com/livekit/sip">SIP</a></td></tr><tr></tr> |
132 | | -<tr><td>Resources</td><td><a href="https://docs.livekit.io">Docs</a> · <a href="https://github.com/livekit-examples">Example apps</a> · <a href="https://livekit.io/cloud">Cloud</a> · <a href="https://docs.livekit.io/oss/deployment">Self-hosting</a> · <a href="https://github.com/livekit/livekit-cli">CLI</a></td></tr> |
133 | | -</tbody> |
134 | | -</table> |
135 | | -<!--END_REPO_NAV--> |
| 1 | +<!--BEGIN_BANNER_IMAGE--> |
| 2 | + |
| 3 | +<picture> |
| 4 | + <source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png"> |
| 5 | + <source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png"> |
| 6 | + <img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/python-sdks/main/.github/banner_light.png"> |
| 7 | +</picture> |
| 8 | + |
| 9 | +<!--END_BANNER_IMAGE--> |
| 10 | + |
| 11 | +[](https://pypi.org/project/livekit/) |
| 12 | + |
| 13 | +# 📹🎙️🐍 Python SDK for LiveKit |
| 14 | + |
| 15 | +<!--BEGIN_DESCRIPTION--> |
| 16 | + |
| 17 | +Use this SDK to add real-time video, audio and data features to your Python app. By connecting to a self- or cloud-hosted <a href="https://livekit.io/">LiveKit</a> server, you can quickly build applications like interactive live streaming or video calls with just a few lines of code. |
| 18 | + |
| 19 | +<!--END_DESCRIPTION--> |
| 20 | + |
| 21 | +This repo contains two packages |
| 22 | + |
| 23 | +- [livekit](https://pypi.org/project/livekit/): Real-time SDK for connecting to LiveKit as a participant |
| 24 | +- [livekit-api](https://pypi.org/project/livekit-api/): Access token generation and server APIs |
| 25 | + |
| 26 | +## Using Server API |
| 27 | + |
| 28 | +```shell |
| 29 | +$ pip install livekit-api |
| 30 | +``` |
| 31 | + |
| 32 | +### Generating an access token |
| 33 | + |
| 34 | +```python |
| 35 | +from livekit import api |
| 36 | +import os |
| 37 | + |
| 38 | +# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars |
| 39 | +token = api.AccessToken() \ |
| 40 | + .with_identity("python-bot") \ |
| 41 | + .with_name("Python Bot") \ |
| 42 | + .with_grants(api.VideoGrants( |
| 43 | + room_join=True, |
| 44 | + room="my-room", |
| 45 | + )).to_jwt() |
| 46 | +``` |
| 47 | + |
| 48 | +### Creating a room |
| 49 | + |
| 50 | +RoomService uses asyncio and aiohttp to make API calls. It needs to be used with an event loop. |
| 51 | + |
| 52 | +```python |
| 53 | +from livekit import api |
| 54 | +import asyncio |
| 55 | + |
| 56 | +async def main(): |
| 57 | + lkapi = api.LiveKitAPI( |
| 58 | + 'http://localhost:7880', |
| 59 | + ) |
| 60 | + room_info = await lkapi.room.create_room( |
| 61 | + api.CreateRoomRequest(name="my-room"), |
| 62 | + ) |
| 63 | + print(room_info) |
| 64 | + results = await lkapi.room.list_rooms(api.ListRoomsRequest()) |
| 65 | + print(results) |
| 66 | + await lkapi.aclose() |
| 67 | + |
| 68 | +asyncio.get_event_loop().run_until_complete(main()) |
| 69 | +``` |
| 70 | + |
| 71 | +## Using Real-time SDK |
| 72 | + |
| 73 | +```shell |
| 74 | +$ pip install livekit |
| 75 | +``` |
| 76 | + |
| 77 | +### Connecting to a room |
| 78 | + |
| 79 | +```python |
| 80 | +from livekit import rtc |
| 81 | + |
| 82 | +async def main(): |
| 83 | + room = rtc.Room() |
| 84 | + |
| 85 | + @room.on("participant_connected") |
| 86 | + def on_participant_connected(participant: rtc.RemoteParticipant): |
| 87 | + logging.info( |
| 88 | + "participant connected: %s %s", participant.sid, participant.identity) |
| 89 | + |
| 90 | + async def receive_frames(stream: rtc.VideoStream): |
| 91 | + async for frame in video_stream: |
| 92 | + # received a video frame from the track, process it here |
| 93 | + pass |
| 94 | + |
| 95 | + # track_subscribed is emitted whenever the local participant is subscribed to a new track |
| 96 | + @room.on("track_subscribed") |
| 97 | + def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant): |
| 98 | + logging.info("track subscribed: %s", publication.sid) |
| 99 | + if track.kind == rtc.TrackKind.KIND_VIDEO: |
| 100 | + video_stream = rtc.VideoStream(track) |
| 101 | + asyncio.ensure_future(receive_frames(video_stream)) |
| 102 | + |
| 103 | + # By default, autosubscribe is enabled. The participant will be subscribed to |
| 104 | + # all published tracks in the room |
| 105 | + await room.connect(URL, TOKEN) |
| 106 | + logging.info("connected to room %s", room.name) |
| 107 | + |
| 108 | + # participants and tracks that are already available in the room |
| 109 | + # participant_connected and track_published events will *not* be emitted for them |
| 110 | + for participant in room.participants.items(): |
| 111 | + for publication in participant.tracks.items(): |
| 112 | + print("track publication: %s", publication.sid) |
| 113 | +``` |
| 114 | + |
| 115 | +### Sending and receiving chat |
| 116 | + |
| 117 | +```python |
| 118 | + |
| 119 | +room = rtc.Room() |
| 120 | +... |
| 121 | + |
| 122 | +chat = rtc.ChatManager(room) |
| 123 | + |
| 124 | +# receiving chat |
| 125 | +@chat.on("message_received") |
| 126 | +def on_message_received(msg: rtc.ChatMessage): |
| 127 | + print(f"message received: {msg.participant.identity}: {msg.message}") |
| 128 | + |
| 129 | +# sending chat |
| 130 | +await chat.send_message("hello world") |
| 131 | +``` |
| 132 | + |
| 133 | +## Examples |
| 134 | + |
| 135 | +- [Facelandmark](https://github.com/livekit/python-sdks/tree/main/examples/face_landmark): Use mediapipe to detect face landmarks (eyes, nose ...) |
| 136 | +- [Basic room](https://github.com/livekit/python-sdks/blob/main/examples/basic_room.py): Connect to a room |
| 137 | +- [Publish hue](https://github.com/livekit/python-sdks/blob/main/examples/publish_hue.py): Publish a rainbow video track |
| 138 | +- [Publish wave](https://github.com/livekit/python-sdks/blob/main/examples/publish_wave.py): Publish a sine wave |
| 139 | + |
| 140 | +## Getting help / Contributing |
| 141 | + |
| 142 | +Please join us on [Slack](https://livekit.io/join-slack) to get help from our devs / community members. We welcome your contributions(PRs) and details can be discussed there. |
| 143 | + |
| 144 | +<!--BEGIN_REPO_NAV--> |
| 145 | + |
| 146 | +<br/><table> |
| 147 | + |
| 148 | +<thead><tr><th colspan="2">LiveKit Ecosystem</th></tr></thead> |
| 149 | +<tbody> |
| 150 | +<tr><td>Real-time SDKs</td><td><a href="https://github.com/livekit/components-js">React Components</a> · <a href="https://github.com/livekit/client-sdk-js">Browser</a> · <a href="https://github.com/livekit/client-sdk-swift">iOS/macOS</a> · <a href="https://github.com/livekit/client-sdk-android">Android</a> · <a href="https://github.com/livekit/client-sdk-flutter">Flutter</a> · <a href="https://github.com/livekit/client-sdk-react-native">React Native</a> · <a href="https://github.com/livekit/rust-sdks">Rust</a> · <a href="https://github.com/livekit/node-sdks">Node.js</a> · <b>Python</b> · <a href="https://github.com/livekit/client-sdk-unity-web">Unity (web)</a> · <a href="https://github.com/livekit/client-sdk-unity">Unity (beta)</a></td></tr><tr></tr> |
| 151 | +<tr><td>Server APIs</td><td><a href="https://github.com/livekit/node-sdks">Node.js</a> · <a href="https://github.com/livekit/server-sdk-go">Golang</a> · <a href="https://github.com/livekit/server-sdk-ruby">Ruby</a> · <a href="https://github.com/livekit/server-sdk-kotlin">Java/Kotlin</a> · <b>Python</b> · <a href="https://github.com/livekit/rust-sdks">Rust</a> · <a href="https://github.com/agence104/livekit-server-sdk-php">PHP (community)</a></td></tr><tr></tr> |
| 152 | +<tr><td>Agents Frameworks</td><td><a href="https://github.com/livekit/agents">Python</a> · <a href="https://github.com/livekit/agent-playground">Playground</a></td></tr><tr></tr> |
| 153 | +<tr><td>Services</td><td><a href="https://github.com/livekit/livekit">Livekit server</a> · <a href="https://github.com/livekit/egress">Egress</a> · <a href="https://github.com/livekit/ingress">Ingress</a> · <a href="https://github.com/livekit/sip">SIP</a></td></tr><tr></tr> |
| 154 | +<tr><td>Resources</td><td><a href="https://docs.livekit.io">Docs</a> · <a href="https://github.com/livekit-examples">Example apps</a> · <a href="https://livekit.io/cloud">Cloud</a> · <a href="https://docs.livekit.io/oss/deployment">Self-hosting</a> · <a href="https://github.com/livekit/livekit-cli">CLI</a></td></tr> |
| 155 | +</tbody> |
| 156 | +</table> |
| 157 | +<!--END_REPO_NAV--> |
0 commit comments