Posted on May 18th, 2010 by Ian Devlin
Adding video to your website with HTML5
In the first of his blogs for PC Pro, web developer Ian Devlin reveals how to embed video into your website with HTML5

Probably the biggest and most talked about feature of HTML5 is embedded video. Currently, the only method of adding video content to your website is with a third-party plugin such as Flash, QuickTime or RealPlayer. With the dawn of HTML5 and the video element this will all change, with video support being handled by the web browser, doing away with the need for any third party support.
Several web browsers already offer support for HTML5. Here we’re going to reveal how you can embed plugin-free video into your site and the issues you’ll face.
File types and browser compatibility
To begin with, we’ll briefly take a look at the different video file types that are supported in HTML5. These are Theora OGG and H.264 (.mp4). Different browsers support different types, and some support none at all. The following table indicates this:
| Theora OGG | H.264 (mp4) | |
|---|---|---|
| Firefox 3.5+ | ✓ | x |
| Chrome 3+ | ✓ | ✓ |
| Safari 3+ | x | ✓ |
| Opera 10.5+ | ✓ | x |
| Internet Explorer 8 | x | x |
| Internet Explorer 9 | x | ✓ |
| iPhone | x | ✓ |
| Android | x | ✓ |
Codecs and other technical issues
HTML5 itself doesn’t specify a video codec to use, and this has led to arguments as to which is the best to use for the web. So to cover all browsers, we have to support both codecs.
And then of course there’s Internet Explorer. At the moment, none of the released versions of Internet Explorer support the video element and a plugin is still required to play video. This will change with the release of Internet Explorer 9 (likely early next year), when H.264 will be supported, along with many other HTML5 goodies.
In case you’re wondering how, you can convert your video files to OGG (you can read more about the Theora OGG type over at the TheoraCookbook) files using the Miro Video Converter.
For further in-depth information on the video element and codecs, head over to the dive into html5: video on the web by Mark Pilgrim.
HTML5 code
Now we move onto the actual HTML5 code and how we can get the thing to work. HTML5 provides us with two new elements that we can use to add video to our web pages: the <video> element, which we’ve already mentioned, and the <source> element. Let’s look at each of these in turn.
The <video> element
The video element can have the follow attributes:
| Attribute | Description |
|---|---|
| src | a valid URL to the video file itself |
| autoplay | a boolean indicating whether the video should be played automatically |
| controls | a boolean indicating that the default media controls should be displayed by the browser |
| loop | a boolean indicating whether the video should be played repeatedly |
| preload | indicates to the browser whether pre-emptive downloading of the video itself is required, or if metadata alone is all that’s needed. Possible values are:
|
| poster | the URL to an image file to be displayed when no video data is available |
| width | the width of the video, in CSS pixels |
| height | the height of the video, in CSS pixels |
From this, it can be seen how easy it is to embed an OGG video into your website using the video element alone:
<video src="myVideo.theora.ogg" autoplay controls></video>
That’s really all there is to it.
Any browser that supports the Theora OGG format should now successfully display and play your video without further ado. Of course it’s not as easy as that, because as we have seen from the table above, the code would only work in Firefox, Chrome and Opera. So we need to have a fallback to H.264 as well. This can be achieved using the <source> element, which allows us to define multiple media sources for the video element.
The <source> element
The source element has the following attributes:
| Attribute | Description |
|---|---|
| src | a valid URL to the media (in this case video) file itself |
| type | the type of the media file which must be a MIME type, e.g. type="video/ogg" indicates that it is a Theora OGG video, and you can also provide the MIME codec to help the browser to decide how to play the video by using type='video/ogg; codecs="theora, vorbis". |
| media | gives the intended media type of the media resource and must be a valid media query, e.g. media="handheld" indicates that the video is suitable for handheld devices or media="all and (min-device-height:720px)" which indicates that the video is appropriate for screens of 720 pixels or more. |
Note: that the source element is void and has a starting tag but no closing tag
The most useful thing about the the source element is that we can use it to stack the different file types, allowing the browser to try each in turn until it finds one that it can play.
Using <video> and <source> together
In order to stack videos in the different types within the video element, we enter the code as follows:
<video autoplay controls width="512" height="300">
<source src='myVideo.theora.ogg' type='video/ogg; codecs="theora, vorbis"'>
<source src='myVideo.mp4' type='video/mp4; codecs="mp4v.20.8, samr"'>
Your browser does not support the video element.
</video>
The above code will now work in all browsers except Internet Explorer, which will display the message indicated above.
You can test this yourself by viewing the HTML5 Test Video page, which contains a sample video of a butterfly in both Theora OGG and MP4 format, so if you’re viewing this in Firefox, Chrome, Safari, Opera or on the iPhone or an Android handset, you should be able to view it.
The sharp knives amongst you will now noticed that we can take advantage of this stacking and have a fallback to Flash (or some other plugin) at the bottom instead of displaying a “sorry you can’t see this video” message, by using the following code:
<video autoplay controls width="512" height="300">
<source src='myVideo.theora.ogg' type='video/ogg; codecs="theora, vorbis"'>
<source src='myVideo.mp4' type='video/mp4; codecs="mp4v.20.8, samr"'>
<object type="application/x-shockwave-flash" width="512" height="300" wmode="transparent"
data="flvplayer.swf?file=myVideo.flv&autoStart=true">
<param name="movie" value="flvplayer.swf?file=myVideo.flv&autoStart=true" />
<param name="wmode" value="transparent" />'
</object>
</video>
Conclusion
As with most HTML5 elements, browser support for the source and video elements is currently patchy. There’s also a large debate going on at the moment as to whether the source element will kill the usage of Flash as the most popular method of adding video content to websites. I’m not sure it will kill Flash completely, but nevertheless I think it’s fair to say that it’s here to stay and will provide web developers with a cleaner, easier approach to embedding video.
You can skip to the end and leave a response. Pinging is currently not allowed.
22 Responses to “ Adding video to your website with HTML5 ”
Leave a Reply
Authors
- Barry Collins
- Chris Brennan
- Christine Horton
- Darien Graham-Smith
- Dave Stevenson
- Davey Winder
- David Bayon
- David Fearon
- Ewen Rankin
- Ian Devlin
- Jon Honeyball
- Jonathan Bray
- Kevin Partner
- Mike Jennings
- Nicole Kobie
- Sasha Muller
- Steve Cassidy
- Stewart Mitchell
- Stuart Turton
- Tim Danton
- Tom Arah
Categories
- About the bloggers
- Android App of the Week
- cloud computing
- From Gmail to Hotmail
- Green
- Hardware
- How To
- iPhone App of the Week
- Just in
- Microsoft Office 2010
- Newsdesk
- Online business
- Random
- Rant
- Real World Computing
- Software
- View from the Labs
- Web
- Windows 7
- Windows 8
Archives
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
advertisement


