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
46
votes
5 answers

How to detect whether HTML5 video has paused for buffering?

I'm trying to test whether a video is choppy. I have noticed that the pause event is not triggered when the video pauses for buffering. What is the best way to detect whether the video has paused for buffering?
Supreet Totagi
  • 822
  • 3
  • 9
  • 12
46
votes
5 answers

How to take a snapshot of HTML5-JavaScript-based video player?

Actually, i have a HTML5 page with JavaScript function that allow me to play a wmv video file. I need to take a snapshot when the video is playing (with pause or without) and saved in any image format JPG or BMP whatever. Any help will…
Ahmed MEZRI
  • 514
  • 1
  • 5
  • 7
45
votes
19 answers

rounded corners on html5 video

Is there a way I can cut off the corners of my html5 video element using the CSS3 border-radius attribute? Check out this example. it's not working.
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
45
votes
5 answers

HTML5 Video - Percentage Loaded?

Does anyone know what event or property I need to query in order to get a percentage figure of the amount an HTML5 video has loaded? I want to draw a CSS styled "loaded" bar that's width represents this figure. Just like You Tube or any other video…
wilsonpage
  • 17,341
  • 23
  • 103
  • 147
45
votes
5 answers

Why Safari on iOS is not showing my HTML5 video poster?

I have this webpage: http://healthpad.net/dashboard/ It have 10
Prody
  • 5,182
  • 6
  • 44
  • 62
44
votes
3 answers

Can you display HTML5

How can you display HTML5
Yammi
  • 465
  • 1
  • 4
  • 6
44
votes
7 answers

Is it possible to check if the user has a camera and microphone and if the permissions have been granted with Javascript?

I would like to find out if the user's device has an attached camera and microphone, and if so, has permissions been granted to get the audio and video stream using Javascript. I want to make this check to be made across Chrome and Firefox at the…
Amal Antony
  • 6,477
  • 14
  • 53
  • 76
44
votes
7 answers

HTML5 video element request stay pending forever (on chrome)

I have a weird issue in Chrome. Each time I load a
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
43
votes
6 answers

Is it possible to play HTML5 video in reverse?

Can HTML5
Ish
  • 28,486
  • 11
  • 60
  • 77
43
votes
4 answers

H.265/HEVC web browser support

Are there any web browsers out there that can playback the H.265 / MPEG-4 HEVC codec in a html5 video element? On what platform or hardware? I heard rumours about HEVC support in Edge when hardware decoding is available. With current GPUs and CPUs…
Duvrai
  • 3,398
  • 3
  • 20
  • 21
43
votes
8 answers

VideoJS centered play button

I thought I'd share a piece of code that might come in handy to someone. This is a function that center's the play button inside the video-js player, it works for me. You just need to call it on the pause event and when the player is initialized and…
user1592579
42
votes
10 answers

scale HTML5 Video and break aspect ratio to fill whole site

I want to use a 4:3 video as a background on a site. However, setting the width and height to 100% doesn't work since the aspect ratio is kept intact, so the video doesn't fill the whole width of the site. Here is my HTML and CSS…
Roland
  • 1,908
  • 4
  • 21
  • 34
42
votes
3 answers

use video as background for div

I would like to use a video as background in CSS3. I know that there is no background-video property, but is it possible to do this behavior. Using a fullsize video-tag doesn't give the wanted result, cause there is content that need to be displayed…
Knerd
  • 1,892
  • 3
  • 28
  • 55
41
votes
10 answers

Why does setting currentTime of HTML5 video element reset time in Chrome?

I want to set the time position of a video in HTML5. The time should be set like this: function settime(){ var video = document.getElementById("video1"); console.log(video.currentTime); //----->output for example 15.3 video.currentTime =…
user2212461
  • 3,105
  • 8
  • 49
  • 87
41
votes
7 answers

HTML5 Video Autoplay not working correctly

I'm using this code: