Camen Design

c share + remix

Video for Everybody!

  1. How It Works
  2. The Code
    1. IMPORTANT Notes
    2. Adding Custom Controls
  3. Encoding the Videos
    1. Using HD Video
    2. A Warning About H.264
    3. Using WebM Video
  4. Related Projects
  5. Acknowledgements

Video for Everybody is simply a chunk of HTML code that embeds a video into a website using the HTML5 <video> element, falling back to 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.

If you’ve arrived here from a link please note that VfE no longer includes QuickTime as part of the fallback, but the code for that version is still available here.

Hosting of the video generously provided by
André M. Åslund of Vorwärts GmbH.

Download Video: Closed Format: “MP4” • Open Format: “Ogg” / “WebM”


Thanks to the rapid adoption of HTML5 video happening right now, Video for Everybody isn’t the only solution around. Video for Everybody is for developers who either want something really simple they can quickly use on their blog or websites, or as a good starting point to develop their own custom solution. Video for Everybody is not a neatly packaged, fully-featured solution for non-developers.

How It Works

If your browser supports it, HTML5 video is used. No Flash—no crash.

iPad demonstrating Video for Everybody playback

If HTML5 video is not supported, Adobe Flash is used.
You can host locally or embed any Flash file, such as a YouTube video.

Screenshot of Internet Explorer 8 playing video using Adobe Flash
Flash fallback in IE8

Finally, if all else fails, a placeholder image is shown and the user can download the video using the links provided. If the user doesn’t have Flash they are not prompted to install it. Users have enough problems with security already without random websites prompting them to install things—and it’s even more annoying for people who don’t want or cannot use Flash anyway. This is one aspect that makes VfE different than any other video embedding method.

Screenshot of Firefox 3 without QuickTime or Flash installed, displaying a fall back message about the video
Final fallback image, you could use a different image that points to the download links

This is all done without JavaScript and requires only two video encodes, one Ogg file, and one MP4 file. Instructions on how to convert your videos to these formats with the correct settings are provided further down this article.

It’s compatible with HTML 4, HTML5 (valid markup), XHTML 1 and additionally also works when served as application/xhtml+xml.

For a full browser compatibility list, see the Video for Everybody Test Page.

The Code

Here follows the full source code. It’s very large because it’s fully commented.
You can easily compact this down (one such example follows afterwards).

Do not miss the important notes below or you will be kicking yourself after wasting hours trying to get it to work.

<!-- "Video For Everybody" v0.4.1 by Kroc Camen of Camen Design <camendesign.com/code/video_for_everybody>
     =================================================================================================================== -->
<!-- first try HTML5 playback: if serving as XML, expand `controls` to `controls="controls"` and autoplay likewise       -->
<!-- warning: playback does not work on iPad/iPhone if you include the poster attribute! fixed in iOS4.0                 -->
<video width="640" height="360" controls>
	<!-- MP4 must be first for iPad! -->
	<source src="__VIDEO__.MP4" type="video/mp4" /><!-- WebKit video    -->
	<source src="__VIDEO__.OGV" type="video/ogg" /><!-- Firefox / Opera -->
	<!-- fallback to Flash: -->
	<object width="640" height="384" type="application/x-shockwave-flash" data="__FLASH__.SWF">
		<!-- Firefox uses the `data` attribute above, IE/Safari uses the param below -->
		<param name="movie" value="__FLASH__.SWF" />
		<param name="flashvars" value="image=__POSTER__.JPG&amp;file=__VIDEO__.MP4" />
		<!-- fallback image. note the title field below, put the title of the video there -->
		<img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__"
		     title="No video playback capabilities, please download the video below" />
	</object>
</video>
<!-- you *must* offer a download link as they may be able to play the file locally. customise this bit all you want -->
<p>	<strong>Download Video:</strong>
	Closed Format:	<a href="__VIDEO__.MP4">"MP4"</a>
	Open Format:	<a href="__VIDEO__.OGV">"Ogg"</a>
</p>

