HTML5 audio and video

From EduTech Wiki
Jump to: navigation, search

1 Introduction

This tutorial shows basic use of video and audio in HTML5. HTML5 includes special elements (tags) allowing to include video and audio and to define controls. Unlike in HTML 4.x and XHTML 1.x, video and audio is fully integrated in HTML5, meaning that these elements can also be styled and scripted via the DOM.

Before learning how to use video and audio in HTML5, you need to understand that media files are actually containers (i.e. so-called Multimedia container formats) that include in turn various types of data files, e.g. video and audio streams, chapter-information, captions (subtitles) and meta-information plus synchronization information. Audio and video data are compressed with so-called codecs (compression and decompression algorithms).

See also:

1.1 Browser support

Browser support can change and there may be mistakes in the information below (updated Jan 2014). We did not test Safari and Android ourselves. In addition, a same browser may behave differently on different operating systems. Test it yourself, e.g. by using our simple example file ...). Or better, explore the links at the end of this article for more exhaustive and up-to-date information.

The most popular HTML5 video formats are Webm/VP8 (soon: Web/VP9), mp4/H.264 and OGG/Theora (ogv). Originally, the HTML5 specification wanted to include OGG as standard, but after vendor opposition, it was decided to support all formats.

The following table provides an overview of implementations as of April 2012. It does not include browser extensions that will add extra functionality. See LeanBackPlayer's or Wikipedia's HTML5 video for more detailed information.

Browser implementations of various Video formats
Browser Formats natively supported by different web browsers (April 2012)
ogv (Theora/Vorbis) mp4 (H.264/AVC) webm (VP8/Vorbis)
IE (9) No Yes No
Firefox (11) Yes no Yes
Chrome Yes Yes Yes
Safari No Yes No
Opera Yes No Yes
Android Yes Yes Yes

The example video we are using is this page was taken from Vimeo. It includes a 2011 talk about the state of Wikipedia by Jimmy Wales, its founder. We produced several versions without paying a lot of attention to encoding details.

Browser support for audio, as for Video, is not uniform

Browser implementations of various audio formats
Ogg Vorbis (.ogg) WAV PCM (.wav) MP3 (.mp3) AAC (.m4a)
IE no no yes yes
Firefox yes yes no no
Chrome yes yes yes yes
Safari  ? yes yes yes
Opera yes yes no no
Android  ?  ? yes  ?

Note about "fallback": When you create a webpage and you wish to integrate video and sound using HTML5, you have to take into account the fact that people may be using browsers that don't support HTML5. Since your goal is to be sure your content is available to the widest possible audience, you have to provide an alternative. This is called a "fallback": you either display a message stating that their browser doesn't support HTML5 (displaying ONLY a message is not a very good idea, since they won't be able to watch your video) or switch to a player that makes use of a more common (and older) technology such as an Adobe Flash player.

2 Basic use of video

While the video tag and its attributes are standardized, the video formats (i.e. containers and codecs) are not. For this reason, it is good practice to include several variants of the same file. Since the HTML5 video element doesn't work with older browsers and since default skin and controls differ among browser makers, you also may consider using an HTML5 video player library.

Example file: (also look at its source code).

A note on self-closing tags and boolean attributes: The following code is perfectly legal HTML5:

<video id="movie1" controls>
  <source src="videos/state-of-wikipedia-480x272.mp4" >
  Your browser doesn't support HTML5. Maybe you should upgrade.

However, since there may be a future XHTML5 profile and since editors deal better with closed tags we suggest to make your code XML compliant:

<video id="movie1" controls="controls">
  <source src="videos/state-of-wikipedia-480x272.mp4" />
  Your browser doesn't support HTML5. Maybe you should upgrade.

2.1 Very simple use - not recommended

The following example code shows how to include a single video format. We will show three variants, one for each of popular the HTML5-supported formats. Including a single variant is not recommended. See below for defining alternatives.

<video src="videos/state-of-wikipedia-480x272.ogv" controls>
Sorry, your browser doesn't support HTML5 video
<p>This example wouldn't work in a browser like IE9 
   that doesn't support Ogg</p>

<video src="videos/state-of-wikipedia-480x272.mp4" controls>
Sorry, your browser doesn't support HTML5 video
<p>This example wouldn't work in a browser like Firefox 
   that doesn't support mp4</p>

<video src="videos/state-of-wikipedia-480x272.webm" controls>
Sorry, your browser doesn't support HTML5 video
<p>This example wouldn't work in a browser like IE9
   that doesn't support webm</p>

2.2 Simple use of alternatives

The following code shows how to define three alternatives with a fallback message for non HTML5 browsers.

<video id="movie1" controls>
  <source src="videos/state-of-wikipedia-480x272.mp4" />
  <source src="videos/state-of-wikipedia-480x272.ogv" />
  <source src="videos/state-of-wikipedia-480x272.webm" />
  Your browser doesn't support HTML5. Maybe you should upgrade.

The following code shows how to define alternatives that specify codecs. This method is useful if you plan to include "high quality" videos that some clients may not support.

<video id="movie2" controls="controls">
  <source src="videos/state-of-wikipedia-480x272.mp4" 
          type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"' />
  <source src="videos/state-of-wikipedia-480x272.ogv"
          type='video/ogg; codecs="theora, vorbis"' />
  <source src="videos/state-of-wikipedia-480x272.webm"
          type='video/webm; codecs="vp8, vorbis"' />
	<p>The video is available as <a href="videos/state-of-wikipedia-480x272.mp4">H.264 in an MP4 container</a>, 
	or as <a href="videos/state-of-wikipedia-480x272.ogv">VP8 in a Ogg container</a>
	or as <a href="videos/state-of-wikipedia-480x272.webm">VP8 in a
	WebM container</a>. </p>

3 The HTML5 video element

3.1 Content model

  • The video element can not be used inside the a and the button elements
  • The video element must have both a start and an end tag. I.e. it can't be self-closed.
  • The video element either must include a single src attribute or one or more source elements followed by track elements, followed by any sort of or flow contents, i.e. all sorts of stuff, but that only will be shown as fallback).

