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