Media Elements

repository·main·Indexed 42 days ago

https://github.com/muxinc/media-elements

A collection of HTMLMediaElement compatible custom elements and add-ons that wrap video players, streaming protocols, and media services into reusable web components. Includes specialized elements such as <castable-video> for Google Cast, <cloudflare-video>, <dash-video> (dash.js), <hls-video> (hls.js), and <jwplayer-video>, as well as base classes like CustomVideoElement and CustomAudioElement for creating custom media extensions.

Tokens
41.1K
Snippets
145
Records
170
Agent score
53%

What's inside Media Elements

  1. Overview of media-elements packages

    main

    The media-elements repository provides a collection of custom HTML elements and add-ons that are compatible with the standard HTMLMediaElement API. These packages allow you to wrap various video players, streaming protocols, and media services into custom web components.

    Available Elements and Add-ons

    Core & Extensions:

    • custom-media-element: Extends native <audio> or <video> elements.
    • media-tracks: Polyfill for audio and video tracks with renditions.
    • castable-video: Adds casting capabilities to video elements.

    Streaming Protocol Wrappers:

    • <hls-video>: Uses hls.js.
    • <dash-video>: Uses dash.js.
    • <shaka-video>: Uses Shaka Player.

    Third-Party Player Wrappers:

    • <mux-player> / <mux-video>: Mux-specific video elements.
    • <youtube-video>: YouTube player.
    • <tiktok-video>: TikTok player.
    • <vimeo-video>: Vimeo player.
    • <videojs-video>: Video.js player.
    • <spotify-audio>: Spotify player.
    • <wistia-video>: Wistia player.
    • <jwplayer-video>: JW Player.
    • <twitch-video>: Twitch player.
    • <cloudflare-video>: Cloudflare Stream.
    • <peertube-video>: PeerTube player.
  2. What is media-tracks and how does it work?

    main

    The media-tracks library polyfills standard HTML <audio> and <video> elements to support multiple audio and video tracks, as well as renditions (quality levels).

    It acts as a bridge between two types of consumers:

    1. Media Engines (e.g., hls.js, shaka): These engines can use the standardized API to add tracks and renditions retrieved from a manifest.
    2. Media UI Implementations (e.g., media-chrome): These UI components consume the uniform API to render menus for selecting tracks (languages/subtitles) and renditions (video quality).

    Note on iOS: Manual rendition switching is not supported on iOS because it uses a native HLS implementation that cannot be overridden by this library.

  3. Configure transpilation for media-elements

    main

    The packages in this repository are distributed as ESNext and include modern JavaScript features like private class fields.

    Because they are not pre-transpiled (unlike media-chrome), you must configure your bundler (e.g., Webpack, Vite, Rollup) to include these packages in its transpilation step if you need to support older browsers.

    Example Requirement: If you need to support browsers like Safari < 14.1, ensure your build tool processes these packages and targets a compatible version (e.g., es2019).

  4. Extend native media elements with CustomMediaElement

    main

    The custom-media-element package provides base classes for creating custom HTML elements that extend native <audio> or <video> functionality. You can subclass CustomVideoElement or CustomAudioElement to override methods (like play()) or intercept property accessors (like src) while maintaining the native media interface.

    import { CustomVideoElement } from 'custom-media-element';
    
    class MyCustomVideoElement extends CustomVideoElement {
      constructor() {
        super();
      }
    
      // Override the play method.
      play() {
        return super.play()
      }
    
      // Override the src getter & setter.
      get src() {
        return super.src;
      }
    
      set src(src) {
        super.src = src;
      }
    }
    
    if (globalThis.customElements && !globalThis.customElements.get('my-custom-video')) {
      globalThis.customElements.define('my-custom-video', MyCustomVideoElement);
    }
    
    export default MyCustomVideoElement;
  5. Install and use `<castable-video>`

    main

    The CastableVideoElement extends the native HTMLVideoElement to add Google Cast functionality. You can use it by importing the module via a CDN or installing it via npm.

    To use the CDN version, include the following script in your HTML:

    <script type="module" src="https://cdn.jsdelivr.net/npm/castable-video/+esm"></script>

    Then, use the <castable-video> tag in your markup. You can control it using standard video methods like .play() and .pause(), or use the .remote.prompt() method to open the browser's casting menu.

    <script type="module" src="https://cdn.jsdelivr.net/npm/castable-video/+esm"></script>
    
    <castable-video
      id="castable"
      src="https://stream.mux.com/DS00Spx1CV902MCtPj5WknGlR102V5HFkDe/high.mp4"
    ></castable-video>
    
    <button onclick="castable.play()">Play</button>
    <button onclick="castable.pause()">Pause</button>
    <button id="castBtn" hidden onclick="castable.prompt()">Cast...</button>
    
    <script type="module">
      castable.remote.watchAvailability((available) => {
        castBtn.hidden = !available;
      });
    </script>
  6. Install and use `<videojs-video>`

    main

    <videojs-video> is a custom element for Video.js that provides an API matching the standard <video> (HTMLMediaElement) API. It is designed to integrate seamlessly with Media Chrome.

    Installation via NPM

    1. Install the package:
    npm install videojs-video-element
    1. Import it in your application's JavaScript entry point (e.g., src/App.js):
    import 'videojs-video-element';

    Installation via CDN

    You can load the element directly from JSDelivr without a build step:

    <script type="module" src="https://cdn.jsdelivr.net/npm/videojs-video-element@1.0/+esm"></script>

    Basic Usage

    Once registered, use the <videojs-video> tag in your HTML. You can use standard attributes like src and controls:

    <videojs-video controls src="https://stream.mux.com/O6LdRc0112FEJXH00bGsN9Q31yu5EIVHTgjTKRkKtEq1k/high.mp4"></videojs-video>
    <script type="module" src="https://cdn.jsdelivr.net/npm/videojs-video-element@1.0/+esm"></script>
    <videojs-video controls src="https://stream.mux.com/O6LdRc0112FEJXH00bGsN9Q31yu5EIVHTgjTKRkKtEq1k/high.mp4"></videojs-video>
  7. Install the `<vimeo-video>` custom element

    main

    You can use the <vimeo-video> custom element to embed a Vimeo player with an API that matches the standard <video> API. This allows for seamless integration with Media Chrome.

    Via NPM

    1. Install the package:
    npm install vimeo-video-element
    1. Import it in your application's entry point (e.g., src/App.js):
    import 'vimeo-video-element';

    Via CDN

    Alternatively, you can load the element directly from JSDelivr using a module script:

    <script type="module" src="https://cdn.jsdelivr.net/npm/vimeo-video-element@1.0/+esm"></script>
  8. Use `<youtube-video>` via CDN

    main

    To use the element directly in your HTML without a build step, load the module via a <script> tag. Ensure you use type="module" so the browser registers the custom element correctly. It is recommended to load this in the <head> for quick registration.

    <head>
      <script type="module" src="https://cdn.jsdelivr.net/npm/youtube-video-element@1"></script>
    </head>
    <body>
      <youtube-video controls src="https://www.youtube.com/watch?v=rubNgGj3pYo"></youtube-video>
    </body>