The video player web component
videobana-web-component · Complete guide to Videobana's <video-player> web component (widget.js): adding it to a page and to React, Next.js, Vue, Svelte, Angular, Astro, WordPress and site builders; where the video comes from (a public video, a demo, a JSON file, inline JSON or a JavaScript object); every attribute and property with its default and how values are read; control bar parts and keyboard shortcuts; autoplay and sound; fit, size and hero backgrounds; JavaScript members and events; styling; CORS and Content-Security-Policy requirements; and troubleshooting. Use it when embedding or playing a Videobana video in a website or web app, or when scripting, sizing or styling the player.
Put a Videobana video on any web page with one script and one tag: <video-player>. It plays the same player as the app and the demos, with the voice-over, the captions and the controls. This skill covers everything the web component guide says, generated from the same data, and the exact behaviour of the widget's source.
Quick start
- Add the script, once per page:
<script src="https://videobana.vercel.app/widget.js" async></script>. It is small (a few kilobytes) and only defines the tag. The player itself is downloaded when a video comes into view. - Add the tag. Point
srcat a video. Here it is the ad for Videobana, one of the demos (ads-videobana).
<script src="https://videobana.vercel.app/widget.js" async></script>
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana"></video-player>Your tag starts playing by itself when it comes into view, unless you write autoplay="false" (the demos on the guide do, so that page stays quiet). The script tag may come before or after the element, and anywhere on the page.
Where the video comes from
The tag accepts the video in four ways. They can be combined with a change of mind later: set spec or src from your code and the new video replaces the old one.
URL
<!-- A video of the platform, or any JSON file that your site or another one serves -->
<video-player src="https://videobana.vercel.app/api/public/videos/0123456789abcdef"></video-player>
<video-player src="/videos/launch.json"></video-player>JSON text
<video-player spec='{"version":1,"title":"Hello","domain":"science","scenes":[
{"kind":"intro","say":["Hello from inline JSON."]},
{"kind":"statement","text":"It **works**.","say":"It works."}
]}'></video-player>Inline script
<video-player>
<script type="application/json">
{ "version": 1, "title": "Hello", "domain": "science", "scenes": [ /* … */ ] }
</script>
</video-player>JavaScript object
const player = document.querySelector("video-player");
player.spec = { version: 1, title: "Hello", domain: "science", scenes: [/* … */] }; // an object, or JSON text
player.src = "https://videobana.vercel.app/api/examples/ads-videobana"; // or an address| Address or JSON | What you get |
|---|---|
https://videobana.vercel.app/api/public/videos/<id> | A video you made public in the dashboard. Its voice-over, timing and pictures come with it. The Share dialog has a ready-made snippet (the Web component tab). |
https://videobana.vercel.app/api/examples/<id> | Any video of the demos (demos), with its recorded voice-over. The id is the last part of its demo address. |
| A JSON file, or JSON text | The video document, as described in the JSON format. It is checked first; a problem is shown in the frame, with its place in the document, and sent as an error event. |
Voice-over comes with a video of the platform
A JSON document has no recorded voice: it plays without narration, with its timing worked out from the text (music and sounds still play). Public videos and demos bring their voice-over. Pictures and files in the JSON may be addresses on any site. A relative one is read from the place of the JSON file, or from the page for JSON written in it.
The payload of a platform video
https://videobana.vercel.app/api/public/videos/<id> and https://videobana.vercel.app/api/examples/<id> answer { spec, timings, audioUrl }: the document, when each scene and sentence is spoken, and the address of the voice-over MP3 (or null). The tag recognises this shape (an object with an object spec and a timings key) and plays the recording with it; any other JSON is read as a video document. A video is public only after its owner made it so (the Share dialog, or PUT /api/videos/{id}/visibility with { "visibility": "public" }); a private or unknown id answers 404, and the tag shows the error.
Options
Everything is an attribute, so it works in plain HTML, and a property of the same name, so it works from code. Yes/no options follow the HTML way: loop alone is yes, and loop="false" (or 0) is no. The last form is the only way to switch off something that is on by default, such as autoplay.
| Attribute | Property | Default | What it does |
|---|---|---|---|
src | src | – | The address of the video. A video of the platform, one of the demos, or any JSON file. |
spec | spec | – | The video itself, as JSON text. The property also takes an object. It wins over src. |
captions | captions | off | Show the subtitles. The viewer can still switch them with the button. |
autoplay | autoplay | on | Start by itself when it comes into view. autoplay="false" shows the title card with a Watch button. |
loop | loop | off | Start again when it ends. |
muted | muted | off | Start without sound. The viewer can turn it on. |
volume | volume | 1 | From 0 to 1. An iPhone or iPad ignores it (the buttons of the device set the volume there); muted still works. |
playback-rate | playbackRate | 1 | Speed, from 0.25 to 4. |
controls | controls | all | The control bar: none, or a list of its parts. See below. |
fit | – (attribute only) | width | How the picture fills the element: width, contain or cover. See below. |
aspect | – (attribute only) | the video's | Show the video in another shape: 16:9, 9:16, 1:1, 4:5 and the others the platform exports. |
Exactly how the values are read
- Yes/no attributes (
captions,autoplay,loop,muted): absent is the default; present with no value or any other value is yes;false,0,offorno(any case, spaces around ignored) is no. Defaults:captionsoff,autoplayon,loopoff,mutedoff. volume: a number from 0 to 1 (outside is clamped); empty or not a number means 1.playback-rate: a number from 0.25 to 4 (outside is clamped); empty or not a number means 1.fit:width,containorcover; anything else meanswidth.aspect:16:9,4:3,1:1,4:5,3:4or9:16; anything else is ignored and the video keeps the shape it was made for.controls: see below.- The element reacts to changes of
src,spec,captions,autoplay,loop,muted,volume,playback-rate,controls,fitandaspectat any time; the change applies to the video that is playing (a newsrcorspecstarts another video).
Controls
The bar is whole by default. controls="none" hides it (the video then plays by itself and a script can drive it), and a list keeps only some parts. The viewer's keyboard shortcuts follow the same list, and so does a click on the picture (it plays and pauses when play is there): space and K need play, the arrows need seek, M needs volume, F needs fullscreen, C needs captions.
| Part | What it is |
|---|---|
play | Play and pause. |
chapters | Previous and next scene. |
time | The time and the length. |
seek | The bar to jump around, with a mark for every scene. |
captions | The subtitles button. |
speed | The playback speed button. |
volume | Mute, and the volume slider. An iPhone or iPad shows only Mute: its own buttons set the volume. |
fullscreen | Full screen. Where the browser has none for a page (an iPhone), the player fills the browser window instead. |
<video-player src="…" captions controls="play time fullscreen"></video-player>- The list is separated by spaces or commas, in any case and any order (the bar keeps its own order:
play,chapters,time,seek,captions,speed,volumeandfullscreen). false,0,off,noneorhiddenin the list hides the whole bar.- Words that are not parts are ignored; a list with no part in it (a typo, an empty value, or a bare
controls) shows the whole bar, since a video nobody can pause is worse than too many buttons. - Without a
controlsattribute the bar is whole, except withfit="cover", which has no bar unless the attribute asks for one. - The shortcuts work while the player has the focus, and not with Ctrl, Cmd or Alt held (those stay the browser's). The left and right arrows jump back and forward.
The guide's demo of a reduced bar (add the script tag of the quick start):
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" autoplay="false" captions controls="play time fullscreen"></video-player>Autoplay and sound
Browsers refuse to start sound on a page that nobody has clicked yet. The tag tries to play with sound, and when the browser says no it plays the video muted and shows a button, *Sound is off · Tap to turn it on*. After the visitor has used the page (a click anywhere is enough) the next video starts with sound. If you want no sound at all, say so with muted: that is also what a background video should do.
With autoplay="false" the tag shows the title card of the video with a *Watch the video* button and its length, and starts on a click (or on play()).
Fit and size
The tag is a block that is as wide as its container. fit says what the picture does inside it:
| fit | The picture | Use it for |
|---|---|---|
width | is as wide as the element, and as tall as it needs, with the control bar under it. The element has no height of its own. | Articles, landing pages, documentation. |
contain | fits whole inside the height you give the element, with bars where the shapes differ. | A fixed slot: a sidebar, a modal, a card. |
cover | fills the element and is cropped where the shapes differ. No control bar unless you ask for one. | Hero sections and backgrounds. |
With contain and cover the element is 16:9 until you give it a height (height, min-height or a grid or flex layout that sizes it). aspect shows a video in another shape than it was made for.
As a hero section
Whatever you write between the tags is put over the picture, so the tag can be the whole hero: the video fills it, and your headline and buttons stay your own HTML (indexable, translatable, styled by your CSS). Put the text in a panel or a gradient that keeps it readable over every scene.
<script src="https://videobana.vercel.app/widget.js" async></script>
<style>
.hero { height: 70vh; min-height: 24rem; }
.hero-copy { align-self: center; max-width: 34rem; margin-left: 6vw; color: #fff; }
.hero-copy h1 { font-size: clamp(2rem, 5vw, 3.5rem); line-height: 1.05; }
</style>
<video-player class="hero" src="https://videobana.vercel.app/api/examples/ads-videobana" fit="cover" muted loop>
<div class="hero-copy">
<h1>Make videos from words.</h1>
<p>Describe a topic. Get a narrated, animated video.</p>
<a href="/signup">Start for free</a>
</div>
</video-player>The guide's demo of it (add the script tag of the quick start):
<video-player fit="cover" muted loop src="https://videobana.vercel.app/api/examples/ads-videobana" style="height:21rem">
<div style="align-self:center;max-width:24rem;margin-left:2rem;padding:1.25rem 1.5rem;border-radius:1rem;background:rgb(7 4 26 / 0.72);color:#fff;font-family:system-ui,sans-serif">
<h3 style="margin:0 0 .5rem;font-size:1.5rem;line-height:1.2">Make videos from words.</h3>
<p style="margin:0;font-size:.9rem;line-height:1.5;opacity:.85">This text is the page's own. The video plays behind it.</p>
</div>
</video-player>muted loopmakes it a background: it starts everywhere, even where sound is refused, and never ends. Leavemutedout for a hero whose voice-over you want heard; the sound button shows when the browser blocks it.- The slot is a grid that fills the element:
align-self,justify-selfandplace-selfon your child place it, and clicks go through to the video everywhere your content is not. - Only a video that is shown starts: the tag waits until it is near the viewport, so a hero below the fold costs nothing until it is scrolled to.
From JavaScript
The tag behaves like a <video>: the same names for the same things.
const player = document.querySelector("video-player");
player.addEventListener("ready", () => console.log("length:", player.duration, "seconds"));
player.addEventListener("ended", () => console.log("done"));
player.addEventListener("error", (event) => console.warn(event.detail.message));
document.querySelector("#watch").addEventListener("click", async () => {
player.muted = false; // a click is what lets a browser play sound
player.currentTime = 0;
await player.play();
});| Member | What it is |
|---|---|
play() / pause() | Start and pause. play() also loads the video, even when the element is not on screen. |
currentTime | Where the video is, in seconds. Set it to jump. |
duration, paused, ended | Read-only state. duration is NaN until the video has loaded. |
muted, volume, playbackRate, loop, autoplay, captions, controls | The options above, readable and writable. |
src, spec | Change the video. Setting either starts the new video over. |
error | Why the video cannot be shown, or null. |
| Event | When |
|---|---|
ready | The video has been loaded. duration is now known. |
play | It started or resumed, whoever caused it. |
pause | It was paused. |
ended | It reached the end (with loop it starts again, and ended still fires). |
volumechange | Muted or the volume changed, by the viewer or by your code. |
ratechange | The playback speed changed. |
error | The video could not be shown. event.detail.message says why. |
Exactly how the members and events behave
- The events are dispatched on the element and do not bubble: listen on the element itself (
player.addEventListener(…), Vue's@ended, Svelte'son:ended), not on a parent. readyfires each time a video has been loaded (again after a newsrcorspec).play,pause,ended,volumechangeandratechangerepeat those of the video's clock, whoever caused them.erroris aCustomEventwhosedetail.messagesays why; it fires when the reason changes to a new one (a bad address, CORS, a video that is not public, a document that fails the checks, a player file that could not be loaded, a scene that could not be drawn).player.errorholds the same text, ornull.play()returns a promise that resolves at once: it does not wait for the video, and it does not reject when the browser refuses sound (the video then plays muted, with the sound button). Called before the element is near the viewport, it loads the player and starts the video anyway.- Until the video has loaded,
currentTimereads 0 and setting it does nothing,durationisNaN,pausedistrueandendedisfalse. - Once it has loaded,
muted,volumeandplaybackRateread the player's live state (a viewer's change shows), andcaptionsreads what the viewer's button last chose, until the page sets the attribute again. - Setting a yes/no property writes its attribute: it is removed when the value is the default, empty for yes, and
"false"for no. A string is read as an attribute value is ("false"and"0"are no), which is what frameworks that set properties from markup pass. - Setting
volumeorplaybackRateclamps the value and writes thevolumeorplayback-rateattribute;controls = nullremoves the attribute. srcreads""when there is none.specreads the object or text you set, else thespecattribute.fitandaspecthave no properties: set the attributes (player.setAttribute("fit", "cover")).
Styling
The player lives in a shadow root, so your page's CSS cannot break it and its CSS cannot touch your page. You size and place the tag like any block, and two variables restyle it:
video-player {
--vp-radius: 20px; /* corners (12px when it is as wide as the page, square for contain and cover) */
--vp-letterbox: #0b0b12; /* the bars of fit="contain" and fit="cover" */
max-width: 960px;
margin-inline: auto;
}--vp-radius: the corners of the picture. Default 12px withfit="width", square (0) withcontainandcover.--vp-letterbox: the colour of the bars ofcontainandcover. Default black (#000).- The
hiddenattribute hides the element, as on any element. - The player's sizes are in pixels (the build turns
reminto pixels), so the root font size of your page cannot change how the controls look.
On your platform
The script goes in once, the tag goes where the video should be. In a framework, tell the compiler that video-player is a custom element (React's JSX typing, Vue's isCustomElement, Angular's CUSTOM_ELEMENTS_SCHEMA), and write autoplay="false" as a string where you want it off.
HTML
<!doctype html>
<html>
<body>
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana"></video-player>
<!-- once per page, anywhere -->
<script src="https://videobana.vercel.app/widget.js" async></script>
</body>
</html>React
// index.html: <script src="https://videobana.vercel.app/widget.js" async></script>
// Tell TypeScript about the element (a .d.ts file):
declare module "react" {
namespace JSX {
interface IntrinsicElements {
"video-player": React.HTMLAttributes<HTMLElement> & {
src?: string;
spec?: string;
captions?: boolean | string;
autoplay?: string; // "false" switches it off
loop?: boolean | string;
muted?: boolean | string;
fit?: "width" | "contain" | "cover";
controls?: string;
"playback-rate"?: number | string;
};
}
}
}
export function Demo() {
// Write "false" as a string: in markup a missing attribute cannot switch off what is on by default.
return <video-player src="https://videobana.vercel.app/api/examples/ads-videobana" autoplay="false" captions />;
}Next.js
import Script from "next/script";
// Put the script in the layout, or in the page that shows the video.
export default function Page() {
return (
<>
<Script src="https://videobana.vercel.app/widget.js" strategy="afterInteractive" />
{/* A server component can render the element: it does nothing until the script has loaded. */}
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" fit="cover" muted loop />
</>
);
}
// Add the JSX typing from the React tab to a .d.ts file in your project.Vue
// index.html: <script src="https://videobana.vercel.app/widget.js" async></script>
// vite.config.ts: tell Vue that video-player is not a Vue component
export default defineConfig({
plugins: [vue({ template: { compilerOptions: { isCustomElement: (tag) => tag === "video-player" } } })],
});
<!-- Any component -->
<template>
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" captions @ended="onEnded" />
</template>Svelte
<!-- src/app.html, before </body> -->
<script src="https://videobana.vercel.app/widget.js" async></script>
<!-- Any component. Svelte passes attributes through to custom elements. -->
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" captions on:ended={() => (done = true)}></video-player>Angular
// index.html: <script src="https://videobana.vercel.app/widget.js" async></script>
// The component (or module) that uses the element:
@Component({
selector: "app-hero",
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" fit="cover" muted loop></video-player>`,
})
export class HeroComponent {}Astro
---
// src/pages/index.astro
---
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" fit="cover" muted loop></video-player>
<!-- is:inline keeps Astro from bundling the script -->
<script is:inline src="https://videobana.vercel.app/widget.js" async></script>WordPress
<!-- The simple way: a "Custom HTML" block with both lines -->
<script src="https://videobana.vercel.app/widget.js" async></script>
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana"></video-player>
// If your editor strips <script>, load it from the theme (functions.php)
// and keep only the element in the block:
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('videobana-player', 'https://videobana.vercel.app/widget.js', [], null, ['strategy' => 'async']);
});Webflow, Wix, Shopify…
1. Site settings → Custom code → "Footer" (Shopify: theme.liquid, before </body>):
<script src="https://videobana.vercel.app/widget.js" async></script>
2. On the page, add an Embed / Custom code / Custom Liquid block and paste the element:
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana"></video-player>
Tools that do not run scripts at all (Notion, Medium, e-mail) cannot show the element.
Use the iframe or the link from the Share dialog there.Good to know
- Other sites need permission to read your JSON. The platform's script, public videos and examples answer every origin. If you host a JSON file yourself on another domain, it must send
Access-Control-Allow-Origin. - Content-Security-Policy. Allow
https://videobana.vercel.appinscript-src(the widget loads a second file from the same place), inconnect-src(the video), and inimg-srcandmedia-src(pictures and the voice-over). The player's own styles are applied without<style>elements, sostyle-srcneeds nothing for them. - Fonts come from Google Fonts, loaded when a video needs them (the families a video uses, no others). They need
style-src https://fonts.googleapis.comandfont-src https://fonts.gstatic.com; a policy that blocks them gets system fonts instead. - Browsers: current Chrome, Edge, Firefox and Safari (custom elements, shadow DOM and ES2022).
- The script is cached, the player is cached for good. Its file name carries a hash of its content, so a new release is picked up by the next load of
https://videobana.vercel.app/widget.jswithout any change on your side. Load the script from the platform's address; do not copy it.
Nothing appears?
Open the browser console. A video that cannot be shown says why in its frame and in an error event: an address that is wrong or blocked (CORS), a video that is not public, or a document that fails the checks. The script tag <script src="https://videobana.vercel.app/widget.js" async></script> must be on the same page, and the tag must be <video-player> with a dash.
An example to start from: https://videobana.vercel.app/api/examples/ads-videobana opens as JSON and is the payload the tag reads. To embed in a place that cannot run scripts (Notion, Medium, e-mail), use the iframe from the Share dialog instead (https://videobana.vercel.app/embed/<id> for a public video; see the HTTP API for the embed and oEmbed addresses).
The guide's demos
The markup of the live demos on the web component guide, with the example's full address. Each needs the script tag of the quick start on the page.
Quick start, without autoplay
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" autoplay="false"></video-player>Reduced control bar with captions
<video-player src="https://videobana.vercel.app/api/examples/ads-videobana" autoplay="false" captions controls="play time fullscreen"></video-player>Hero with the page's own text over the video
<video-player fit="cover" muted loop src="https://videobana.vercel.app/api/examples/ads-videobana" style="height:21rem">
<div style="align-self:center;max-width:24rem;margin-left:2rem;padding:1.25rem 1.5rem;border-radius:1rem;background:rgb(7 4 26 / 0.72);color:#fff;font-family:system-ui,sans-serif">
<h3 style="margin:0 0 .5rem;font-size:1.5rem;line-height:1.2">Make videos from words.</h3>
<p style="margin:0;font-size:.9rem;line-height:1.5;opacity:.85">This text is the page's own. The video plays behind it.</p>
</div>
</video-player>Details from the widget's source
Not on the guide; read from the code of the widget. Rely on them when you script the element or debug a page.
- What the tag plays, in this order of precedence: the
specproperty, thespecattribute,src, then the text of the first<script type="application/json">inside the element. Only the first that is set is used. Setting the same address or text again does nothing; an inline script added or changed after the element is picked up. - A payload's
timingsare used only when they have one entry per scene of itsspecand a length above zero, and itsaudioUrlonly whentimings.audiois notnull; otherwise the video plays without the recording, timed from its text. - Relative addresses inside the JSON (pictures, footage, clips, sound clips, music) are made absolute against the address of the JSON file (
src), or against the page's base address for JSON written in the page or set from code. A relativesrcis read against the page. - A document is checked by the same validator as the platform (
POST /api/validate); the frame shows the first problem asThe video is not valid: <path>: <message> (and N more). An address that cannot be fetched showsThe video could not be loaded from <url>. Is the address right, and does the site allow other sites to read it (CORS)?, and one that answers an error shows its status. https://videobana.vercel.app/widget.jsis a classic script (it may beasyncordefer). It definesvideo-playeronly when it is not defined yet, so loading it twice is harmless, and finds the player file next to itself (https://videobana.vercel.app/widget/player-<hash>.js), which it loads as an ES module withimport().- The player file is loaded once per page, when the first element comes within 200 pixels of the viewport (at once in a browser without
IntersectionObserver), or whenplay()is called. Until then the element shows a dark 16:9 placeholder, so the page does not jump. If the file cannot be loaded, the frame says "The video player could not be loaded.", anerrorevent says from where, and the next element tries again. - The player adds two things to the page itself, because a shadow root cannot hold them: its
@propertyrules (withadoptedStyleSheetson the document) and a<link rel="stylesheet">in<head>for the Google Fonts of the video (weights 400 to 900, and JetBrains Mono for code scenes). Its styles go in as constructed style sheets where the browser supports them, and as<style>elements only where it does not. - The shadow root is open (
player.shadowRoot), and the element setscolor-scheme: darkfor the player. - A video without a recorded voice-over is never refused by the browser, but its sound clips, music and sound effects are: until the visitor has used the page, such a video starts muted, with the sound button.
- When the video ends with
playin the bar, the player offers to watch it again. Withloopit starts over by itself.
Related endpoints
All public: no token is needed.
GET https://videobana.vercel.app/api/public/videos/<id>: a public video as a payload,{ spec, timings, audioUrl }, readable from any site (CORS). Private and unknown ids answer 404. The files it names (/api/public/videos/<id>/audio,/api/public/videos/<id>/assets/<assetId>) redirect to signed addresses in storage.GET https://videobana.vercel.app/api/examples/<id>: a demo as a payload, with its voice-over, readable from any site (CORS).GET https://videobana.vercel.app/api/exampleslists the examples as documents (call it from a server or the platform's own pages).GET https://videobana.vercel.app/api/oembed?url=https://videobana.vercel.app/watch/<id>: oEmbed for a public watch page, with an iframe ofhttps://videobana.vercel.app/embed/<id>.
To make a video public, store it and set its visibility with the HTTP API (PUT /api/videos/{id}/visibility, with an API token), or use the Share dialog of the dashboard. The videobana-http-api skill describes those endpoints.