1
0
Fork 0
peertube/support/doc/api/embeds.md

270 lines
6.6 KiB
Markdown
Raw Normal View History

# 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.
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'
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
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()
```
2023-02-22 10:06:25 -05:00
## 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`
2023-02-22 10:06:25 -05:00
### start
Start the video at a specific time.
Value must be raw seconds or a duration (`3m4s`)
2023-02-22 10:06:25 -05:00
### stop
Stop the video at a specific time.
Value must be raw seconds or a duration (`54s`)
2023-02-22 10:06:25 -05:00
### 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`.
2023-02-22 10:06:25 -05:00
### controlBar
Hide control bar when the video is played.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### peertubeLink
2022-06-28 08:49:05 -04:00
Hide PeerTube instance link in control bar.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### muted
Mute the video by default.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### loop
Automatically start again the video when it ends.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### subtitle
Auto select a subtitle by default.
Value must be a valid subtitle ISO code (`fr`, `en`, etc.).
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`.
2023-02-22 10:06:25 -05:00
### title
2022-06-28 08:49:05 -04:00
Hide embed title.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### warningTitle
2022-06-28 08:49:05 -04:00
Hide P2P warning title.
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
### p2p
2022-06-28 08:49:05 -04:00
Disable P2P.
Value must be `0` or `1`.
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)`).
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)`).
2023-02-22 10:06:25 -05:00
### mode
2022-06-28 08:49:05 -04:00
Force a specific player engine.
Value must be a valid mode (`webtorrent` or `p2p-media-loader`).
2023-02-22 10:06:25 -05:00
### api
2022-06-28 08:49:05 -04:00
Enable embed JavaScript API (see methods below).
Value must be `0` or `1`.
2023-02-22 10:06:25 -05:00
## Embed methods
2023-02-22 10:06:25 -05:00
### `play() : Promise<void>`
Starts playback, or resumes playback if it is paused.
2023-02-22 10:06:25 -05:00
### `pause() : Promise<void>`
Pauses playback.
2023-02-22 10:06:25 -05:00
### `seek(positionInSeconds : number)`
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)`
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[]>`
Get the available resolutions. A `PeerTubeResolution` looks like:
```json
{
"id": 3,
"label": "720p",
2019-12-17 10:17:22 -05:00
"height": "720",
"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>`
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[]>`
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>`
Get the current playback rate. See `getPlaybackRates()` for more information.
2023-02-22 10:06:25 -05:00
### `setPlaybackRate(rate: number) : Promise<void>`
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>`
Set the playback volume. Value should be between `0` and `1`.
2023-02-22 10:06:25 -05:00
### `getVolume(): Promise<number>`
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-02-22 10:06:25 -05:00
## Embed events
You can subscribe to events by using `addEventListener()`. See above for details.
2023-02-22 10:06:25 -05:00
### Event `playbackStatusUpdate`
2020-05-06 05:54:33 -04:00
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"
}
```
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.
2023-02-22 10:06:25 -05:00
### Event `playbackStatusChange`
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.
2023-02-22 10:06:25 -05:00
### Event `resolutionUpdate`
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.