Questions tagged [html5-video]

HTML5 video is an element introduced in the HTML5 draft specification for the purpose of creating a standards-compliant, plugin-free way for playing videos and movies, partially replacing the object element.

HTML5 video is an element introduced in the HTML5 draft specification for the purpose of creating a standards-compliant, plugin-free way for playing videos and movies, partially replacing the object element.

Basic usage

You can embed a video element into a HTML5 document by using the following code:

<video width="320" height="240" controls="controls">
   <source src="movie.ogg" type="video/ogg">
   <source src="movie.mp4" type="video/mp4">
   <source src="movie.webm" type="video/webm">
   <p>Your browser does not support the video tag.</p>
</video>

Attributes

The video element has the following specific attributes:

  • src gives the address of the media resource to show
  • crossorigin is a CORS settings attribute
  • postergives the address of an image file that the user agent can show while no video data is available
  • preload sets the preload behavior, valid values being none, metadata and auto
  • autoplay is a boolean attribute. When present, the user agent will automatically begin playback of the video as soon as it can do so without stopping
  • mediagroup can be used to link multiple media elements together by implicitly creating a MediaController
  • loop is a boolean attribute that, if specified, indicates that the media element is to seek back to the start of the media resource upon reaching the end
  • muted is a boolean attribute that controls the default state of the audio output of the media resource, potentially overriding user preferences
  • controls is a boolean attribute that, if present, indicates that the author has not provided a scripted controller and would like the user agent to provide its own set of controls
  • width & height specify the dimensions of the element in pixels

JavaScript API & Events

The HTML5 video element can be easily controlled from JavaScript.

For example you can easily pause and play a video by doing the following:

document.getElementsByTagName('video')[0].play();
document.getElementsByTagName('video')[0].pause();

It also has properties:

var time = document.getElementsByTagName('video')[0].currentTime; //will return the current position of the playhead

And it also fires events:

document.getElementsByTagName('video')[0].addEventListener('ended',function(){
   alert('That\'s all folks!');
},false);

For a wonderful overview refer to the demo page of the W3.

Media Encoding

Browser vendors all support different codec and container formats, so you should encode your video more than once and serve several files simultaneously to ensure your visitor will be able to watch them. A common approach (at the time of writing, i.e. spring 2012) is to serve the following:

  • Make one version that uses WebM (VP8 + Vorbis)
  • Make another version that uses H.264 baseline video and AAC “low complexity” audio in an MP4 container
  • Make another version that uses Theora video and Vorbis audio in an Ogg container

Be aware that this is CONSTANTLY CHANGING, so before you set up your video do research on what is currently supported and what the future outlook does look like!

Flash & Download Fallback

Since video is still unsupported in many browsers used (IE 7+8 for example) it should be considered a good idea to serve Flash Video as a fallback. For browsers that cannot play any video at all you can also serve a link to a simple download.

This can be done by adding the traditional object style Flash content into the fallback part of the video element:

<video controls poster="video.jpg" width="854" height="480">
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  <source src="video.ogg" type="video/ogg">
  <object type="application/x-shockwave-flash" data="player.swf" width="854" height="504">
    <param name="allowfullscreen" value="true">
    <param name="allowscriptaccess" value="always">
    <param name="flashvars" value="file=video.mp4">
    <!--[if IE]><param name="movie" value="player.swf"><![endif]-->
    <img src="video.jpg" width="854" height="480" alt="Video">
    <p>Your browser can’t play HTML5 video. <a href="video.webm"> Download it</a> instead.</p>
  </object>
</video>

FAQ (on StackOverflow)

Available libraries

There are many JavaScript libraries that offer tools to handle tasks (and fix cross-browser issues) that you will encounter when using the video-element. These are a few (if you know anything else feel free to add them):

Tools for encoding video

Further reading

Attributions & Notes

This tag-wiki was compiled with the help and contents of Dive into HTML 5, dev.opera.com, w3.org and the MDN. In case anyone has vast experience regarding HTML5 video and mobile devices I think this would be a great addition to this wiki page!

