The HTML5 <video> tag makes it possible to embed video clips in web pages much like how the <img> tag works for images. The browser itself provides the playback functionality without any need for plugins like Quicktime or Flash.
Update: Firefox 3.5+ doesn't like the .ogm extension for the Ogg Theora video file, I've changed it to .ogg.
In it's simplest form, the tag can look like this:
<video src="someclip.mp4" controls />
Safari (version 3.1 onwards) and Firefox 3.1b2 support <video>. Opera has released experimental builds demonstrating preliminary support.
Safari plays back MPEG4 (and at least on the Mac, anything that QuickTime can play). Firefox currently only supports Ogg Theora.
This lack of (default) codec support overlap can be overcome by providing both formats in the same <video> tag:
<video controls>
<source src="video.m4v" type="video/mp4" /> <!-- MPEG4 for Safari -->
<source src="video.ogg" type="video/ogg" /> <!-- Ogg Theora for Firefox 3.1b2 -->
</video>
For legacy browsers that don't support <video> we need to use an alternative. An easy solution is to degrade to Flash using SWFObject.
Here's the HTML:
<div id="demo-video-flash">
<video id="demo-video" poster="snapshot.jpg" controls>
<source src="video.m4v" type="video/mp4" /> <!-- MPEG4 for Safari -->
<source src="video.ogg" type="video/ogg" /> <!-- Ogg Theora for Firefox 3.1b2 -->
</video>
</div>
We place the <video> tag inside the <div> that we use to attach the SWFObject object.
Now for some JavaScript:
<script type="text/javascript">
$(document).ready(function() { // ... a dash of jQuery.
var v = document.createElement("video"); // Are we dealing with a browser that supports <video>?
if ( !v.play ) { // If no, use Flash.
var params = {
allowfullscreen: "true",
allowscriptaccess: "always"
};
var flashvars = {
file: "video.f4v",
image: "snapshot.jpg"
};
swfobject.embedSWF("player.swf", "demo-video-flash", "480", "272", "9.0.0", "expressInstall.swf", flashvars, params);
}
});
</script>
The browser support detection is done by creating a video element and attempting to play it. If this fails, the Flash player is inserted using SWFObject.
Here's the example in action.
A downside to this approach as opposed to using Flash or another plugin is obviously that we currently need to provide three versions of every clip we want to embed (MPEG4, Ogg and FLV).
Hopefully the browser makers will have settled on a baseline codec by the time the final versions of Firefox 3.1 and Opera 10 (or whatever version finally brings the support) are released.
Comments
Great post! Thanks for the informative article!
For some reason the example doesn't work on iPhone however. Any chance of getting a solution that works across all platforms?
This isn't working for me on Firefox 1.5 but works on everything else. Anyone else have this issue?
This is extremely helpful.
Your sample video does not include the full-screen control in the player (it is in the code). How would that be included so that the video can be viewed full-screen?
I'm trying to fashion a website that anyone can view without having to download plug-ins. I'm leaning towards H.264 files played through a Flash/Quicktime player. In contrast to Quicktime, Flash controls are more customizable and supports full-screen playback. Any ideas? We will be using Leopard and QTSS.
The following website has also been helpful.
http://camendesign.com/code/video_for_everybody
@Richard Das: It works in iPhone OS 3.0.
@Jay: I haven't tested it with Firefox 1.5, but that userbase should be pretty negligeble by now.
@Anonymous: I don't believe any browser vendor has implemented full screen viewing of HTML5 video yet.
The javascript has the allowfullscreen: "true" parameter, but the player in the example does not have a full-screen button. How can that be added to the player?
Thanks
As an option, you could just encode the video in MP4. Drop the FF3 ogg format, drop support for older Flash clients. Then pump the same MP4 file into your Flash player.
@Anonymous: The feature needs to be implemented in the browser and no browser currently supports it.
@Rob Colburn: That's a great idea. I try to avoid Flash as much as possible, but that's an excellent way to target everything from Mobile Safari to Internet Explorer using the same video file.
Making it fullscreen can be done easily with CSS3, and can also be done with very little fuss using jQuery or Mootools. Nice, smooth transitions to full-screen.
But personally I'm just going to wait until Apple & Mozilla quit arguing over formats (the img spec doesn't have only one supported format!) and get back on to what's best for developers.
are you people retarded? you need firefox 3.5 for html5 to work. great job btw. I like the flash fallback.
Anonymous said:
Um, if so I must be using FF3.5. But it says it's Safari 4. Wait, it is Safari 4. But my (somewhat) HTML 5-built pages work just fine. Now, how is that? (Oh, yeah... Mac OS X... ;))
Scaling video content to fullscreen using nothing but CSS is the best solution imho. Just add video {width:100%; height auto;} to your style sheet and you're like done. At least, that works in Safari 4. As in the browser I use mostly. In other words, I agree completely with Conrad.
Good write-up, Henrik. Thanks.
Why wouldn't we use The Right Way instead?
...flash...Browsers must render the internal contents of the element only if they do not understand the tag.
I suggest include a control for subtitle (captioning). Regards.
You need Firefox 3.5+ or a recent Safari or Chrome to use the video element.
Also, you should change your ogg extensions, because the standard says video/ogg has the extension .ogv - .ogg are Ogg vorbis audio files. I'm surprised your video works in Firefox.
These blog posts explain in more detail: http://www.bluishcoder.co.nz/2009/02/fallback-options-for-html5-video.html, http://blog.gingertech.net/2009/07/08/javascript-libraries-for-support/
BTW: a standard means to include subtitles is in the works...
Another alternative would be to the following:
- test for video format support
- try to see if plugins will play format
- and then optionally fall back to flash
Not sure if the second step will do the job, but given prevalence of plugins support MP4 such as MPlayer and Quicktime, then this could be an alternative.
Video for Everybody is simply a chunk of HTML code that embeds a video into a website using the HTML5 element, falling back to QuickTime and Flash automatically, without the use of JavaScript or browser-sniffing. It therefore works in RSS readers (no JavaScript), on the iPhone / iPad (don’t support Flash) and on many, many browsers and platforms.
http://camendesign.com/code/video_for_everybody
From my own tests it seems that iPhone with OS 3.0 or later which is meant to support HTML5 in fact doesn't implement it correctly, as soon as I add the ogg video for firefox in the source it breaks as per the links below, you can view the source code to see the minor difference.
Works for iPhone
http://www.cleverbits.com/html6/
Doesn't work for iPhone
http://www.cleverbits.com/html7/
In fact the video for evrybody code doesn't work for many iPhones I've tested on, can anyone else confirm this or provide a work around?
That "allow fullscreen" parameter is for the Flash object that swfobject embeds, it has nothing to do with the html5 elements.
Are you people retarded? you need firefox 3.5 for html5 to work.
Um, if so I must be using FF3.5. But it says it's Safari 4. Wait, it is Safari 4. But my (somewhat) HTML 5-built pages work just fine. Now, how is that? (Oh, yeah... Mac OS X... ;))
Scaling video content to fullscreen using nothing but CSS is the best solution imho. Just add video {width:100%; height auto;} to your style sheet and you're like done. At least, that works in Safari 4. As in the browser I use mostly. In other words, I agree completely with Conrad.
Good write-up, Henrik. Thanks.
Vist me at: get pregnant
Post new comment