Docs › The <video-piper> web component
The <video-piper> web component
A drop-in player that plays the stream (HLS, with hls.js loaded only where the browser needs it) or the MP4, shows captions, and refreshes its links before they expire. One script, no framework, about 3 KB gzipped:
Install: each component is one script tag from this site, or, with a bundler, npm install @videopiper/elements and import "@videopiper/elements/player" (also /recorder, /editor, /thumbnail; TypeScript types included). The same scripts are on jsDelivr: https://cdn.jsdelivr.net/npm/@videopiper/elements@0/dist/cdn/recorder.js. Runnable examples: github.com/Video-Piper/demos.
<script src="https://videopiper.com/player/player.js"></script>
<video-piper links-url="/api/videos/VIDEO_ID/links"></video-piper>
links-url is your own endpoint, like the server example above: it returns JSON with playbackUrl, streamUrl, posterPlaybackUrl (or posterUrl), captionsUrl, captionsStatus (the status's captions.status) and playbackExpiresAt. The player calls it again a few minutes before the links expire (and after a playback error), keeping the viewer's place. To render the first links on the page yourself, pass them as attributes too:
<video-piper src="PLAYBACK_URL" stream-src="STREAM_URL" poster="POSTER_PLAYBACK_URL"
captions="VTT_URL" expires-at="1790910405" links-url="/api/videos/VIDEO_ID/links"
cc start="30"></video-piper>
| Attribute | Description |
|---|---|
src | The MP4 (playbackUrl). Used where there's no stream or HLS can't play. |
stream-src | The HLS stream (streamUrl, Pro and Scale). Preferred when present. |
poster | The poster image (posterPlaybackUrl). |
captions, captions-lang, captions-label | A WebVTT file for the MP4 (the stream carries its own subtitle track). |
chapters | The video's chaptersUrl (or chaptersUrl in your links-url answer): marks on the seek bar and a chapter menu. element.chapters lists them ({start, end, title}) and seekToChapter(i) jumps to one. |
captions-status | processing when the captions aren't made yet; see see the notes below on captions that finish later. |
expires-at | Unix time the links expire (playbackExpiresAt); when to refresh. |
links-url | Your endpoint for fresh links (JSON, same names as the status). |
autoplay, muted, loop | As for <video>; autoplay also mutes, as browsers require. |
cc | Show captions from the start. |
start | Start at this many seconds. |
controls | By default the player draws its own controls, the same in every browser: play, a seek bar with chapter marks (hover for the time and chapter), the time and current chapter (which opens the chapter menu), volume, captions, speed and full screen. They hide while playing and come back when the pointer moves. Keys, once the player has focus: Space or K play/pause, ←/→ 5 s, J/L 10 s, Shift+←/→ previous/next chapter, ↑/↓ volume, M mute, F full screen, C captions, 0–9 jump to 0–90%. controls="native" uses the browser's own controls instead (chapters then appear as a menu over the video); controls="false" shows none. Style the bar from your page with video-piper::part(controls). |
- No token or key in the page. The signed links are the only thing the player needs. Your API key stays on your server, and you decide who your
links-urlendpoint answers (for example, only signed-in users; the player sends your site's cookies with it). Embedding doesn't need to be turned on: that switch is only for the iframe embed below. - No endpoint? Leave out
links-urland listen for thevp-expiringevent, then callelement.setLinks({playbackUrl, streamUrl, posterPlaybackUrl, captionsUrl, playbackExpiresAt})with fresh links. - Captions that finish later. Captions are made after the video is published, so for the first minutes your
links-urlmay answer withcaptionsStatus: "processing"(and nocaptionsUrl; sends.captions?.statusands.captions?.vttUrlfrom the video's status). The player then askslinks-urlagain about every 20 seconds, easing to 45, only while the tab is visible and for at most 10 minutes. WhencaptionsUrlappears it adds the subtitles track to the playing video without reloading or interrupting it (shown at once only withcc), the CC control appears, and the element fires a bubblingvp-captionsreadyevent withdetail: { url, language }, once. If the captions areskippedorfailedit simply stops asking. Withoutlinks-urlit doesn't check; callsetLinks()when you learn the captions are ready. - Events (they bubble out of the player):
vp-loaded,vp-play,vp-pause,vp-ended,vp-seeked,vp-timeupdate(about 4 a second while playing),vp-volumechangeandvp-ratechange, each with{currentTime, duration, paused, muted}, andvp-error({message, code}:NO_VIDEO,EXPIRED, orLOAD_RETRYINGwhile it tries again), plusvp-captionsreadyandvp-expiringabove, andvp-chapterchange({index, start, end, title}) as playback enters another chapter. - Control it like a
<video>:play(),pause(), andcurrentTime(set it to seek),duration,paused,ended,muted,volume,playbackRate. - Callbacks: every event also has an
on…property that gets its detail:onLoaded,onPlay,onPause,onEnded,onSeeked,onTimeUpdate,onVolumeChange,onRateChange,onError,onCaptionsReady,onExpiring.const player = document.querySelector("video-piper"); player.onEnded = () => showNextVideo(); player.onTimeUpdate = ({ currentTime, duration }) => track(currentTime / duration); player.currentTime = 30; player.play(); - Sizing: it's 16:9 by default; style it like any element (
video-piper { width: 100%; }). - Try every option live. In the app, Account → Playground (sign in) lets you set up the player, run your page in an isolated frame with a stand-in for your server, see its events and errors, and copy the page and server code.
- Your allowed domains apply as usual: the links only play on your sites.