]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - support/doc/api/embeds.md
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / support / doc / api / embeds.md
CommitLineData
99941732
WL
1# PeerTube Embed API
2
3PeerTube lets you embed videos and programmatically control their playback. This documentation covers how to interact with the PeerTube Embed API.
4
5## Playground
6
1151f521
C
7Any PeerTube embed URL (ie `https://my-instance.example.com/videos/embed/52a10666-3a18-4e73-93da-e8d3c12c305a`) can be viewed as an embedding playground which
8allows you to test various aspects of PeerTube embeds. Simply replace `/embed` with `/test-embed` and visit the URL in a browser.
99941732
WL
9For instance, the playground URL for the above embed URL is `https://my-instance.example.com/videos/test-embed/52a10666-3a18-4e73-93da-e8d3c12c305a`.
10
11## Quick Start
12
9a207a71
C
13Given an existing PeerTube embed `<iframe>` **with API enabled** (`https://my-instance.example.com/videos/embed/52a10666-3a18-4e73-93da-e8d3c12c305a?api=1`),
14one can use the PeerTube Embed API to control it by first including the library. You can include it via Yarn with:
99941732
WL
15
16```
17yarn add @peertube/embed-api
18```
19
20Now just use the `PeerTubePlayer` class exported by the module:
21
22```typescript
23import { PeerTubePlayer } from '@peertube/embed-api'
24
03d641a0
C
25...
26```
27
28Or use the minified build from NPM CDN in your HTML file:
29
30```
afd1a6ed 31<script src="https://unpkg.com/@peertube/embed-api/build/player.min.js"></script>
03d641a0
C
32
33<script>
34 const PeerTubePlayer = window['PeerTubePlayer']
35
36 ...
37</script>
38```
39
40Then you can instantiate the player:
41
42```typescript
99941732
WL
43let player = new PeerTubePlayer(document.querySelector('iframe'))
44await player.ready // wait for the player to be ready
45
46// now you can use it!
47player.play()
48player.seek(32)
b2b0ce8a 49player.pause()
99941732
WL
50```
51
6a160a0c 52## Embed URL parameters
60f013e1
C
53
54You can customize PeerTube player by specifying URL query parameters.
55For example `https://my-instance.example.com/videos/embed/52a10666-3a18-4e73-93da-e8d3c12c305a??start=1s&stop=18s&loop=1&autoplay=1&muted=1&warningTitle=0&controlBar=0&peertubeLink=0&p2p=0`
56
6a160a0c 57### start
60f013e1
C
58
59Start the video at a specific time.
60Value must be raw seconds or a duration (`3m4s`)
61
6a160a0c 62### stop
60f013e1
C
63
64Stop the video at a specific time.
65Value must be raw seconds or a duration (`54s`)
66
6a160a0c 67### controls
60f013e1
C
68
69Mimics video HTML element `controls` attribute, meaning that all controls (including big play button, control bar, etc.) will be removed.
70It can be useful if you want to have a full control of the PeerTube player.
71
72Value must be `0` or `1`.
73
6a160a0c 74### controlBar
60f013e1
C
75
76Hide control bar when the video is played.
77
78Value must be `0` or `1`.
79
6a160a0c 80### peertubeLink
60f013e1 81
4c8336af 82Hide PeerTube instance link in control bar.
60f013e1
C
83
84Value must be `0` or `1`.
85
6a160a0c 86### muted
60f013e1
C
87
88Mute the video by default.
89
90Value must be `0` or `1`.
91
6a160a0c 92### loop
60f013e1
C
93
94Automatically start again the video when it ends.
95
96Value must be `0` or `1`.
97
6a160a0c 98### subtitle
60f013e1
C
99
100Auto select a subtitle by default.
101
102Value must be a valid subtitle ISO code (`fr`, `en`, etc.).
103
6a160a0c 104### autoplay
4c8336af
C
105
106Try to automatically play the video.
107Most web browsers disable video autoplay if the user did not interact with the video. You can try to bypass this limitation by muting the video
108
109Value must be `0` or `1`.
110
6a160a0c 111### title
4c8336af
C
112
113Hide embed title.
114
115Value must be `0` or `1`.
116
6a160a0c 117### warningTitle
4c8336af
C
118
119Hide P2P warning title.
120
121Value must be `0` or `1`.
122
6a160a0c 123### p2p
4c8336af
C
124
125Disable P2P.
126
127Value must be `0` or `1`.
128
6a160a0c 129### bigPlayBackgroundColor
4c8336af
C
130
131Customize big play button background color.
132
133Value must be a valid color (`red` or `rgba(100, 100, 100, 0.5)`).
134
6a160a0c 135### foregroundColor
4c8336af
C
136
137Customize embed font color.
138
139Value must be a valid color (`red` or `rgba(100, 100, 100, 0.5)`).
140
6a160a0c 141### mode
4c8336af
C
142
143Force a specific player engine.
144
145Value must be a valid mode (`webtorrent` or `p2p-media-loader`).
146
6a160a0c 147### api
4c8336af
C
148
149Enable embed JavaScript API (see methods below).
150
151Value must be `0` or `1`.
152
60f013e1 153
6a160a0c 154## Embed methods
99941732 155
6a160a0c 156### `play() : Promise<void>`
99941732
WL
157
158Starts playback, or resumes playback if it is paused.
159
6a160a0c 160### `pause() : Promise<void>`
99941732
WL
161
162Pauses playback.
163
6a160a0c 164### `seek(positionInSeconds : number)`
99941732
WL
165
166Seek to the given position, as specified in seconds into the video.
167
6a160a0c 168### `addEventListener(eventName : string, handler : Function)`
99941732
WL
169
170Add a listener for a specific event. See below for the available events.
171
6a160a0c 172### `removeEventListener(eventName : string, handler : Function)`
914af0d9
C
173
174Remove a listener.
175
6a160a0c 176### `getResolutions() : Promise<PeerTubeResolution[]>`
99941732
WL
177
178Get the available resolutions. A `PeerTubeResolution` looks like:
179
180```json
181{
182 "id": 3,
183 "label": "720p",
03d641a0 184 "height": "720",
99941732
WL
185 "active": true
186}
187```
188
189`active` is true if the resolution is the currently selected resolution.
190
6a160a0c 191### `setResolution(resolutionId : number): Promise<void>`
99941732
WL
192
193Change the current resolution. Pass `-1` for automatic resolution (when available).
194Otherwise, `resolutionId` should be the ID of an object returned by `getResolutions()`
195
6a160a0c 196### `getPlaybackRates() : Promise<number[]>`
99941732
WL
197
198Get the available playback rates, where `1` represents normal speed, `0.5` is half speed, `2` is double speed, etc.
199
6a160a0c 200### `getPlaybackRates() : Promise<number>`
99941732
WL
201
202Get the current playback rate. See `getPlaybackRates()` for more information.
203
6a160a0c 204### `setPlaybackRate(rate: number) : Promise<void>`
99941732
WL
205
206Set the current playback rate. The passed rate should be a value as returned by `getPlaybackRates()`.
207
6a160a0c 208### `setVolume(factor: number) : Promise<void>`
99941732
WL
209
210Set the playback volume. Value should be between `0` and `1`.
211
6a160a0c 212### `getVolume(): Promise<number>`
99941732
WL
213
214Get the playback volume. Returns a value between `0` and `1`.
03d641a0 215
6a160a0c 216### `setCaption(id: string) : Promise<void>`
1151f521
C
217
218Update current caption using the caption id.
219
6a160a0c 220### `getCaptions(): Promise<{ id: string, label: string, src: string, mode: 'disabled' | 'showing' }>`
1151f521
C
221
222Get video captions.
223
6a160a0c 224### `playNextVideo(): Promise<void>`
9054a8b6
C
225
226Play next video in playlist.
227
6a160a0c 228### `playPreviousVideo(): Promise<void>`
9054a8b6
C
229
230Play previous video in playlist.
231
6a160a0c 232### `getCurrentPosition(): Promise<void>`
9054a8b6
C
233
234Get current position in playlist (starts from 1).
235
6a160a0c 236## Embed events
99941732
WL
237
238You can subscribe to events by using `addEventListener()`. See above for details.
239
6a160a0c 240### Event `playbackStatusUpdate`
99941732 241
1151f521 242Fired every half second to provide the current status of playback.
6ccdf9d5 243The parameter of the callback will resemble:
99941732
WL
244
245```json
246{
247 "position": 22.3,
248 "volume": 0.9,
6ccdf9d5 249 "duration": "171.37499",
99941732
WL
250 "playbackState": "playing"
251}
252```
253
624a0221
C
254`duration` field and `ended` `playbackState` are available in PeerTube >= 2.2.
255
96aae68c
C
256The `volume` field contains the volume from `0` (silent) to `1` (full volume).
257The `playbackState` can be `unstarted`, `playing`, `paused` or `ended`. More states may be added later.
99941732 258
6a160a0c 259### Event `playbackStatusChange`
99941732 260
5ab994fe 261Fired when playback transitions between states, such as `paused` and `playing`. More states may be added later.
99941732 262
6a160a0c 263### Event `resolutionUpdate`
99941732 264
478924a0
C
265Fired when the available resolutions have changed, or when the currently selected resolution has changed. Listener should call `getResolutions()` to get the updated information.
266
6a160a0c 267### Event `volumeChange`
478924a0
C
268
269Fired when the player volume changed.