7933 questions
299
votes
7 answers

Detect when an HTML5 video finishes

How do you detect when a HTML5
Brent
  • 23,354
  • 10
  • 44
  • 49
271
votes
20 answers

How to handle "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first." on Desktop with Chrome 66?

I'm getting the error message.. Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. ..when trying to play video on desktop using Chrome version 66. I did find an ad that began playback…
Steven
  • 3,526
  • 3
  • 18
  • 28
252
votes
17 answers

Stop/Close webcam stream which is opened by navigator.mediaDevices.getUserMedia

I opened a webcam by using the following JavaScript code: const stream = await navigator.mediaDevices.getUserMedia({ /* ... */ }); Is there any JavaScript code to stop or close the webcam?
Shih-En Chou
  • 4,007
  • 6
  • 20
  • 27
239
votes
18 answers

Play/pause HTML 5 video using JQuery

I am trying to control HTML5 videos using JQuery. I have two clips in a tabbed interface, there are six tabs in total, the others just have images. I am trying to make the video clips play when their tab is clicked and then stop when any of the…
Barny83
  • 2,395
  • 2
  • 14
  • 4
232
votes
14 answers

How to change the playing speed of videos in HTML5?

How to change the video play speed in HTML5? I've checked video tag's attributes in w3school but couldn't approach that.
Young
  • 7,986
  • 7
  • 43
  • 64
230
votes
8 answers

In Chrome 55, prevent showing Download button for HTML 5 video

I am getting this download button with
Muhammad Zeeshan
  • 4,591
  • 3
  • 11
  • 20
229
votes
28 answers

Prevent HTML5 video from being downloaded (right-click saved)?

How can I disable "Save Video As..." from a browser's right-click menu to prevent clients from downloading a video? Are there more complete solutions that prevent the client from accessing a file path directly?
python
  • 2,677
  • 3
  • 17
  • 15
203
votes
4 answers

Correct mime type for .mp4

I have two applications as mentioned below: Admin application through which I am able to upload a .mp4 file to the server. I am trying to download the .mp4 using mobile application in iPad. The Admin application is made by using asp.net 4.0 and…
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
192
votes
8 answers

How to set the thumbnail image on HTML5 video?

Is there a way to set thumbnail image on HTML5 video? I want to see some pictures before play. My code looks like this:
167
votes
32 answers

HTML5 Video tag not working in Safari , iPhone and iPad

I am trying to create an html5 web page in which there is a small video like 13s , I converted the flash version of this video into 3 format : .ogv using fireFogg , .webm using firefogg also and .mp4 using HandBrake application the html script I…
Khaled Al Hage Ismail
  • 1,769
  • 2
  • 13
  • 9
159
votes
21 answers

Is there a way to make HTML5 video fullscreen?

Is there a way to play a video fullscreen using the HTML5
nicudotro
  • 7,129
  • 6
  • 25
  • 17
130
votes
7 answers

HTML5 Video Dimensions

I'm trying to get the dimensions of a video of which I'm overlaying onto a page with JavaScript, however it is returning the dimensions of the poster image instead of the actual video as it seems it's being calculated before the video is loaded.
Elliot
  • 2,199
  • 4
  • 21
  • 28
125
votes
8 answers

How to tell if a

I see that the MediaElement interface exposes attributes like paused, seeking, and ended. Missing from the list, however, is playing. I know there are playing events that fire when an element starts playing, and timeupdate events events periodically…
Evan Krall
  • 2,915
  • 3
  • 23
  • 20
125
votes
4 answers

Play infinitely looping video on-load in HTML5

I'm looking to place a video in an HTML5 page that will begin playing on page-load, and once completed, loop back to the beginning without a break. The video should also NOT have any controls associated with it, and either be compatible with all…
stefmikhail
  • 6,877
  • 13
  • 47
  • 61
120
votes
5 answers

Start HTML5 video at a particular position when loading?

I need HTML5 video to start at certain point. Let's say at time 50 seconds onward. I tried but its not working as expected. is there something i am doing wrong? Here is the code:
1
2 3
99 100