VideobanaBeta
AI skills

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

  1. Add the script, once per page: <script src="https://www.bana.video/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.
  2. Add the tag. Point src at a video. Here it is the ad for Videobana, one of the demos (ads-videobana).
html
<script src="https://www.bana.video/widget.js" async></script>

<video-player src="https://www.bana.video/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

html
<!-- A video of the platform, or any JSON file that your site or another one serves -->
<video-player src="https://www.bana.video/api/public/videos/0123456789abcdef"></video-player>
<video-player src="/videos/launch.json"></video-player>

JSON text

html
<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

html
<video-player>
  <script type="application/json">
    { "version": 1, "title": "Hello", "domain": "science", "scenes": [ /* … */ ] }
  </script>
</video-player>

JavaScript object

js
const player = document.querySelector("video-player");

player.spec = { version: 1, title: "Hello", domain: "science", scenes: [/* … */] }; // an object, or JSON text
player.src = "https://www.bana.video/api/examples/ads-videobana"; // or an address
Address or JSONWhat you get
https://www.bana.video/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://www.bana.video/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 textThe 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://www.bana.video/api/public/videos/<id> and https://www.bana.video/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.

AttributePropertyDefaultWhat it does
srcsrc–The address of the video. A video of the platform, one of the demos, or any JSON file.
specspec–The video itself, as JSON text. The property also takes an object. It wins over src.
captionscaptionsoffShow the subtitles. The viewer can still switch them with the button.
autoplayautoplayonStart by itself when it comes into view. autoplay="false" shows the title card with a Watch button.
looploopoffStart again when it ends.
mutedmutedoffStart without sound. The viewer can turn it on.
volumevolume1From 0 to 1. An iPhone or iPad ignores it (the buttons of the device set the volume there); muted still works.
playback-rateplaybackRate1Speed, from 0.25 to 4.
controlscontrolsallThe control bar: none, or a list of its parts. See below.
fit– (attribute only)widthHow the picture fills the element: width, contain or cover. See below.
aspect– (attribute only)the video'sShow 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, off or no (any case, spaces around ignored) is no. Defaults: captions off, autoplay on, loop off, muted off.
  • 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, contain or cover; anything else means width.
  • aspect: 16:9, 4:3, 1:1, 4:5, 3:4 or 9: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, fit and aspect at any time; the change applies to the video that is playing (a new src or spec starts 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.

PartWhat it is
playPlay and pause.
chaptersPrevious and next scene.
timeThe time and the length.
seekThe bar to jump around, with a mark for every scene.
captionsThe subtitles button.
speedThe playback speed button.
volumeMute, and the volume slider. An iPhone or iPad shows only Mute: its own buttons set the volume.
fullscreenFull screen. Where the browser has none for a page (an iPhone), the player fills the browser window instead.
html
<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, volume and fullscreen).
  • false, 0, off, none or hidden in 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 controls attribute the bar is whole, except with fit="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):

html
<video-player src="https://www.bana.video/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:

fitThe pictureUse it for
widthis 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.
containfits whole inside the height you give the element, with bars where the shapes differ.A fixed slot: a sidebar, a modal, a card.
coverfills 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.

html
<script src="https://www.bana.video/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://www.bana.video/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):

html
<video-player fit="cover" muted loop src="https://www.bana.video/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 loop makes it a background: it starts everywhere, even where sound is refused, and never ends. Leave muted out 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-self and place-self on 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.

js
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();
});
MemberWhat it is
play() / pause()Start and pause. play() also loads the video, even when the element is not on screen.
currentTimeWhere the video is, in seconds. Set it to jump.
duration, paused, endedRead-only state. duration is NaN until the video has loaded.
muted, volume, playbackRate, loop, autoplay, captions, controlsThe options above, readable and writable.
src, specChange the video. Setting either starts the new video over.
errorWhy the video cannot be shown, or null.
EventWhen
readyThe video has been loaded. duration is now known.
playIt started or resumed, whoever caused it.
pauseIt was paused.
endedIt reached the end (with loop it starts again, and ended still fires).
volumechangeMuted or the volume changed, by the viewer or by your code.
ratechangeThe playback speed changed.
errorThe 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's on:ended), not on a parent.
  • ready fires each time a video has been loaded (again after a new src or spec). play, pause, ended, volumechange and ratechange repeat those of the video's clock, whoever caused them.
  • error is a CustomEvent whose detail.message says 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.error holds the same text, or null.
  • 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, currentTime reads 0 and setting it does nothing, duration is NaN, paused is true and ended is false.
  • Once it has loaded, muted, volume and playbackRate read the player's live state (a viewer's change shows), and captions reads 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 volume or playbackRate clamps the value and writes the volume or playback-rate attribute; controls = null removes the attribute.
  • src reads "" when there is none. spec reads the object or text you set, else the spec attribute. fit and aspect have 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:

