Skip to navigation

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.

// Home / Blogs

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

NEWSonyHDRBack_Web

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:

  • none – indicates that the video is not to be preloaded (as it probably won’t be required)
  • metadata – the video is probably not going to be required but it’s metadata (e.g. dimensions, duration) is desirable
  • auto – informs the browser to attempt to download the entire video
  • (empty string) – means the same as auto
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.

Tags: ,

Posted in: How To

Permalink

Follow any responses to this entry through the RSS 2.0 feed.

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 ”

  1. Steve Haigh Says:
    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?

     
  2. Ben Says:
    May 18th, 2010 at 3:19 pm

    Oh goody, back to the bad old days of the “Which CODEC do you have?” game! *sigh*

     
  3. AnonnyMuss Says:
    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?

     
  4. Ian Devlin Says:
    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.

     
  5. Henry West Says:
    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.

     
  6. Ian Devlin Says:
    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.

     
  7. stonee Says:
    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.

     
  8. John Whitworth Says:
    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.

     
  9. John Whitworth Says:
    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.

     
  10. Ian Devlin Says:
    May 20th, 2010 at 1:25 pm

    @John that sounds odd. Do you have a link to your code, or post it here?

     
  11. John Whitworth Says:
    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

     
  12. Francis King Says:
    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.

     
  13. Ian Devlin Says:
    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).

     
  14. Henry West Says:
    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

     
  15. Ian Devlin Says:
    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]-->

     
  16. Henry West Says:
    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.

     
  17. Henry West Says:
    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!

     
  18. Ian Devlin Says:
    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.

     
  19. Henry West Says:
    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.

     
  20. Ian Devlin Says:
    May 24th, 2010 at 1:30 pm

    @Henry glad you managed to get it working in the end!

     
  21. Shaun Masterton Says:
    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

     
  22. Ian Says:
    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.

     

Leave a Reply

Spam Protection by WP-SpamFree

* required fields

* Will not be published

Authors

Categories

Archives

advertisement

SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2010