Skip to content
FrameworkStyle

youtube-video

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/html @videojs/youtube-video
import '@videojs/html/media/youtube-video';

Or load it from the CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/@videojs/cdn@10.0.0-beta.32/media/youtube-video.js"></script>

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.

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

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

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

API Reference

Attributes

These attributes configure the embedded media adapter:

AttributeTypeDefaultDetails
autoplaybooleanfalse
controlsbooleanfalse
loopbooleanfalse
mutedbooleanfalse
playsinlinebooleantrue
posterstring''
preloadMediaPreloadType'metadata'
srcstring''

Properties

PropertyTypeDefaultDetails
autoplaybooleanfalse
bufferedTimeRangeLike
controlsbooleanfalse
currentSrcstring
currentTimenumber
defaultMutedbooleanfalse
durationnumber
endedboolean
engine{ playVideo(): void; pauseVideo(): void; seekTo(seconds: number, allowSeekAhead: boolean): void; mute(): void; unMute(): void; isMuted(): boolean; setVolume(volume: number): void; getVolume(): number; getDuration(): number; getCurrentTime(): number; getPlaybackRate(): number; setPlaybackRate(rate: number): void; getVideoLoadedFraction(): number; getPlayerState(): number; loadVideoById(options: { videoId: string; startSeconds?: number }): void; cueVideoById(options: { videoId: string; startSeconds?: number }): void; loadPlaylist(options: { list: string; listType?: string }): void; cuePlaylist(options: { list: string; listType?: string }): void; stopVideo(): void; getOption(module: string, option: string): unknown; setOption(module: string, option: string, value: unknown): void; addEventListener(type: string, listener: ((event: { data: number }) => void)): void; destroy(): void } | null
errorMediaError | null
isFullscreenboolean
loopbooleanfalse
mutedbooleanfalse
pausedboolean
playbackRatenumber
played{ length: number; start(index: number): number; end(index: number): number }
playsInlinebooleantrue
posterstring''
preloadMediaPreloadType'metadata'
readyStatenumber
seekableTimeRangeLike
seekingboolean
source{ src?: string; engine?: YouTubeSourceEngineConfig } | nullnull
srcstring''
textTracksTextTrackListLike
volumenumber

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.

const video = document.querySelector('youtube-video');
video.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

Methods

Supports these media methods: afterLoadbeginLoadbindPlayerEventscreatePlayercreatePlayerApiexitFullscreenisStaleloadonErroronLoadedonPlayerReadypauseplaypollrequestFullscreenresetStatesetupTextTrackssnapshotPropsstartPollingstopPollingsyncTextTracksteardownTextTracks

Events

Implements these standard media events through the embedded player: durationchangeemptiedendederrorloadedmetadataloadstartpauseplayplayingprogressratechangeseekedseekingtimeupdatevolumechangewaiting

Also emits these Video.js-specific events:

EventDescription
loadcomplete
sourcechange