Command palette

@yasinsensoy/web

Web · Components

VideoPlayer

Video player matching the library's design.

10 exports

Live example

Preview

0:00 / 0:00

Code

import { VideoPlayer } from "@yasinsensoy/web";

<VideoPlayer
  src="/videos/intro.mp4"
  poster="/videos/intro.jpg"
  title="Intro"
  tracks={[{ src: "/videos/tr.vtt", srcLang: "tr", label: "Turkish", default: true }]}
/>

Events

  1. Events appear here when you interact with the preview.

Reels style: muted, looping, pauses on tap

Import

import {
  VideoFrameImage,
  VideoPlayer,
  cachedVideoFrame,
  extractVideoFrame,
  useVideoFrame,
} from "@yasinsensoy/web";

VideoPlayer

componentClient component

Video player matching the library's design. Uses the browser's native <video> element (hardware decoding, battery friendly); only the controls are ours and look the same in every browser.

  • Controls: Play/pause, a position bar showing the buffer (time bubble on hover), volume, duration, subtitle, speed and quality menu, picture-in-picture, full screen. While playing they hide when the mouse stops; in full screen the cursor hides too.
  • Keyboard: Space/K play-pause, ←/→ seekStep seconds, J/L twice that, ↑/↓ volume, M mute, F full screen, C subtitles, 0–9 percent of the video, </> speed, Home/End start/end.
  • Mobile: Tapping shows the controls; double tapping right or left seeks forward/back (like YouTube). Entering full screen locks a landscape video to landscape. On iPhone full screen opens with the system player.
  • Full screen: The player itself goes full screen; controls, title and menus work the same.
  • Quality: Sources in the src array with a label become the quality menu; the position is kept on switch.
  • The volume is remembered; the title and cover show on the lock screen (Media Session).

Videos in the gallery open with this player in the full screen view (see Gallery).

Client component.

VideoPlayer({ src, poster: posterProp, autoPoster, title, tracks, autoPlay, muted: initialMuted, loop, preload, controls, aspectRatio, fit, playbackRates, seekStep, startTime, rememberVolume, label, onPlay, onPause, onEnded, onTimeUpdate, onError, videoRef, className, style, onKeyDown, ref, ...props }: VideoPlayerProps): JSX.Element

Examples

<VideoPlayer src="/videos/intro.mp4" poster="/videos/intro.jpg" title="Intro" />

<VideoPlayer
  src={[
    { src: "/v/1080.mp4", label: "1080p" },
    { src: "/v/720.mp4", label: "720p" },
  ]}
  tracks={[{ src: "/v/en.vtt", srcLang: "en", label: "English", default: true }]}
/>

// Reels style, muted and looping
<VideoPlayer src={url} controls="minimal" autoPlay muted loop aspectRatio={9 / 16} fit="cover" />

VideoFrameImage

componentClient component

Shows the first frame of a video without a cover image as an image (covers the box, absolute inset-0). Empty until the frame is extracted; if it can't be converted to an image, the video is shown paused on the first frame.

VideoFrameImage({ src, fit, onFrame, className, ...props }: VideoFrameImageProps): JSX.Element

Examples

<div className="relative aspect-video">
  <VideoFrameImage src="/videos/clip.mp4" />
</div>

The extracted frame if cached; otherwise undefined.

cachedVideoFrame(src: string): VideoFrame | undefined

Extracts the first frame of a video (see the top of the file). A single job runs per URL; the result is cached. When signal is aborted and nobody else is waiting, the job leaves the queue or is stopped. null if the video can't be opened.

extractVideoFrame(src: string, { signal }?: { signal?: AbortSignal; }): Promise<VideoFrame | null>

Examples

const frame = await extractVideoFrame("/videos/clip.mp4");
if (frame?.url) image.src = frame.url;

First frame of a video without a cover image. No work starts while enabled is false. If target is given (or is still null) work starts only when that element comes on screen (within 200 px); when it leaves the screen the pending work is cancelled.

useVideoFrame(src: string | undefined, { enabled, target }?: { enabled?: boolean; target?: Element | null; }): {
    frame: VideoFrame | undefined

VideoFrame

interface

Result of extractVideoFrame.

NameTypeDescription
urlstring | undefinedImage URL of the frame (blob:). undefined if the frame couldn't be converted to an image (CORS); then the video should be shown paused on the first frame.
widthnumberIntrinsic width of the video (px).
heightnumberIntrinsic height of the video (px).

Props of VideoFrameImage.

Extends: Omit<ComponentProps<"span">, "children">

NameTypeDefaultDescription
srcstringVideo address.
fit?"cover" | "contain""cover"object-fit.
onFrame?(frame: VideoFrame) => voidWhen the frame is extracted (to measure the aspect ratio).

VideoSource

interface

A video source for VideoPlayer.

NameTypeDescription
srcstringVideo address.
type?stringMIME type (video/webm); the browser picks the first source it can play.
label?stringQuality name ("1080p", "720p"). If one of the sources has a name, a quality menu appears in settings; position and playback state are kept on switch. The first source is the default.

VideoTrack

interface

Subtitle file (WebVTT) for VideoPlayer.

NameTypeDefaultDescription
srcstringURL of the .vtt file.
srcLangstringLanguage code ("tr", "en").
labelstringName shown in the menu ("English").
kind?"subtitles" | "captions""subtitles"Kind.
default?booleanOn at start.

Props of the VideoPlayer component.

Extends: Omit<ComponentProps<"div">, "children" | "onPlay" | "onPause" | "onEnded" | "onError" | "onTimeUpdate">

NameTypeDefaultDescription
srcstring | readonly VideoSource[]Video URL or source list. See VideoSource.
poster?stringCover image shown before the video starts.
autoPoster?booleantrueIf poster is omitted, the first frame of the video is extracted in the browser and used as the cover (only the start of the video is downloaded; the result is cached). See extractVideoFrame.
title?stringTitle. Shown at the top in full screen; used by the player on the lock screen and in the notification area (Media Session).
tracks?readonly VideoTrack[]Subtitles. See VideoTrack.
autoPlay?booleanPlays by itself when visible. Browsers may block autoplay with sound; if blocked, the video is muted and played.
muted?booleanStarts muted.
loop?booleanStarts over when it ends.
preload?"none" | "metadata" | "auto""metadata"How much is downloaded when the page opens. "metadata" only the duration and first frame, "auto" enough to be ready to play.
controls?boolean | "minimal"trueControls: true the full bar; "minimal" Reels/Shorts style (tap to pause/play, a thin progress bar and mute button at the bottom); false none.
aspectRatio?number | "auto" | "fill"16 / 9Aspect ratio. A number (16 / 9), "auto" (the video's own ratio) or "fill" (fills the container; you give the size).
fit?"contain" | "cover""contain"How the picture fits the box.
playbackRates?readonly number[][0.5, 0.75, 1, 1.25, 1.5, 2]Speeds in settings.
seekStep?number5Time skipped with arrow keys (seconds). A double tap skips twice that.
startTime?numberStart position (seconds).
rememberVolume?booleantrueRemember the volume in the browser; all players on the same site open with the last chosen volume.
label?stringtitleScreen reader name of the player.
onPlay?() => voidWhen playback starts.
onPause?() => voidOn pause.
onEnded?() => voidOn end.
onTimeUpdate?(time: number, duration: number) => voidWhen the position changes (a few times per second), with the current position and duration.
onError?() => voidWhen the video can't be opened.
videoRef?Ref<HTMLVideoElement>The underlying <video> element; to control playback from code.