1.1.0 • Published 1 year ago

@ducanh2912/react-tuby v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React Tuby

Build Size Version

A React video player library with YouTube-like UI

Demo

https://react-tuby.vercel.app

Feature

  • 📹 HTML Video, M3U8 support
  • 🎛 Allow multiple qualities
  • 📱 Fully responsive
  • 🖥 Full screen cross browser support, even Safari on iPhone
  • 📖 Subtitles support
  • ⏰ Speed control
  • ⌨️ Keyboard shortcuts
  • ⚙️ Support server side rendering (Next.js)
  • 🛠 No extra dependencies
  • ✅ Automatically fallback to default <video /> if rendering fails on older browsers

Installation

npm i @ducanh2912/react-tuby
# or
# yarn add @ducanh2912/react-tuby
# or
# pnpm add @ducanh2912/react-tuby

Import

import { Player } from "@ducanh2912/react-tuby";
import "@ducanh2912/react-tuby/css/main.css";

Example player

<Player
  src={[
    {
      quality: "Full HD",
      url: "https://cdn.glitch.me/cbf2cfb4-aa52-4a1f-a73c-461eef3d38e8/1080.mp4",
    },
    {
      quality: 720,
      url: "https://cdn.glitch.me/cbf2cfb4-aa52-4a1f-a73c-461eef3d38e8/720.mp4",
    },
    {
      quality: 480,
      url: "https://cdn.glitch.me/cbf2cfb4-aa52-4a1f-a73c-461eef3d38e8/480.mp4",
    },
  ]}
  subtitles={[
    {
      lang: "en",
      language: "English",
      url: "https://cdn.jsdelivr.net/gh/naptestdev/video-examples@master/en.vtt",
    },
    {
      lang: "fr",
      language: "French",
      url: "https://cdn.jsdelivr.net/gh/naptestdev/video-examples@master/fr.vtt",
    },
  ]}
  poster="https://cdn.jsdelivr.net/gh/naptestdev/video-examples@master/poster.png"
/>

Props

proptypedescription
src{ quality: number | string; url: string; }[] | string;One video url or array of video urls and qualities
subtitles{ lang: string; language: string; url: string; }[]Array of subtitles, the first one will be the default. Subtitle must be in .vtt format.
dimensionsnumber | { width: number | string; height: number | string }Number: aspect ratio (height/width). Default is 56.25% (9/16) Width, height: set custom width and height
primaryColorstringCustomize the primary color. Default: #ff0000
posterstringThe url of poster image
seekDurationnumberSeek duration when pressing left/right key. Default: 10
playerKeystringUnique user key to store video state in localStorage
internationalizationstringSee internationalization section below
playerRefRefObject\<HTMLVideoElement>Use your own ref to control the video player
pictureInPicturebooleanShow picture in picture button
keyboardShortcutboolean | { pause?: boolean; rewind?: boolean; forward?: boolean; fullScreen?: boolean; mute?: boolean; subtitle?: boolean; }Customize keyboard shortcuts
preserve{ watchTime?: boolean; volume?: boolean; playbackSpeed?: boolean; }Choose which values to preserve in localStorage

Examples

One src only

<Player src="/your-video.mp4" />

Custom width, height

<Player src="/your-video.mp4" dimensions={{ width: "100%", height: "100%" }} />

Custom props for video

This library uses render props to allow user to set custom render component

<Player src="/your-video.mp4">
  {(ref, props) => <video ref={ref} {...props} autoPlay loop />}
</Player>

Custom event

<Player src="/your-video.mp4">
  {(ref, { onPause, ...others }) => (
    <video
      ref={ref}
      {...others}
      onPause={(e) => {
        // The library original event
        onPause && onPause(e);

        // Do something here
        console.log("Paused");
      }}
    />
  )}
</Player>

Usage with m3u8 / HLS

Install react-hls-player (I'd recommend my fork @ducanh2912/react-hls-player though, since that package isn't maintained anymore)

npm i react-hls-player
# or
# yarn add react-hls-player
# or
# pnpm add react-hls-player

Usage

import ReactHlsPlayer from "react-hls-player";

<Player src="/your-video.m3u8">
  {(ref, props) => <ReactHlsPlayer playerRef={ref} {...props} />}
</Player>;

Get video currentTime using custom ref

const ref = useRef(null);

useEffect(() => {
  ref.current?.addEventListener("timeupdate", () => {
    console.log(ref.current?.currentTime);
  });
}, []);

<Player playerRef={ref} src="/your-video.mp4" />;

Disable keyboard shortcuts

<Player src="/your-video.mp4" keyboardShortcut={false} />

// or

<Player
  src="/your-video.mp4"
  keyboardShortcut={{
    pause: false,
    forward: true,
    rewind: true,
    fullScreen: true,
    mute: true,
    subtitle: true,
  }}
/>

Internationalization

propertydefault
tooltipsPlayPlay (k)
tooltipsPausePause (k)
tooltipsMuteMute (m)
tooltipsUnmuteUnmute (m)
tooltipsSubtitlesSubtitles (c)
tooltipsSettingsSettings
tooltipsFullscreenFull Screen (f)
tooltipsExitFullscreenExit full screen (f)
settingsPlaybackSpeedPlayback Speed
settingsPlaybackSpeedNormalNormal
settingsSubtitlesSubtitles
settingsSubtitlesOffOff
settingsQualityQuality
settingsModalOffOK

Issues

If you encounter some bugs or have a feature request, feel free to submit an issue on the github repo.

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.3-beta.1

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0-beta.2

2 years ago

0.3.0-beta.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago