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:
Browser support can change and there may be mistakes in the information below (updated March 2018). 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/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 HTML5 should be open to accept all formats. As of 2018, most recent browers support most popular formats, i.e. WebM and MP4. Safari does not seem to play OGV (Ogg/Theora), the only free open source format.
The following table made in 2012 and updated March 2018 provides a summary overview. There may be mistakes and it does not include compatibility with all Codecs. It does not include browser extensions or that will add extra functionality, e.g. Flash. See LeanBackPlayer's or Wikipedia's HTML5 video for more detailed information.
Browser | Formats natively supported by different web browsers (March 2018) | ||
---|---|---|---|
ogv (Theora/Vorbis) | mp4 (H.264/AVC) | webm (VP8/Vorbis) | |
IE (11) | No | Yes | No |
Edge (17) | Yes | Yes | Yes |
Firefox (58) | Yes | yes | Yes |
Chrome (49) | Yes | Yes | Yes |
Chrome Android (64) | Yes | Yes | Yes |
Safari 11 | No | Yes | No |
iOS Safari 10.2 | No | Yes | No |
Opera | Yes | Yes | Yes |
Opera Mini | No | No | No |
Samsung Android | No | 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
Ogg Vorbis (.ogg) | WAV PCM (.wav) | MP3 (.mp3) | AAC (.m4a) | |
---|---|---|---|---|
IE | no | no | yes | yes |
Firefox | yes | yes | yes | |
Chrome | yes | yes | yes | yes |
Safari | no | yes | yes | yes |
Opera mini | yes | yes | no | no |
Android (Chrome, UC, Samsung) | yes | yes | yes | 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 some people still 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 may 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.
While the video tag and its attributes are standardized, the video formats (i.e. containers and codecs) are not, as explained above. For this reason, it is good practice to include several variants of the same file. As of 2018 most browsers do support popular video formats. However, since new formats are introduced every few years, a designer still must know about presenting alternatives. E.g. HEVC/H.265 only works in Microsoft and Apple Browsers as of March 2018. The former also require hardware support.
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: http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html (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.
</video>
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.
</video>
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
</video>
<p>*.ogv - 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
</video>
<p>*.mp4 - 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
</video>
<p>*.webm - This example wouldn't work in a browser like IE9
that doesn't support webm</p>
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.
</video>
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>
</video>
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."
</video>
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.
controls
controls="controls"
controls=""
The video element may have the following attributes:
Below we describe the most important ones.
src="myfile.webm"
src="vaction_picture.jpg"
controls
controls = "controls"
width="500"
height="400"
autoplay
muted
preload="none"
preload="metadata"
preload=""
preload="auto"
autobuffer
loop
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.
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:
Example file: http://tecfa.unige.ch/guides/html/html5-audio/html5-audio-simple.html (also look at its source code).
Ogg
<audio src="audio/guitar.ogg" controls>
Sorry, your browser doesn't support HTML5 audio
</audio>
MP3
<audio src="audio/guitar.mp3" controls>
Sorry, your browser doesn't support HTML5 audio
</audio>
<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.
</audio>
<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.
</audio>
There is no attribute for the volume, but there is JavaScript ...
Example:
The source element must be either a child of the video or the audio element. Below we recall a simple example:
<video id="movie1" controls>
<source src="vid.mp4" />
</video>
It can include the so-called global attributes (like "style" or "id"). It's proper attributes are:
type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"'
type="video/mp4;codecs='avc1.42E01E, mp4a.40.2'"
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="sampleCaptions.srt" srclang="en">
<track kind="descriptions" src="sampleDesciptions.srt" srclang="en">
<track kind="chapters" src="sampleChapters.srt" srclang="en">
<track kind="subtitles" src="sampleSubtitles_de.srt" srclang="de">
<track kind="subtitles" src="sampleSubtitles_en.srt" srclang="en">
<track kind="subtitles" src="sampleSubtitles_ja.srt" srclang="ja">
<track kind="subtitles" src="sampleSubtitles_oz.srt" srclang="oz">
<track kind="metadata" src="keyStage1.srt" srclang="en" label="Key Stage 1">
<track kind="metadata" src="keyStage2.srt" srclang="en" label="Key Stage 2">
<track kind="metadata" src="keyStage3.srt" srclang="en" label="Key Stage 3">
</video>
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="subtitles_en.srt" 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"
type="application/x-shockwave-flash"
data="http://releases.flowplayer.org/swf/flowplayer.swf">
<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer.swf" />
<param name="allowFullScreen" value="true" />
<param name="wmode" value="opaque" />
<param name="bgcolor" value="#000000" />
<param name="flashVars"
value="config={'playlist':['wikipedialogo.png',
{'url':'videos/state-of-wikipedia-480x272.mp4','autoPlay':false,'autobuffering':false}]}" />
</object>
<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." />
<div>
<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>
</div>
</div>
</div>
Live example file:
Track files:
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"
style="float:right;
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.
</video>
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>
<div>
<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>
</div>
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 vidojs.com). Other player products could be described in the same way.
Some of HTML5 players (in particular order) are:
HTML5 Video.org 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 !
HTML Media Capture, allows to capture data from device capturing mechanism such as a microphone or the Camera. It has become a recommendation in Feb 2018. As of March 2018 it only seems to work in mobile browsers such as iOS Safari, Chrome for Android or Samsung Internet. There exist other technologies to do that and that rely on JavaScript.
Below we include the examples from the 2018 recommendation, which also shows some JavaScript examples that we do not reproduce here.
Taking a picture
<form action="server.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" capture="user">
<input type="submit" value="Upload">
</form>
Taking a video with the rear camera
<form action="server.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="video" accept="video/*" capture="environment">
<input type="submit" value="Upload">
</form>
Capture an audio
<form action="server.cgi" method="post" enctype="multipart/form-data">
<input type="file" name="audio" accept="audio/*" capture>
<input type="submit" value="Upload">
</form>