Typical structure of a video element:

<video id="movie1" controls>
  <source src="vid.mp4" />
  <source src="..." />
  <track enabled="true" kind="subtitles" label="EN VTT" src="captions.vtt" srclang="en" type="text/vtt"/>
  Any sort of extra stuff below for fallback. E.g. a message like
  "Your browser doesn't support HTML5. Maybe you should upgrade."

3.2 Parameters of the video element

HTML5 attributes work like HTML4 attributes, i.e. so-called boolean attributes dont' need to include a value. In addition, it seems that attribute values don't need to be quoted in HTML5. The following three lines are strictly equivalent.


Allowed video attributes:

  • global attributes & autoplay & preload & controls & loop & poster & height & width & mediagroup & muted & src
src = "source file"
Allows to define a single video file. See the source element below for a better alternative.
poster = "picture file"
Will display a picture while the video downloads and before the user decides to play the video. If absent, the first frame of the video will be shown.
controls = on|off
Defines if use controls should be displayed. By default there will be none.
controls = "controls"
width and height = "size"
Allow to define size of the video element
By default, width and height will adapt to the original size.
autoplay = on|off
If present, the video will start playing automatically. This can be annoying since the user may not want to look at it.
muted = on|off
If present, will mute the audio
preload = none | metadata | auto
Since most browsers will automatically start downloading the whole video, one could use this attribute to help the users save bandwith (in particular on mobile devices and slow home networks).
Example: The author hints that nothing should be downloaded.
Example: Enough data should be downloaded to show a frame and to determine duration.
Example: browser should download as much as it sees fit to give a good user experience, possibly downloading the whole video.
autobuffer (deprecated element, don't use this !)
If present, will start downloading the whole video after the page is loaded. Also use with care, because this will take up bandwidth.
loop = on | off
If present, will loop
style = "CSS style"
CSS style properties
mediagroup = "string with resources"
Will tell the the client to link multiple video and audio streams
Example (needed)

3.3 Web server configuration

Your web server must define all the mime types, else web browsers may not display video/audio.

In particular, either add the following mime types in the mime.types file

video/ogg ogg ogm ogv
video/mp4 mp4
video/webm webm
audio/mpeg mpga mp2 mp2a mp3 m2a m3a
audio/ogg oga ogg spx
audio/mp4 mp4a

On an Apache server, you also could edit an http.conf file like this:

AddType video/ogg .ogm
AddType video/ogg .ogv
AddType video/ogg .ogg
AddType video/mp4 .mp4
AddType video/webm .webm
AddType audio/mpeg mpga mp2 mp2a mp3 m2a m3a
AddType audio/ogg oga ogg spx
AddType audio/mp4 mp4a
AddType audio/playlist m3u M3U

If you don't have administrator rights, you could add them on a per directory basis, e.g. put the above list of AddTypes in the .htaccess file on Apache.

4 The audio element

The audio element is very similar to the video element. It includes the same content model (must not be within an a or a button) and can include source elements, followed by track elements followed by flow elements or phrasing content.

Permitted attributes are mostly the same:

  • global attributes & autoplay & preload & controls & loop & mediagroup & muted & src
  • Audio doesn't support the poster attribute (wouldn't make sense).

Example file: (also look at its source code).

4.1 Simple use with control


<audio src="audio/guitar.ogg" controls>
  Sorry, your browser doesn't support HTML5 audio


<audio src="audio/guitar.mp3" controls>
  Sorry, your browser doesn't support HTML5 audio

4.2 Alternatives

<audio id="audio1" controls>
  <source src="audio/guitar.mp3">
  <source src="audio/guitar.ogg">
  <source src="audio/guitar.m4a">
  <source src="audio/guitar.wav">
Your browser doesn't support HTML5. Maybe you should upgrade.

4.3 Simple background music

<audio id="my_song" autoplay loop>
 <source src="audio/guitar.mp3">
 <source src="audio/guitar.ogg">
 <source src="audio/guitar.m4a">
 <source src="audio/guitar.wav">
 Your browser doesn't support HTML5. Maybe you should upgrade.

There is no attribute for the volume, but there is JavaScript ...


5 The source element

The source element must be either a child of the video or the audio element.

It can include the so-called global attributes (like "style" or "id"). It's proper attributes are:

src ="URL" (mandatory)
Identifies the video file
type="MIME type"
Allows to identifies the mime type of the media source plus precise codes.
 type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"'
 type="video/mp4;codecs='avc1.42E01E, mp4a.40.2'"
Can define a media type to help the client selecting the right video source.

6 The track element

The track element allows to add captions (subtitles) and other information, i.e. allow to add textual information that can be displayed to the user. However, track currently (April 2012) is not implemented in most browsers. In addition, the supported track file formats still seem to be a very moving target. Contenders are WebVTT, SRT and Timed Text (TTML). In addition, some containers also may include their own "track" data.

Example code found at Mozilla. Tracks may not yet work in your browser.

<video src="sample.ogv">
   <track kind="captions" src="" srclang="en">
   <track kind="descriptions" src="" srclang="en">
   <track kind="chapters" src="" srclang="en">
   <track kind="subtitles" src="" srclang="de">
   <track kind="subtitles" src="" srclang="en">
   <track kind="subtitles" src="" srclang="ja">
   <track kind="subtitles" src="" srclang="oz">
   <track kind="metadata" src="" srclang="en" label="Key Stage 1">
   <track kind="metadata" src="" srclang="en" label="Key Stage 2">
   <track kind="metadata" src="" srclang="en" label="Key Stage 3">

Good HTML5 players, however implement some of these track formats.

Simple live example using the LeanBack Player

<div class="leanback-player-video">
  <video controls="controls" preload="metadata" poster="wikipedialogo.png" width="480" height="272">
   <source src="videos/state-of-wikipedia-480x272.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
   <source src="videos/state-of-wikipedia-480x272.webm" type='video/webm; codecs="vp8, vorbis"' />
   <source src="videos/state-of-wikipedia-480x272.ogv" type='video/ogg; codecs="theora, vorbis"' />
   <track enabled="true" kind="subtitles" label="EN"
          src="" srclang="en" type="text/x-srt"/>
   <track enabled="true" kind="subtitles" label="EN VTT"
          src="subtitles_en.vtt" srclang="en" type="text/vtt"/>
   <track enabled="true" kind="subtitles" label="FR" 
          src="subtitles_fr.vtt" srclang="fr" type="text/vtt"/>
   <object class="leanback-player-flash-fallback" width="640" height="360"
      <param name="movie" value="" />
      <param name="allowFullScreen" value="true" />
      <param name="wmode" value="opaque" />
      <param name="bgcolor" value="#000000" />
      <param name="flashVars" 
		{'url':'videos/state-of-wikipedia-480x272.mp4','autoPlay':false,'autobuffering':false}]}" />
   <div class="leanback-player-html-fallback" style="width: 640px; height: 360px;">
     <img src="wikipedialogo.png" width="640" height="360" alt="Poster Image" 
          title="No HTML5-Video playback capabilities found. Please download the video(s) below." />
<strong>Download Video:</strong>
   <a href="videos/state-of-wikipedia-480x272.mp4">.mp4</a>
   <a href="videos/state-of-wikipedia-480x272.webm">.webm</a>
   <a href="videos/state-of-wikipedia-480x272.ogv">.ogv</a>

Live example file:

Track files:

6.1 Track attributes

Kind of text track. The following keywords are allowed according to Mozilla:
subtitles: A transcription or translation of the dialogue.
captions: A transcription or translation of the dialogue or other sound effects. Suitable for users who are deaf or when the sound is muted.
descriptions: Textual descriptions of the video content. Suitable for users who are blind or not able to view the video for another reason.
chapters: Chapter titles, intended to be used when the user is navigating the media resource.
metadata: Tracks used by script. Not visible to the user.
src (mandatory)
Defines the URL for the track file
Defines the language of the track text, so that the user and/or the client can make a choice
A user-readable title of the text track Used by the browser when listing available text tracks.
Identifies a track as default track. It only can be used with one track (of course).

7 Style and controlling media playback

In addition to using the width and height attribute for the video tag, you can use any reasonable and not reasonable CSS properties.

<video id="movie1" controls preload="metadata" 
                 margin: 0px 10px 0px 10px;
                 border-style: solid;
                 border-width: 50px 50px 50px 50px;
                 border-image: url(bricks.jpg) 50 round;
                 -webkit-border-image: url(bricks.jpg) 50 round;
                 -moz-border-image: url(bricks.jpg) 50 round;
 <source src="videos/state-of-wikipedia-480x272.mp4">
 <source src="videos/state-of-wikipedia-480x272.ogv">
 <source src="videos/state-of-wikipedia-480x272.webm">
 Your browser doesn't support HTML5. Maybe you should upgrade.

Users can control playback through the default player. However, in case you don't want to use the control attribute, you can implement your own through JavaScript. An other alternative is to work with a Player library (see below).

Below is an audio control example found in the Using audio in video in Firefox tutorial

<audio id="demo" src="audio.mp3"></audio>
  <button onclick="document.getElementById('demo').play()">Play the Audio</button>
  <button onclick="document.getElementById('demo').pause()">Pause the Audio</button>
  <button onclick="document.getElementById('demo').volume+=0.1">Increase Volume</button>
  <button onclick="document.getElementById('demo').volume-=0.1">Decrease Volume</button>

8 HTML5 player libraries

Since default controls in navigators do not offer a lot of functionality and since there still many non-HTML5 navigators installed, so-called HTML5 video players can offer both advanced controls and fallback. In addition they may implement features that are still missing in various browser implementations. These HTML5 video players are implemented with ready-to-go JavaScript libraries. Of course, you could write your own.

In addition, web designers may not like the visual aspect of default player and may want to adapt it to the page design.

These players are implemented with JavaScript, offer different functionality and may or may not be easy to use. More precisely, this principle can be explained by example: “An HTML5 Video Player is a JavaScript library that builds a custom set of controls over top of the HTML5 video element to provide a consistent look between HTML5 browsers. Video.js builds on this by fixing many cross browser bugs or inconsistencies, adding new features that haven't been implemented by all browsers (like fullscreen and subtitles), as well as providing one consistent JavaScript API for both HTML5, Flash, and other playback technologies.” (What's an HTML5 Video Player, retrieved 13:36, 22 April 2012 (CEST) from Other player products could be described in the same way.

Some of HTML5 players (in particular order) are:

HTML5 offers a HTML5 Player Comparison, i.e. provides an overview table plus a detailed description of each HTML5 Player library. Recommended reading, although it may be biased since it is sponsored by Kaltura.

Otherwise, you can search for something like "best html5 player", but pay attention to the publication date !

9 Video capturing

HTML Media Capture seems to be still under discussion as of early 2013

A first proposal works by overloading the <input type="file"> and adding new values for the accept parameter. If you wanted to let users take a snapshot of themselves with the webcam, that's possible with capture=camera:

<input type="file" accept="image/*;capture=camera">

Recording a video or audio is similar:

<input type="file" accept="video/*;capture=camcorder">
<input type="file" accept="audio/*;capture=microphone">

However, that proposal seems to be dead now and it has been replaced by javascript extensions.

To learn more about the media capture you can follow these links:

3rd January 2013.

(to do ....)

10 Links

10.1 Specifications

  • WebRTC 1.0: Real-time Communication Between Browsers, W3C Editor's Draft 16 March 2012. This specification defines a set of APIs to represent streaming media, including audio and video, in JavaScript, to allow media to be sent over the network to another browser or device implementing the appropriate set of real-time protocols, and media received from another browser or device to be processed and displayed locally.

10.2 Manuals and reference

10.3 Tutorials

10.4 Web sites

10.5 Example code / testing