2018-07-10 11:47:56 -04:00
# 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
2020-05-06 05:54:33 -04:00
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.
2018-07-10 11:47:56 -04:00
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
2020-02-28 08:01:17 -05:00
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:
2018-07-10 11:47:56 -04:00
```
yarn add @peertube/embed -api
```
Now just use the `PeerTubePlayer` class exported by the module:
```typescript
2023-07-31 08:34:36 -04:00
import { PeerTubePlayer } from '@peertube/embed-api.js'
2018-07-10 11:47:56 -04:00
2019-12-17 10:17:22 -05:00
...
```
Or use the minified build from NPM CDN in your HTML file:
```
2020-04-08 08:44:25 -04:00
< script src = "https://unpkg.com/@peertube/embed-api/build/player.min.js" > < / script >
2019-12-17 10:17:22 -05:00
< script >
const PeerTubePlayer = window['PeerTubePlayer']
...
< / script >
```
Then you can instantiate the player:
```typescript
2018-07-10 11:47:56 -04:00
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)
2020-03-25 08:32:29 -04:00
player.pause()
2018-07-10 11:47:56 -04:00
```
2023-02-22 10:06:25 -05:00
## Embed URL parameters
2022-05-20 03:59:53 -04:00
You can customize PeerTube player by specifying URL query parameters.
2023-06-19 03:05:20 -04:00
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`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### start
2022-05-20 03:59:53 -04:00
Start the video at a specific time.
Value must be raw seconds or a duration (`3m4s`)
2024-01-12 10:01:19 -05:00
Default: starts at `0`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### stop
2022-05-20 03:59:53 -04:00
Stop the video at a specific time.
Value must be raw seconds or a duration (`54s`)
2024-01-12 10:01:19 -05:00
Default: ends at content end
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### controls
2022-05-20 03:59:53 -04:00
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` .
2024-01-12 10:01:19 -05:00
Default: `1`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### controlBar
2022-05-20 03:59:53 -04:00
Hide control bar when the video is played.
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `1`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### peertubeLink
2022-05-20 03:59:53 -04:00
2022-06-28 08:49:05 -04:00
Hide PeerTube instance link in control bar.
2022-05-20 03:59:53 -04:00
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `1`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### muted
2022-05-20 03:59:53 -04:00
Mute the video by default.
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: tries to restore the last muted setting set by the user
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### loop
2022-05-20 03:59:53 -04:00
Automatically start again the video when it ends.
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `0`
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### subtitle
2022-05-20 03:59:53 -04:00
Auto select a subtitle by default.
Value must be a valid subtitle ISO code (`fr`, `en` , etc.).
2024-01-12 10:01:19 -05:00
Default: no subtitle selected and then tries to restore the last subtitle set by the user
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
### autoplay
2022-06-28 08:49:05 -04:00
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` .
2024-01-12 10:01:19 -05:00
Default: `0`
2022-06-28 08:49:05 -04:00
2023-06-19 03:05:20 -04:00
### playbackRate
Force the default playback rate (`0.75`, `1.5` etc).
2024-01-12 10:01:19 -05:00
Default: `1`
2023-06-19 03:05:20 -04:00
2023-02-22 10:06:25 -05:00
### title
2022-06-28 08:49:05 -04:00
2024-01-12 10:01:19 -05:00
Show/Hide embed title.
2022-06-28 08:49:05 -04:00
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `1`
2022-06-28 08:49:05 -04:00
2023-02-22 10:06:25 -05:00
### warningTitle
2022-06-28 08:49:05 -04:00
2024-01-12 10:01:19 -05:00
Show/Hide P2P warning title.
2022-06-28 08:49:05 -04:00
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `1`
2022-06-28 08:49:05 -04:00
2023-02-22 10:06:25 -05:00
### p2p
2022-06-28 08:49:05 -04:00
2024-01-12 10:01:19 -05:00
Enable/Disable P2P.
2022-06-28 08:49:05 -04:00
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: tries to use the user setting and fallbacks to instance setting if user setting is not found
2022-06-28 08:49:05 -04:00
2023-02-22 10:06:25 -05:00
### bigPlayBackgroundColor
2022-06-28 08:49:05 -04:00
Customize big play button background color.
Value must be a valid color (`red` or `rgba(100, 100, 100, 0.5)` ).
2024-01-12 10:01:19 -05:00
Default: rgba(0, 0, 0, 0.8)
2022-06-28 08:49:05 -04:00
2023-02-22 10:06:25 -05:00
### foregroundColor
2022-06-28 08:49:05 -04:00
Customize embed font color.
Value must be a valid color (`red` or `rgba(100, 100, 100, 0.5)` ).
2024-01-12 10:01:19 -05:00
Default: `white`
2023-02-22 10:06:25 -05:00
### mode
2022-06-28 08:49:05 -04:00
Force a specific player engine.
2023-07-11 03:21:13 -04:00
Value must be a valid mode (`web-video` or `p2p-media-loader` ).
2022-06-28 08:49:05 -04:00
2024-01-12 10:01:19 -05:00
See behaviour description [here ](https://docs.joinpeertube.org/admin/configuration#vod-transcoding )
Default: `p2p-media-loader` and fallback to `web-video` mode.
2023-02-22 10:06:25 -05:00
### api
2022-06-28 08:49:05 -04:00
2024-01-12 10:01:19 -05:00
Enable/Disable embed JavaScript API (see methods below).
2022-06-28 08:49:05 -04:00
Value must be `0` or `1` .
2024-01-12 10:01:19 -05:00
Default: `0`
2023-11-23 02:14:54 -05:00
### waitPasswordFromEmbedAPI
**PeerTube >= 6.0**
If the video requires a password, PeerTube will wait a password provided by `setVideoPassword` method before loading the video.
Until you provide a password, `player.ready` is not resolved.
2024-01-12 10:01:19 -05:00
Value must be `0` or `1` .
Default: `0`
2023-11-23 02:14:54 -05:00
## Embed attributes
### `ready: Promise<void>`
This promise is resolved when the video is loaded an the player is ready.
2022-05-20 03:59:53 -04:00
2023-02-22 10:06:25 -05:00
## Embed methods
2018-07-10 11:47:56 -04:00
2023-02-22 10:06:25 -05:00
### `play() : Promise<void>`
2018-07-10 11:47:56 -04:00
Starts playback, or resumes playback if it is paused.
2023-02-22 10:06:25 -05:00
### `pause() : Promise<void>`
2018-07-10 11:47:56 -04:00
Pauses playback.
2023-02-22 10:06:25 -05:00
### `seek(positionInSeconds : number)`
2018-07-10 11:47:56 -04:00
Seek to the given position, as specified in seconds into the video.
2023-02-22 10:06:25 -05:00
### `addEventListener(eventName : string, handler : Function)`
2018-07-10 11:47:56 -04:00
Add a listener for a specific event. See below for the available events.
2023-02-22 10:06:25 -05:00
### `removeEventListener(eventName : string, handler : Function)`
2022-09-09 05:40:14 -04:00
Remove a listener.
2023-02-22 10:06:25 -05:00
### `getResolutions() : Promise<PeerTubeResolution[]>`
2018-07-10 11:47:56 -04:00
Get the available resolutions. A `PeerTubeResolution` looks like:
```json
{
"id": 3,
"label": "720p",
2019-12-17 10:17:22 -05:00
"height": "720",
2018-07-10 11:47:56 -04:00
"active": true
}
```
`active` is true if the resolution is the currently selected resolution.
2023-02-22 10:06:25 -05:00
### `setResolution(resolutionId : number): Promise<void>`
2018-07-10 11:47:56 -04:00
Change the current resolution. Pass `-1` for automatic resolution (when available).
Otherwise, `resolutionId` should be the ID of an object returned by `getResolutions()`
2023-02-22 10:06:25 -05:00
### `getPlaybackRates() : Promise<number[]>`
2018-07-10 11:47:56 -04:00
Get the available playback rates, where `1` represents normal speed, `0.5` is half speed, `2` is double speed, etc.
2023-02-22 10:06:25 -05:00
### `getPlaybackRates() : Promise<number>`
2018-07-10 11:47:56 -04:00
Get the current playback rate. See `getPlaybackRates()` for more information.
2023-02-22 10:06:25 -05:00
### `setPlaybackRate(rate: number) : Promise<void>`
2018-07-10 11:47:56 -04:00
Set the current playback rate. The passed rate should be a value as returned by `getPlaybackRates()` .
2023-02-22 10:06:25 -05:00
### `setVolume(factor: number) : Promise<void>`
2018-07-10 11:47:56 -04:00
Set the playback volume. Value should be between `0` and `1` .
2023-02-22 10:06:25 -05:00
### `getVolume(): Promise<number>`
2018-07-10 11:47:56 -04:00
Get the playback volume. Returns a value between `0` and `1` .
2019-12-17 10:17:22 -05:00
2023-02-22 10:06:25 -05:00
### `setCaption(id: string) : Promise<void>`
2020-05-06 05:54:33 -04:00
Update current caption using the caption id.
2023-02-22 10:06:25 -05:00
### `getCaptions(): Promise<{ id: string, label: string, src: string, mode: 'disabled' | 'showing' }>`
2020-05-06 05:54:33 -04:00
Get video captions.
2023-02-22 10:06:25 -05:00
### `playNextVideo(): Promise<void>`
2020-08-05 08:16:39 -04:00
Play next video in playlist.
2023-02-22 10:06:25 -05:00
### `playPreviousVideo(): Promise<void>`
2020-08-05 08:16:39 -04:00
Play previous video in playlist.
2023-02-22 10:06:25 -05:00
### `getCurrentPosition(): Promise<void>`
2020-08-05 08:16:39 -04:00
Get current position in playlist (starts from 1).
2023-11-23 02:14:54 -05:00
### `setVideoPassword(): Promise<void>`
**PeerTube >= 6.0**
Set the video password so the user doesn't have to manually fill it.
`waitPasswordFromEmbedAPI=1` is required in embed URL.
2023-02-22 10:06:25 -05:00
## Embed events
2018-07-10 11:47:56 -04:00
You can subscribe to events by using `addEventListener()` . See above for details.
2023-02-22 10:06:25 -05:00
### Event `playbackStatusUpdate`
2018-07-10 11:47:56 -04:00
2020-05-06 05:54:33 -04:00
Fired every half second to provide the current status of playback.
2020-04-08 08:51:44 -04:00
The parameter of the callback will resemble:
2018-07-10 11:47:56 -04:00
```json
{
"position": 22.3,
"volume": 0.9,
2020-04-08 08:51:44 -04:00
"duration": "171.37499",
2018-07-10 11:47:56 -04:00
"playbackState": "playing"
}
```
2020-04-14 03:02:44 -04:00
`duration` field and `ended` `playbackState` are available in PeerTube >= 2.2.
2020-04-08 08:39:31 -04:00
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.
2018-07-10 11:47:56 -04:00
2023-02-22 10:06:25 -05:00
### Event `playbackStatusChange`
2018-07-10 11:47:56 -04:00
2020-03-20 10:04:02 -04:00
Fired when playback transitions between states, such as `paused` and `playing` . More states may be added later.
2018-07-10 11:47:56 -04:00
2023-02-22 10:06:25 -05:00
### Event `resolutionUpdate`
2018-07-10 11:47:56 -04:00
2019-12-17 09:19:42 -05:00
Fired when the available resolutions have changed, or when the currently selected resolution has changed. Listener should call `getResolutions()` to get the updated information.
2023-02-22 10:06:25 -05:00
### Event `volumeChange`
2019-12-17 09:19:42 -05:00
Fired when the player volume changed.