1.1.0 • Published 2 years ago

tplayer.js v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

tplayer logo

This project is made possible with Plyr, Hls.js, Dash.js.

Features 📑

  • HLS and DASH playback 🎥
  • Multi quality supported 🎬
  • Drm with custom header support (Widevine & Playready) 🔐
  • Customizable UI ⛏
  • Active development 🧱

Setup and Usage 📚

tplayer.js requires a video element in the DOM.

HTML

See example

  1. Put the tplayer.js script tag in the <head> of your HTML document.

    <script src="https://cdn.jsdelivr.net/npm/tplayer.js@1.1.0/dist/index.js"></script>
  2. Put the video element in the <body> of your HTML document.

    <video id="tplayer"></video>
  3. Add some JS to the <body> of your HTML document.

    <script>
      window.tplayer(options);
    </script>

NEXTJS

See example

You need to import tplayer.js into a component which will be dynamically imported (No SSR) to your root.

# Install tplayer.js
npm install tplayer.js
// component\player.jsx

import { useRef, useEffect } from "react";
import { tplayer, destroyPlayer } from "tplayer.js";

export default function Player({ config }) {
  const videoRef = useRef();

  useEffect(() => {
    tplayer({
      ...config,
      playerElem: videoRef.current,
    });

    return () => destroyPlayer({ id: config.id });
  }, []);

  return (
    <div>
      <video ref={videoRef}></video>
    </div>
  );
}
// pages\index.jsx

import dynamic from "next/dynamic";
const Player = dynamic(import("../component/player"), { ssr: false });

export default function Home() {
  return <Player config={options} />;
}

Options 📝

NameDescriptionDefault / Requirement / FallbackExample
idThis is the tplayer.js instance idDefault: tplayer'tplayerhtml'
playerElemVideo element from DOMRequireddocument.getElementById("tplayer")
sourceSource ObjectAt least one DASH or HLS URL is required{dash:'some.mpd', hls: 'some.m3u8'}
source.dashMPD URL of your source fileRequired if DRM enabledhttps://some.mpd
source.hlsM3U8 URL of your source fileNot required if Dash is providedhttps://some.m3u8
sourceHeaders.dashAdditional XHR headers for DashOptional{"some": "header"}
sourceHeaders.hlsAdditional XHR headers for hlsOptional{"some": "header"}
drmDRM ObjectOptional{widevine: {url: '', headers: {}}, playready: {url: '', headers: {}}}
drm.widevineWidevine ObjectOptionalwidevine: {url: '', headers: {}}
drm.widevine.urlWidevine license URLRequiredhttps://some/proxy
drm.widevine.headersHeaders object for license requestsOptional{"T-Header-One": "Hi", "T-Header-Two": "Hello"}
drm.playreadyPlayready ObjectOptionalplayready: {url: '', headers: {}}
drm.playready.urlPlayready license URLRequiredhttps://some.asmx
drm.playready.headersHeaders object for license requestsOptional{"T-Header-One": "Hi", "T-Header-Two": "Hello"}
uiExtended UI ObjectOptional{mainColor: '#ff002b'}
ui.mainColorMain colorOptional#ff002b
captionsWebVTT Captions arrayOptional{label: "", src: "", language: ""}
captionsindex.labelCaption labelOptional"English"
captionsindex.srcCaption source URLRequiredhttps://some.vtt
captionsindex.languageCaption languageOptional"en"

See this example

const options = {
  id: "tplayerhtml", // anything
  playerElem: document.getElementById("tplayer"), // grabbing the video element from the DOM
  source: {
    dash: "https://bitmovin-a.akamaihd.net/content/art-of-motion_drm/mpds/11331.mpd",
    hls: "https://bitmovin-a.akamaihd.net/content/art-of-motion_drm/m3u8s/11331.m3u8",
  },
  sourceHeaders: {
    dash: {
      // "T-Header": "You can send header like this",
    },
    hls: {
      // "T-Header": "You can send header like this",
    },
  },
  drm: {
    widevine: {
      url: "https://cwip-shaka-proxy.appspot.com/no_auth", // Widevine license URL
      headers: {
        // "T-Header": "You can send header like this",
      },
    },
    playready: {
      url: "https://playready.directtaps.net/pr/svc/rightsmanager.asmx?PlayRight=1&ContentKey=EAtsIJQPd5pFiRUrV9Layw==", // Playready license URL
      headers: {
        // "T-Header": "You can send header like this",
      },
    },
  },
  ui: {
    mainColor: "red",
  },
  captions: [
    {
      label: "English",
      src: "./test.vtt",
      language: "en",
    },
  ],
};

Methods 🔧

1. destroyPlayer({id: string})

It will destroy the player instance. Id will be same as the id of the player instance.

destroyPlayer({ id: "tplayer" });

2. getPlayer({id: string})

It will return the player instance. Id will be same as the id of the player instance. You can apply additional controls to the player instance.

let player = getPlayer({ id: "tplayer" });
player.pause();

Credits 💖

License 📝

tplayer.js is licensed under the MIT license

Crafted with 💖 by Tuhin Kanti Pal in 1 day.

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago