docs.rodeo

MDN Web Docs mirror

{{HTMLSidebar}} 

The <video> HTML element embeds a media player which supports video playback into the document. You can use <video> for audio content as well, but the {{HTMLElement("audio")}}  element may provide a more appropriate user experience.

{{EmbedInteractiveExample("pages/tabbed/video.html", "tabbed-standard")}} 

The above example shows simple usage of the <video> element. Similarly to the {{htmlelement("img")}}  element, we include a path to the media we want to display inside the src attribute; we can include other attributes to specify information such as video width and height, whether we want it to autoplay and loop, or to show the browser’s default video controls, etc.

The content inside the opening and closing <video></video> tags is shown as a fallback in browsers that don’t support the element.

Attributes

Like all other HTML elements, this element supports the global attributes.

Events

Event Name Fired When
`{{domxref("ScriptProcessorNode.audioprocess_event","audioprocess")}}`  `{{Deprecated_Inline}}`  The input buffer of a `{{DOMxRef("ScriptProcessorNode")}}`  is ready to be processed.
`{{domxref("HTMLMediaElement.canplay_event", 'canplay')}}`  The browser can play the media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content.
`{{domxref("HTMLMediaElement.canplaythrough_event", 'canplaythrough')}}`  The browser estimates it can play the media up to its end without stopping for content buffering.
`{{domxref("OfflineAudioContext.complete_event", "complete")}}`  The rendering of an `{{DOMxRef("OfflineAudioContext")}}`  is terminated.
`{{domxref("HTMLMediaElement.durationchange_event", 'durationchange')}}`  The duration attribute has been updated.
`{{domxref("HTMLMediaElement.emptied_event", 'emptied')}}`  The media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the load() method is called to reload it.
`{{domxref("HTMLMediaElement.ended_event", 'ended')}}`  Playback has stopped because the end of the media was reached.
`{{domxref("HTMLMediaElement.error_event", 'error')}}`  An error occurred while fetching the media data, or the type of the resource is not a supported media format.
`{{domxref("HTMLMediaElement.loadeddata_event", 'loadeddata')}}`  The first frame of the media has finished loading.
`{{domxref("HTMLMediaElement.loadedmetadata_event", 'loadedmetadata')}}`  The metadata has been loaded.
`{{domxref("HTMLMediaElement.loadstart_event", 'loadstart')}}`  Fired when the browser has started to load the resource.
`{{domxref("HTMLMediaElement.pause_event", 'pause')}}`  Playback has been paused.
`{{domxref("HTMLMediaElement.play_event", 'play')}}`  Playback has begun.
`{{domxref("HTMLMediaElement.playing_event", 'playing')}}`  Playback is ready to start after having been paused or delayed due to lack of data.
`{{domxref("HTMLMediaElement.progress_event", 'progress')}}`  Fired periodically as the browser loads a resource.
`{{domxref("HTMLMediaElement.ratechange_event", 'ratechange')}}`  The playback rate has changed.
`{{domxref("HTMLMediaElement.seeked_event", 'seeked')}}`  A seek operation completed.
`{{domxref("HTMLMediaElement.seeking_event", 'seeking')}}`  A seek operation began.
`{{domxref("HTMLMediaElement.stalled_event", 'stalled')}}`  The user agent is trying to fetch media data, but data is unexpectedly not forthcoming.
`{{domxref("HTMLMediaElement.suspend_event", 'suspend')}}`  Media data loading has been suspended.
`{{domxref("HTMLMediaElement.timeupdate_event", 'timeupdate')}}`  The time indicated by the currentTime attribute has been updated.
`{{domxref("HTMLMediaElement.volumechange_event", 'volumechange')}}`  The volume has changed.
`{{domxref("HTMLMediaElement.waiting_event", 'waiting')}}`  Playback has stopped because of a temporary lack of data.

Usage notes

Browsers don’t all support the same video formats; you can provide multiple sources inside nested {{htmlelement("source")}}  elements, and the browser will then use the first one it understands.

<video controls>
  <source src="myVideo.webm" type="video/webm" />
  <source src="myVideo.mp4" type="video/mp4" />
  <p>
    Your browser doesn't support HTML video. Here is a
    <a href="myVideo.mp4" download="myVideo.mp4">link to the video</a> instead.
  </p>
</video>

We offer a substantive and thorough guide to media file types and the guide to the codecs supported for video. Also available is a guide to audio codecs that can be used with them.

Other usage notes:

A good general source of information on using HTML <video> is the HTML video and audio beginner’s tutorial.

Styling with CSS

The <video> element is a replaced element — its {{cssxref("display")}}  value is inline by default — but its default width and height in the viewport is defined by the video being embedded.

There are no special considerations for styling <video>; a common strategy is to give it a display value of block to make it easier to position, size, etc., and then provide styling and layout information as required. Video player styling basics provides some useful styling techniques.

Adding subtitles and other timed text tracks

Timed text tracks for subtitles, closed captions, chapter headings, and so on, can be added declaratively by nesting the {{HTMLElement("track")}}  element. The tracks are specified in Web Video Text Tracks File Format (WebVTT) (.vtt files).

For example, the HTML below includes the file “captions.vtt”, which will be used to overlay closed captions on the video if captions are enabled by the user.

<video controls src="video.webm">
  <track default kind="captions" src="captions.vtt" />
</video>

Timed text tracks can also be added programmatically using the WebVTT API.

Detecting track addition and removal

You can detect when tracks are added to and removed from a <video> element using the {{domxref("VideoTrackList/addtrack_event", "addtrack")}}  and {{domxref("VideoTrackList/removetrack_event", "removetrack")}}  events. However, these events aren’t sent directly to the <video> element itself. Instead, they’re sent to the track list object within the <video> element’s {{domxref("HTMLMediaElement")}}  that corresponds to the type of track that was added to the element:

For example, to detect when audio tracks are added to or removed from a <video> element, you can use code like this:

const elem = document.querySelector("video");

elem.audioTracks.onaddtrack = (event) => {
  trackEditor.addTrack(event.track);
};

elem.audioTracks.onremovetrack = (event) => {
  trackEditor.removeTrack(event.track);
};

This code watches for audio tracks to be added to and removed from the element, and calls a hypothetical function on a track editor to register and remove the track from the editor’s list of available tracks.

You can also use {{domxref("EventTarget.addEventListener", "addEventListener()")}}  to listen for the {{domxref("VideoTrackList/addtrack_event", "addtrack")}}  and {{domxref("VideoTrackList/removetrack_event", "removetrack")}}  events.

Server support for video

If the MIME type for the video is not set correctly on the server, the video may not show or show a gray box containing an X (if JavaScript is enabled).

If you use Apache Web Server to serve WebM videos, you can fix this problem by adding the video file type extensions to the video/webm MIME type (the most common WebM file extension is .webm). To do this, edit the mime.types file in /etc/apache or use the AddType configuration directive in httpd.conf:

AddType video/webm .webm

Your web host may provide an easy interface to MIME type configuration changes for new technologies until a global update naturally occurs.

Accessibility

Videos should provide both captions and transcripts that accurately describe their content (see Adding captions and subtitles to HTML video for more information on how to implement these). Captions allow people who are experiencing hearing loss to understand a video’s audio content as the video is being played, while transcripts allow people who need additional time to be able to review audio content at a pace and format that is comfortable for them.

It’s worth noting that while you can caption audio-only media, you can only do so when playing audio in a <video> element since the video region of the element is used to present the captions. This is one of the special scenarios in which it’s useful to play audio in a video element.

If automatic captioning services are used, it is important to review the generated content to ensure it accurately represents the source video.

In addition to spoken dialog, subtitles and transcripts should also identify music and sound effects that communicate important information. This includes emotion and tone:

14
00:03:14 --> 00:03:18
[Dramatic rock music]

15
00:03:19 --> 00:03:21
[whispering] What's that off in the distance?

16
00:03:22 --> 00:03:24
It's… it's a…

16 00:03:25 --> 00:03:32
[Loud thumping]
[Dishes clattering]

Captions should not obstruct the main subject of the video. They can be positioned using the align VTT cue setting.

Examples

Single source

This example plays a video when activated, providing the user with the browser’s default video controls to control playback.

HTML

<!-- Basic video example -->
<!-- 'Big Buck Bunny' licensed under CC 3.0 by the Blender foundation. Hosted by archive.org -->
<!-- Poster from peach.blender.org -->
<video
  controls
  src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
  poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"
  width="620">
  Sorry, your browser doesn't support embedded videos, but don't worry, you can
  <a href="https://archive.org/details/BigBuckBunny_124">download it</a>
  and watch it with your favorite video player!
</video>

Result

{{EmbedLiveSample('Single source', '', '400')}} 

Until the video starts playing, the image provided in the poster attribute is displayed in its place. If the browser doesn’t support video playback, it displays the fallback text.

Multiple sources

This example builds on the last one, offering three different sources for the media; this allows the video to be watched regardless of which video codecs are supported by the browser.

HTML

<!-- Using multiple sources as fallbacks for a video tag -->
<!-- 'Elephants Dream' by Orange Open Movie Project Studio, licensed under CC-3.0, hosted by archive.org -->
<!-- Poster hosted by Wikimedia -->
<video
  width="620"
  controls
  poster="https://upload.wikimedia.org/wikipedia/commons/e/e8/Elephants_Dream_s5_both.jpg">
  <source
    src="https://archive.org/download/ElephantsDream/ed_hd.avi"
    type="video/avi" />
  <source
    src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
    type="video/mp4" />

  Sorry, your browser doesn't support embedded videos, but don't worry, you can
  <a
    href="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
    download="ed_1024_512kb.mp4">
    download the MP4
  </a>
  and watch it with your favorite video player!
</video>

Result

{{EmbedLiveSample('Multiple sources', '', '400')}} 

First AVI is tried. If that can’t be played, MP4 is tried. A fallback message is displayed if the video element isn’t supported, but not if all sources fail.

Some media file types let you provide more specific information using the codecs parameter as part of the file’s type string. A relatively simple example is video/webm; codecs="vp8, vorbis", which says that the file is a WebM video using VP8 for its video and Vorbis for audio.

Technical summary

Content categories Flow content, phrasing content, embedded content. If it has a controls attribute: interactive content and palpable content.
Permitted content

If the element has a src attribute: zero or more `{{HTMLElement("track")}}`  elements, followed by transparent content that contains no media elements–that is no `{{HTMLElement("audio")}}`  or <video>.

Else: zero or more `{{HTMLElement("source")}}`  elements, followed by zero or more `{{HTMLElement("track")}}`  elements, followed by transparent content that contains no media elements–that is no `{{HTMLElement("audio")}}`  or <video>.

Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts embedded content.
Implicit ARIA role No corresponding role
Permitted ARIA roles application
DOM interface `{{domxref("HTMLVideoElement")}}` 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN