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
116
votes
15 answers

Make HTML5 video poster be same size as video itself

Does anyone know how to resize the HTML5 video poster such that it fits the exact dimensions of the video itself? here's a jsfiddle which shows the problem: http://jsfiddle.net/zPacg/7/ here's that code: HTML:
107
votes
17 answers

Detect if HTML5 Video element is playing

I've looked through a couple of questions to find out if an HTML5 element is playing, but can't find the answer. I've looked at the W3 documentation and it has an event named "playing" but I can't seem to get it to work. This is my current code: var…
user1076821
106
votes
8 answers

Playing HTML5 video on fullscreen in android webview

Well, I've been searching few days already, how to display HTML5 video in full-screen mode on android WebView. I managed to play HTML5 videos on my webview. Problems are arising when displaying video in fullscreen mode. As I figured out, android has…
nbtk
  • 3,039
  • 4
  • 21
  • 19
101
votes
4 answers

Play local (hard-drive) video file with HTML5 video tag?

I want to achieve the following. The intent is that the user will be able to select a file from his/her hard drive. And the reason for not uploading is of course transmission costs and…
Chris
  • 2,572
  • 4
  • 21
  • 19
92
votes
8 answers

HTML5 live streaming

For school I need to set up an HTML5 live stream site. They have a flash stream-player they've been using but now they want it to use HTML5 instead. How can I do this? I tried using the video tag but I can't get it working. Below is the code I have.…
Bernhard
  • 1,455
  • 5
  • 19
  • 24
80
votes
6 answers

Playing m3u8 Files with HTML Video Tag

I am trying to use HTTP Live Streaming (HLS) to stream video to my computers and my iPhone. After reading through the Apple 'HTTP Live Streaming Overview' as well as 'Best Practices for Creating and Deploying HTTP Live Streaming Media for the…
drucifer
  • 801
  • 1
  • 7
  • 3
78
votes
5 answers

HTML5 frame-by-frame viewing / frame-seeking?

I'm looking for a way to view HTML5
Ory Band
  • 14,716
  • 14
  • 59
  • 66
77
votes
9 answers

HTML5 video will not loop

I have a video as a background to a web page, and I am trying to get it to loop. Here is the code:
75
votes
11 answers

How can I play a local video in my IPython notebook?

I've got a local video file (an .avi, but could be converted) that I would like to show a client (ie it is private and can't be published to the web), but I can't figure out how to play it in IPython notebook. After a little Googling it seems that…
Chris
  • 9,603
  • 15
  • 46
  • 67
74
votes
8 answers

Dynamically using the first frame as poster in HTML5 video?

I'm wondering if there's any straightforward way to achieve this effect, without needing backend code to extract a frame, save it as a jpg and database it somewhere. An effect whereby the first frame of the video just shows up as the poster when the…
Damon
  • 10,493
  • 16
  • 86
  • 144
70
votes
15 answers

Video 100% width and height

I have a video, and I want it to FILL 100% of the width, and 100% of the height. And keep the aspect ratio. Is it possible that it at least fills 100% for both? And if a bit of the video has to be out of the screen to keep the aspect ratio, that…
Milan
  • 863
  • 2
  • 9
  • 14
69
votes
11 answers

Disable download button for Google Chrome?

Google Chrome is now shipping with a download button for videos that are just embedded videos (i.e. not MSE): I'm having a hard time find any documentation for Chrome's implementation of the
ranvel
  • 861
  • 1
  • 14
  • 19
67
votes
12 answers

Javascript to stop HTML5 video playback on modal window close

I've got a html5 video element on a modal window. When I close the window the video continues to play. I'm a total newbie to JS. Is there an easy way to tie a video playback stop function to the window close button? Below is my html…
user747836
  • 671
  • 1
  • 5
  • 3
67
votes
7 answers

How can I set preview of video file, selecting from input type='file'

In one of my module, I need to browse video from input[type='file'], after that I need to show selected video before starting upload. I am using basic HTML tag to show. but it is not working. Here is…
Ronak Patel
  • 3,324
  • 4
  • 21
  • 31
63
votes
9 answers

HTML 5 Video "autoplay" not automatically starting in CHROME

I have the following code: The video: displays well…
jon
  • 760
  • 1
  • 5
  • 8