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