css
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 with fit="width", square (0) with contain and cover.
  • --vp-letterbox: the colour of the bars of contain and cover. Default black (#000).
  • The hidden attribute hides the element, as on any element.
  • The player's sizes are in pixels (the build turns rem into 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

html
<!doctype html>
<html>
  <body>
    <video-player src="https://www.bana.video/api/examples/ads-videobana"></video-player>

    <!-- once per page, anywhere -->
    <script src="https://www.bana.video/widget.js" async></script>
  </body>
</html>

React

tsx
// index.html: <script src="https://www.bana.video/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://www.bana.video/api/examples/ads-videobana" autoplay="false" captions />;
}

Next.js

tsx
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://www.bana.video/widget.js" strategy="afterInteractive" />
      {/* A server component can render the element: it does nothing until the script has loaded. */}
      <video-player src="https://www.bana.video/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://www.bana.video/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://www.bana.video/api/examples/ads-videobana" captions @ended="onEnded" />
</template>

Svelte

html
<!-- src/app.html, before </body> -->
<script src="https://www.bana.video/widget.js" async></script>

<!-- Any component. Svelte passes attributes through to custom elements. -->
<video-player src="https://www.bana.video/api/examples/ads-videobana" captions on:ended={() => (done = true)}></video-player>

Angular

ts
// index.html: <script src="https://www.bana.video/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://www.bana.video/api/examples/ads-videobana" fit="cover" muted loop></video-player>`,
})
export class HeroComponent {}

Astro

astro
---
// src/pages/index.astro
---
<video-player src="https://www.bana.video/api/examples/ads-videobana" fit="cover" muted loop></video-player>

<!-- is:inline keeps Astro from bundling the script -->
<script is:inline src="https://www.bana.video/widget.js" async></script>

WordPress

<!-- The simple way: a "Custom HTML" block with both lines -->
<script src="https://www.bana.video/widget.js" async></script>
<video-player src="https://www.bana.video/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://www.bana.video/widget.js', [], null, ['strategy' => 'async']);
});

Webflow, Wix, Shopify…

1. Site settings → Custom code → "Footer" (Shopify: theme.liquid, before </body>):
   <script src="https://www.bana.video/widget.js" async></script>

2. On the page, add an Embed / Custom code / Custom Liquid block and paste the element:
   <video-player src="https://www.bana.video/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://www.bana.video in script-src (the widget loads a second file from the same place), in connect-src (the video), and in img-src and media-src (pictures and the voice-over). The player's own styles are applied without <style> elements, so style-src needs 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.com and font-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://www.bana.video/widget.js without 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://www.bana.video/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://www.bana.video/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://www.bana.video/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

html
<video-player src="https://www.bana.video/api/examples/ads-videobana" autoplay="false"></video-player>

Reduced control bar with captions

html
<video-player src="https://www.bana.video/api/examples/ads-videobana" autoplay="false" captions controls="play time fullscreen"></video-player>

Hero with the page's own text over the video

html
<video-player fit="cover" muted loop src="https://www.bana.video/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 spec property, the spec attribute, 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 timings are used only when they have one entry per scene of its spec and a length above zero, and its audioUrl only when timings.audio is not null; 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 relative src is read against the page.
  • A document is checked by the same validator as the platform (POST /api/validate); the frame shows the first problem as The video is not valid: <path>: <message> (and N more). An address that cannot be fetched shows The 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://www.bana.video/widget.js is a classic script (it may be async or defer). It defines video-player only when it is not defined yet, so loading it twice is harmless, and finds the player file next to itself (https://www.bana.video/widget/player-<hash>.js), which it loads as an ES module with import().
  • 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 when play() 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.", an error event 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 @property rules (with adoptedStyleSheets on 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 sets color-scheme: dark for 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 play in the bar, the player offers to watch it again. With loop it starts over by itself.

All public: no token is needed.

  • GET https://www.bana.video/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://www.bana.video/api/examples/<id>: a demo as a payload, with its voice-over, readable from any site (CORS). GET https://www.bana.video/api/examples lists the examples as documents (call it from a server or the platform's own pages).
  • GET https://www.bana.video/api/oembed?url=https://www.bana.video/watch/<id>: oEmbed for a public watch page, with an iframe of https://www.bana.video/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.

Next skillWalkthroughs and tours with bana.js →