May 18th, 2010 at 2:30 pm
Really useful article. HTML5 video is going to be hugely important in the battle between Adobe and Apple over Flash.
Is this the beginning of the end for Flash?
May 18th, 2010 at 3:19 pm
Oh goody, back to the bad old days of the “Which CODEC do you have?” game! *sigh*
May 18th, 2010 at 4:12 pm
One thing that concerns me is for those of using shared hosting and wanting to share a few small clips. I have oodles of server space and bandwidth on my plan but I’m not supposed to use more than 5% of server resources (i.e. CPU).
With HTML, as opposed to properly created real or flash streaming servers, how can I control the rate the browser downloads the video? Otherwise the browser will try to vacuum up the file as quickly as it can and I’m going to have a very unhappy hosting provider?
May 18th, 2010 at 4:34 pm
@Steve: I don’t think think it’s the end for Flash, but I think that HTML5 will take a large portion of the market. Flash will still remain very useful.
@Ben From what I’ve seen, you don’t have to set the codec, it’s merely to help the browser. They only need to support one or other of the two types and should be able to play them. I have tested without the codec and both work fine.
@AnnonyMuss you can use the preload attribute and set it to “none” so that the video isn’t downloaded immediately unless the user actually wants to view it. There is also a media elements JavaScript API that’s part of HTML5. Perhaps some playing about with that could lead to a solution that will allow you to pause downloads. Worth looking into I think.
May 20th, 2010 at 10:34 am
I have just been trying to convert Quicktime .mov and .mv4 files to theora.ogg using the Miro converter. In both cases the output is a theora.ogv file. Not being familiar with the format, I would welcome advice.
May 20th, 2010 at 10:37 am
@Henry Good point, I should have mentioned that. Rename it to filename.theora.ogg and it’ll be fine. The reason I say this is that sometimes webhosts aren’t set up to understand .ogv files, whereas they’re more likely to understand .ogg file extensions.
May 20th, 2010 at 11:02 am
I tried ifunia video converter pro, and it is fatistic to convert my videos to .ogg for html5.
May 20th, 2010 at 12:46 pm
I can’t actually get any of these samples to work. Well, apart from IE8, which does do exactly what I expected, and simply says “Sorry, your browser does not support the video element.”
Chrome shows a static image, but with a toolbar that although it is active, has no effect. Opera doesn’t even show the static image – yet has the toolbar. Not got FF installed at the moment.
May 20th, 2010 at 1:25 pm
Since my initial comment, I’ve installed FF and Safari, and the video does, indeed, work with both of those. Still no joy with Chrome or Opera though.
May 20th, 2010 at 1:25 pm
@John that sounds odd. Do you have a link to your code, or post it here?
May 20th, 2010 at 3:48 pm
@Ian – I was using your test page: http://www.pcpro.co.uk/blogs/wp-content/uploads/2010/05/html5-video-test.html
May 20th, 2010 at 3:52 pm
The video works with the latest version of FireFox (3.6.3), but not with the earlier version which it first volunteered.
May 20th, 2010 at 4:18 pm
@John with which versions of Chrome and Opera are you trying it with? I can verify that it works with Chrome 4.1.249.1064 and Opera 10.50 and 10.53 for Windows.
@Francis I have downloaded and successfully tested the video on Firefox 3.5.9 (the earliest version I could obtain from the Mozilla site).
May 20th, 2010 at 5:39 pm
@Ian Thanks for your earlier reply. I have now converted a video clip into theora.ogg, .mp4 and .flv and adjusted the code above. It worked in FF3 & Chrome OK, but not in IE8. I took out all the lines except for .ogg to test that it still worked in ff & chrome and it did. Doing the reverse leaving only flv did nothing for IE8. Ditto with mp4
May 20th, 2010 at 5:42 pm
@Henry add the following code into your HTML head:
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
May 21st, 2010 at 10:44 am
@Ian Thanks for the additional code. I still can’t make it work with IE8 in Win7. To overcome the problem to make a video available across different platforms etc, I uploaded some videos to You Tube and then embedded the link on my web page. Incorporating that code as the last inside the section does work. So FF and Chrome pick up the top half of the code and IE8 the second. I guess other IE version will do the same. Later in the day I will upload this to my site to see how it works in the wild. I will post the link when I have done it. The whole idea of this is to ensure that as many people as possible can view your video whatever their browser or plug-ins they may or may not have.
May 21st, 2010 at 2:55 pm
@Ian Right the test is at http://music-at.homeip.net/music/canarytest/
It incorporates all the code you have given us here plus the code to embed a You Tube video. It occurs to me to wonder if your code to play flv files can’t be made to work like the You Tube code, which is also flv. In which case we may have cracked it. I am no programmer, so can’t figure that one out!
May 21st, 2010 at 9:20 pm
@Henry I have your code working without the YouTube embedding. There’s a missing single quote at the end of the second source element.
<source src='canarycantata.mp4' type='video/mp4; codecs="mp4v.20.8, samr">should be
<source src='canarycantata.mp4' type='video/mp4; codecs="mp4v.20.8, samr"'>
IE isn't as forgiving with such errors and this is causing it to not see the Flash embedding.
Secondly, you need to actually have flvplayer.swf in the appropriate directory. This is available free to download and you should be able to find it through Google.
May 24th, 2010 at 12:53 pm
@Ian Ha! You got to be eagle-eyed to spot a missing ‘! Actually on an earlier trawl through the code, I had spotted that one of the other single quotes was missing, so I should have been alerted to look again. Then I did a search my system and found another 64 occasions when I had used the swf plugin. Anyway that’s cracked it. Thanks.
May 24th, 2010 at 1:30 pm
@Henry glad you managed to get it working in the end!
April 8th, 2012 at 8:00 pm
thanks for the article, had been wondering about video command.
i can’t seem to get it showing on below webpage
http://b4andon2work.info/PotentiallyU.php
April 11th, 2012 at 9:59 am
Hi Shaun. That link is using a Flash video I see, so there’s no HTML5 code there. You should also add the HTML5 doctype to the top of the HTML file, and please don’t automatically play the video. Good practice is that the user should choose to do this.