(If you would like your video to automatically start playing, check out the sample code on the test page.)

Here’s a compacted version as an example:

<!-- Video for Everybody, Kroc Camen of Camen Design -->
<video width="640" height="360" controls>
	<source src="__VIDEO__.MP4"  type="video/mp4" />
	<source src="__VIDEO__.OGV"  type="video/ogg" />
	<object width="640" height="384" type="application/x-shockwave-flash" data="__FLASH__.SWF">
		<param name="movie" value="__FLASH__.SWF" />
		<param name="flashvars" value="image=__POSTER__.JPG&amp;file=__VIDEO__.MP4" />
		<img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__"
		     title="No video playback capabilities, please download the video below" />
	</object>
</video>
<p>	<strong>Download Video:</strong>
	Closed Format:	<a href="__VIDEO__.MP4">"MP4"</a>
	Open Format:	<a href="__VIDEO__.OGV">"Ogg"</a>
</p>

And one that auto plays: (notice the changes “autobuffer” and “autostart=true”)

<!-- Video for Everybody, Kroc Camen of Camen Design -->
<video width="640" height="360" controls autobuffer>
	<source src="__VIDEO__.MP4"  type="video/mp4" />
	<source src="__VIDEO__.OGV"  type="video/ogg" />
	<object width="640" height="384" type="application/x-shockwave-flash" data="__FLASH__.SWF">
		<param name="movie" value="__FLASH__.SWF" />
		<param name="flashvars" value="autostart=true&amp;image=__POSTER__.JPG&amp;file=__VIDEO__.MP4" />
		<img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__"
		     title="No video playback capabilities, please download the video below" />
	</object>
</video>
<p>	<strong>Download Video:</strong>
	Closed Format:	<a href="__VIDEO__.MP4">"MP4"</a>
	Open Format:	<a href="__VIDEO__.OGV">"Ogg"</a>
</p>

It’s advised you subscribe to the RSS to be kept informed of new releases in case you get caught out by new bugs introduced by vendors *cough*Apple*cough*. The version isn’t <1 for no reason.

IMPORTANT Notes

Adding Custom Controls

Since VfE doesn’t come with any JavaScript, the HTML5 video will use whatever native interface the browser provides. This is in the best interest of the user because it provides an interface best tailored to that device. For example, the iPhone always plays video fullscreen so that the edges of the video are not cut off in the browser and the user does not have to pan around to get it all in view. The iPad provides finger-friendly sized controls.

Two screenshots of the different HTML5 controls in Opera 10.5 and Google Chrome
Different native video controls in Opera 10.5 and Google Chrome

Designers however don’t like the inconsistency and would like a unified set of controls. This is easy to achieve with some JavaScript. Steve Haffernan has written a tutorial to get you started (this demo page contains better markup, but he hasn’t updated his article to link to this yet). He has also rolled this into a project of its own called video.js which uses VfE and custom controls you can style how you please with CSS.

When adding controls to HTML5 video check if HTML5 video playback is possible first (“!!document.createElement('video').canPlayType”) and insert the HTML controls into the page using JavaScript. Why insert the controls instead of just unhiding them using CSS? Think of RSS readers and other aggregators where the JavaScript (and CSS) won’t travel to the user. You don’t want to present them a set of ‘broken’ controls. Instead, dynamically remove the controls attribute with JavaScript to hide the native controls and then insert your controls into the page. Also, of course, if the browser doesn’t support HTML5 then the Flash video will show and you’ll end up with two sets of controls!

Encoding the Videos

Full instructions are beyond the scope of this article, please refer to Mark Pilgrim’s Video on the Web article for an excellent introduction to video formats and encoding instructions.

Using HD Video

If you would like to use a larger video than 640x480, you can use a QuickTime reference movie to auto-select between an iPhone compatible version and the full-size video. In QuickTime Pro use the ‘File » Export for Web…’ option to output a reference movie (you can also use Apple’s MakeRefMovie tool for finer control). You’ll have three files along these lines: “video.mov”, “video-desktop.mp4” (or m4v) and “video-iphone.mp4”. Now replace the two source elements in the code with these three: (substituting the right file paths)

