Skip to content

ReferenceMedia

YouTubeVideo

Video component that plays YouTube videos and playlists through the YouTube IFrame player

The YouTube IFrame player keeps its own chrome hidden by default, so it drops into a player skin like any other media component; set controls to use YouTube’s UI instead.

Import

pnpm add @videojs/youtube-video
import { YouTubeVideo } from '@videojs/react/media/youtube-video';

Load a source

src takes a YouTube URL or a raw 11-character video id. Watch, share (youtu.be), embed, Shorts, live, and privacy-enhanced (youtube-nocookie.com) URLs all work. Playlist URLs load through their list parameter, and a t parameter sets the start position.

<YouTubeVideo src="https://www.youtube.com/watch?v=aqz-KE-bpKQ" />

Behavior

A few things work differently from a native <video> element, because the IFrame API doesn’t expose them:

  • loop restarts playback when the video ends; the IFrame API has no single-video loop.
  • Fullscreen targets the iframe, so YouTube’s own chrome shows in fullscreen.
  • Picture-in-picture is unavailable, and the player reports it as unsupported.
  • Caption tracks appear in textTracks once playback starts; YouTube reports no caption metadata before then.

Examples

Basic Usage

import { YouTubeVideo } from '@videojs/react/media/youtube-video';

export default function BasicUsage() {
  return (
    <div className="youtube-video">
      <YouTubeVideo src="https://www.youtube.com/watch?v=aqz-KE-bpKQ" controls />
    </div>
  );
}

API Reference

Props

Accepts these Video.js-specific props:

PropTypeDefaultDetails
autoplaybooleanfalse
controlsbooleanfalse
defaultMutedbooleanfalse
loopbooleanfalse
mutedbooleanfalse
playsInlinebooleantrue
posterstring''
preloadMediaPreloadType'metadata'
sourceobjectnull
srcstring''

Engine options

source.engine.youtube

Pass YouTube player parameters under source.engine.youtube, spelled exactly as YouTube spells them. They’re serialized onto the embed URL untouched, and the embed reads them once, when its URL is built. Media Sources covers how engine options fit into a structured source.

autoplay, controls, and playsinline come from the props of the same name, so they have no engine.youtube spelling. Parameters YouTube has deprecated are absent as well: modestbranding, showinfo, autohide, theme, and listType: 'search'. Anything YouTube adds next passes through the same way.

<YouTubeVideo
  source={{
    src: 'https://www.youtube.com/watch?v=aqz-KE-bpKQ',
    engine: { youtube: { hl: 'de', cc_load_policy: 1 } },
  }}
/>
OptionTypeDetails
cc_lang_prefstring | undefined
cc_load_policy1 | undefined
color'red' | 'white' | undefined
disablekb0 | 1 | undefined
enablejsapi0 | 1 | undefined
endnumber | undefined
fs0 | 1 | undefined
hlstring | undefined
iv_load_policy1 | 3 | undefined
liststring | undefined
listType'playlist' | 'user_uploads' | undefined
loop0 | 1 | undefined
originstring | undefined
playliststring | undefined
referrerPolicyReferrerPolicy | undefined
rel0 | 1 | undefined
startnumber | undefined
widget_referrerstring | undefined

Refs

ref

Forwards to the rendered <iframe>, an HTMLIFrameElement. The iframe cannot play or seek; use mediaRef for playback.

const ref = useRef<HTMLIFrameElement>(null);

<YouTubeVideo ref={ref} />

mediaRef

Receives the YouTubeAdapter that drives the embed, which exposes media methods and properties such as play() and currentTime. It is set once the adapter has attached to the iframe, so its target is already that iframe, and it is cleared when the iframe unmounts.

import type { YouTubeAdapter } from '@videojs/youtube-video';

const mediaRef = useRef<YouTubeAdapter>(null);

<YouTubeVideo mediaRef={mediaRef} />

// In an event handler
mediaRef.current?.play();

Events

Handle standard media events with React event props such as onPlay and onTimeUpdate. The adapter dispatches them, not the iframe. For events without a React prop, call addEventListener on the adapter from mediaRef.