<source src="video.mov" type="video/mp4"></source>
<source src="video-desktop.mp4" type="video/mp4"></source>
<source src="video.ogv" type="video/ogg"></source>

What happens here is that the browser will play the QuickTime reference movie (Safari / iPhone / iPad) which will auto-select between the desktop and iPhone versions of the video automatically. If the MOV format isn’t supported by the browser (Chrome for example), we point to the same MPEG4 video that the QuickTime reference movie uses.

A Warning About H.264

I made Video for Everybody because since I don’t have Flash installed I wanted to create a way websites could provide me access to their videos (currently needlessly trapped inside Flash) without having to lose viewers from older browsers. VfE is not a tool I would use myself (other videos on this site are HTML5/Ogg only) because of the threat that H.264 represents to freedom on the web. Websites that are already serving H.264 video to users using Flash have already made the conscious decision to buy into H.264, including what that entails come 2016 when royalty fees kick in (I expect large video sites like YouTube are already well aware of the costs and have the money to pay them).

Just be aware that if you decide to use H.264 video for commercial purposes then you will need to purchase a licence from the MPEG-LA, and for non-commercial purposes (which are vaguely defined) it is free up until 2016 where they can change the rules and charge you.

Using WebM Video

On the 19th of May 2010 Google released the VP8 codec as open-source and royalty free with the full intent to drive broad adoption via industry backing and switching YouTube over to the new format in the long term. “WebM” is a rebranded Matroska container utilising VP8 video and Vorbis audio.

This represents major competition to H.264—Mozilla, Google and Opera have already added support into special builds of their browsers and even Microsoft have about-faced on their H.264-only policy and said that IE9 will support WebM—but only if the codec has been installed by the user. Obviously absent from any support is Apple, and this means that unfortunately codec-fragmentation will continue into the foreseeable future so that you will still need to provide more than one video encode.

Adding a WebM video to Video for Everybody is easy, just add it to the source list! It has to go below the MP4 video due to an iPad bug that ignores anything but the first source element, and ideally above the Ogg source so that browsers that play both Ogg and WebM choose the WebM video first. Here is an example source stack:

<source src="video.mp4"  type="video/mp4"  />
<source src="video.webm" type="video/webm" />
<source src="video.ogv"  type="video/ogg"  />

Note the new mime type, which you will have to add to your server.

At this extremely early stage for WebM I will not provide any help via e-mail on how to encode or use WebM video. If you don’t know how to use it already, it’s not for you—wait until support is added to your favourite tools and more information is readily available. I will update this article as the situation with WebM progresses.

If you’ve modified Video for Everybody to do something else, or have created an HTML5 video related project, please let me know and if it upholds the same principles as Video for Everybody, I’ll list it here.

(I will not list projects that cannot play the video in an RSS reader. Using JavaScript to insert <video> defeats the entire purpose.)

video.js
Based on Video for Everybody but adds custom video controls made of HTML that you can style anyway you please with a bit of CSS. The project is new and there are bugs yet to be ironed out. Remember as with all HTML5 video, do your own testing and only utilise a project like this only if you (or your developer) are knowledgeable enough to fix bugs and keep abreast of the rapidly changing HTML5 landscape.
Univers Video Plugin
A WordPress plugin that provides you a simple interface for inserting video. It uses Video for Everybody for the base template but steps up to a custom interface if JavaScript is available. The plugin even does server-side encoding. The Flash interface is designed to closely mimic the look of Firefox’s native <video> controls to provide consistency, regardless of technology used.
External Video for Everybody
A WordPress plugin that provides a shortcode to insert video using Video for Everybody. The author has also provided an excellent bash script to automate the process of encoding the video files.
Degradable HTML5 audio and video Plugin
Another WordPress plugin. It doesn’t use VfE, but does provide HTML5 video and Flash fallback.

Acknowledgements