Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
May 31, 2014
Chris
HTML5
Comments Off on Crossbrowser-safe HTML5 video (IE6+) with a few lines of code and just one .mp4 video file

Crossbrowser-safe HTML5 video (IE6+) with a few lines of code and just one .mp4 video file

PreviousNext
shadow dom

No time to read the full article ? Get the code directly here on GitHub: panique/html5-video.

Publishing a video on the web is a massive pain in the ass. Even in 2014, several years after HTML5 video was implemented in major browsers, it’s still not that easy, there’s still no de facto standard on how to do this and there’s no good solution / recommendation on StackOverflow. While trying to find a way to implement videos on websites that will run flawlessly in EVERY browser, from IE6 to latest ones and even on most modern smartphones, I’ve been stumbling over an interesting project that provides a definitive crossbrowser-safe HTML5 video solution:

The (totally awesome) person behind the Video For Everybody Generator (fun-fact: site itself is broken in all IE, but the code generated works perfectly in IE6+) has created a simple interface that let’s you enter the paths to your video file(s), your optional cover picture and optional video download links and generates a crossbrowsersafe block of copy&paste-code that contains a HTML5 video element and the according links to an .mp4 file (and optional .ogv and .webm video files) with a nice flash fallback. That’s it. They even let you chose from different flash players: JW player, FlowPlayer and FlashFox. Unfortunatly, the generated code hotlinks to the servers of JW player, FlowPlayer or GoogleCode, which might break if the files go offline, and it’s surely also a legal issue. Hotlinking is never cool. Beside, JW player is a commercial solution now and doesn’t offer a freely downloadable flash player anymore (afaik). FlashFox looks really good, but is broken in older IE versions, so it’s not an option. FlowPlayer has also become a commercial script, BUT they still offer a totally free version, free to download, free for private and commercial projects.

Further in the article we’ll create an improved, minimal version that uses only one .mp4 file, and runs completely with free open-source software that is locally hosted, but first let’s have a look on the output of the Video For Everybody Generator:

 

The fully crossbrowser-safe HTML5 video version with a flash fallback

First, let’s have a look on the “full” version that follows the official (hmm… is it really official ?) way to implement HTML5 video (with .mp4, .ogv and .webm video files), we’ll go on with the “minimal” version in a minute (sorry for the ugly line breaks): The encoding in the param tag is intented, it’s ugly but necessary. %2F stands for a slash /

<!-- "Video For Everybody" http://camendesign.com/code/video_for_everybody -->
<video controls="controls" 
       poster="http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg" 
       width="640" height="360">
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm" type="video/webm" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
    <object type="application/x-shockwave-flash" 
            data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" 
            width="640" height="360">
        <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />
        <param name="allowFullScreen" value="true" />
        <param name="wmode" value="transparent" />
        <param name="flashVars" 
               value="config={'playlist':['http%3A%2F%2Fsandbox.thewikies.com%2Fvfe-generator%2Fimages%2Fbig-buck-bunny_poster.jpg',{'url':'http%3A%2F%2Fclips.vorwaerts-gmbh.de%2Fbig_buck_bunny.mp4','autoPlay':false}]}" />
        <img alt="Big Buck Bunny" 
             src="http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg" 
             width="640" height="360" 
             title="No video playback capabilities, please download the video below" />
    </object>
</video>
<p>
    <strong>Download video:</strong> 
    <a href="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4">MP4 format</a> | 
    <a href="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv">Ogg format</a> | 
    <a href="http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm">WebM format</a>
</p>

We see a HTML5 video element with  sources in mp4, webm and ogv (note that mp4 comes first, as the first playable format is played by the browser, the rest is dismissed). The fourth “source” is a perfect implementation of an on-the-fly loading of a flash player, which then gets options passed. The fourth param is contains … well… params that contains the path to the .mp4 file we want to play (using the .mp4 for the flash player is the best option here) etc.. The img tag is a fallback in case flash fails. Please note that every flash player has a different syntax of the param tag values. Below the video tag we see some linke links for manual downloads of the file(s). The official page of the used free open-source Flash says “it’s a player for Firefox” but it works perfectly on any tested browser that supports Flash.

This is a quite good solution! I’ve seen this working in every tested browser, even on all not-extremely outdated mobile browsers (so it also works on Windows Phone 7).

Potential problem #1: Windows Phone 7 (and 7.5) comes with Internet Explorer Mobile 9 that plays .mp4 nativly. But there is an issue on that, the .mp4 video file needs to be encoded with H.264 (which is quite standard), other encoding formats will not work on Internet Explorer Mobile 9. See this official blog post for more information.

Potential problem #2: People without installed Flash will not see any videos in IE6, IE7 and IE8. But that’s not a real problem, as that’s the pure reality. There simply was never a possibility to view videos in these browsers without flash. I’m just saying this as I’ve worked for clients that seriously asked to “show videos in IE6 even for people that don’t want to install flash”.

 

The minimal, cleaned and 100% legal self-hosted version (the better one!)

Let’s reduce this code block to a minimum, make it legally untouchable, host everything locally and also get rid of any other video files than the .mp4! In the above example code the Flash player is loaded directly from Flowplayer’s servers, which is not future-proof (as they might take it offline) and might also be a legal issue. I’ve prepared this in the GitHub repository html5-video. Download the .zip now, extract it and put it on your server to follow the example below, which is the body content of the according demo.html:

<video controls="controls" poster="img/demo.jpg" width="640" height="360">
    <!-- .mp4 file for native playback in IE9+, Firefox, Chrome, Safari and most mobile browsers -->
    <source src="vid/demo.mp4" type="video/mp4" />
    <!-- flash fallback for IE6, IE7, IE8 and Opera -->
    <object type="application/x-shockwave-flash"
            data="swf/flowplayer-3.2.18.swf" 
            width="640" height="360">
        <param name="movie" value="swf/flowplayer-3.2.18.swf" />
        <param name="allowFullScreen" value="true" />
        <param name="wmode" value="transparent" />
        <!-- note the encoded path to the image and video files, relative to the .swf! -->
        <!-- more on that here: http://en.wikipedia.org/wiki/Percent-encoding -->
        <param name="flashVars"
               value="config={'playlist':['..%2Fimg%2Fdemo.jpg',
                             {'url':'..%2Fvid%2Fdemo.mp4','autoPlay':false}]}" />
        <!-- fallback image if flash fails -->
        <img src="img/demo.jpg" width="640" height="360" title="No Flash found" />
    </object>
</video>

Okay, what do we see ? We have a folder img containing the demo.jpg (that is used as a cover image), a folder vid containing the demo.mp4 video file and a folder swf that contains the flash player file(s), one is the player itself, the other is for the optional player controls (which is autloaded by the flash player). Don’t touch the swf folder unless you know what you do.

Everything here is basically self-explaining. The fourth param element inside the object element (which is the flash player) is interesting: It defines the options of the flash player, in this case only the paths of the .jpg and the .mp4 are interesting:

..%2Fimg%2Fdemo.jpg

looks weird, but is just the URL-encoded version of the path

../img/demo.jpg

More on that – including a URL encoder/decoder here on W3Schools.

Now we still have a fully crossbrowsersafe solution, completely fully locally hosted and 100% independent! All modern browsers will play the .mp4 natively, the rest will use flash to play the video. Users with totally outdated or exotic browsers that ALSO don’t have flash installed will not see the video. But, these users will in general not see any videos on the web, so we seriously don’t give a fuck here. The .mp4 file will play natively on IE9, IE10, IE11, Firefox, Chrome, Safari, iOS Safari, Android 4.4 Stock Browser, Blackberry’s browser, Opera Mobile, Chrome for Android, Firefox for Android and Internet Explorer Mobile 10. For a full list of supported browsers and versions check CanIUse.com – MPEG4/H264.

It will play back on Internet Explorer Mobile 9 when the file is encoded properly with the h264 codec (which is standard). To make it work with old Android versions (2.3+ < 4.4) it might need fixes, like described in this article. However, i’ve never had problems playing .mp4 in Android, even years ago. Also not that CanIUse says Firefox does only support mp4 playback “partially”, but as you can see in the demo on v4e.thewikies.com the mp4 playback on Firefox is excellent.

The only possible “problem”: Users with Opera Desktop that DON’T have Flash installed will not be able to see the video. Side-fact: Opera Mobile can play back nearly any video format, but Opera Desktop can not. But c’mon, who uses Opera ? And who of them hasn’t Flash installed ? If you really need to server native HTML5 video playback to these people, then add the according .ogv and/or .webm file (both will work for opera) with

<!-- note the .ogv format but the ogg mime type --> 
<source src="vid/demo.ogv" type="video/ogg" />

For transcoding .mp4 to .ogv and .webm any video converter will do, I can recommend Any Video Converter. The website looks weird, but the tool is worth the money. A free alternative is MIRO. However, you don’t need this when just using pure .mp4 playback.

 

“This does not work”

This works perfectly and has survived serveral high-traffic real-life scenarios. When testing, make sure you DON’T simply open a .html file (that lays on your desktop) containing the above code with a double-click.  This will not work due to flash’s security restrictions. Always make sure you are running in a real environment, like any server (that has an public accessable IP or domain) or on localhost/127.0.0.1! Again: Flash stuff will probably not work if you open a file directly from the operating system’s path, like D:\test\videoplayer.html.

To test the flash video player alone (without the HTML5 part) simply remove everything around the flash object.

 

Something to improve ?

If you think parts of this solution need work or fixing, then please comment or write a ticket on GitHub.

fallbackflashHTML5ieie10ie11ie6ie7ie8ie9video
Share this
nginx php 5.5

[Link] Set up Nginx with PHP 5.5 easily

As there are surprisingly no good tutorials on how to set up NGINX with PHP 5.5 properly, here’s an excellent

hiphop php

PHP’s HipHop outperforms PHP 5.5 with Zend OPCache and Nginx by 15-20 times

A very interesting benchmark on www.alexfu.it shows excellent comparisons of plain PHP on Nginx (PHP 5.3 I think) PHP on

GitHub finally introduces repo traffic stats

It has been a veeeery long time, but finally GitHub has introduced private traffic stats for all projects. To open

How major web companies (and banks) handle passwords quite wrong

There’s a very interesting “movement” in password handling going on for a long time, the basic idea is to encourage

hack-php

Facebook releases HipHop (HHVM) 3.0, adds mysqli and support for Hack language

Lots of movement in the PHP world these days! After releasing Hack (a dramatically improved fork of the entire PHP

phpstorm-8

PHPStorm 8 (early access version) released – for free

JetBrains have just released an Early Access version of the upcoming PHPStorm 8. In case you never worked with PHPStorm

Create a fast, perfect and bootable 1:1 Windows backup (full clone of HDD) for SSD migration

In this article I want to share a super-simple, extremely fast and totally free workflow with you that will create

O’Reilly’s Learning JavaScript Design Patterns by Addy Osmani for free

Addy Osmani has published his excellent book Learning JavaScript Design Patterns for free! You can read the entire book including

bitdeli git github stats

php-login goes #2 PHP script worldwide in BitDeli stats

Holy! I just checked the stats of my little php-login hobby project on BitDeli (the tracking service for GitHub) and

logo-internet-explorer

How to professionally test on old Internet Explorer versions

If you have (or want) to test your websites and applications on older versions of Internet Explorer, then there’s a

1/4

Categories

Search

github-logo-octocat
GitHub rolls out .PSD diff and viewing
All new features of WordPress 3.9 in this 2 minute video
the-php-login-project
How to install php-login-one-file on Ubuntu 12.04 LTS
laracon-2014-eu-amsterdam
Laracon 2013 – Kapil Verma: Engineering Complex Applications with Laravel 4 (40min video)
What’s new in PHPStorm 9
Increase your HTML / CSS coding speed with EMMET
How to setup a config-free WordPress, PHP and MySQL (for local development) in Windows 7 / 8 in under 3 minutes
ubuntu-14-04-lts lamp
How to install/setup a basic LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu 12.04 or Debian 7.0/7.1
Material Design – How Google designed Android L (7min video)
php
How to install/setup latest version of PHP 5.5 on Debian Wheezy 7.0/7.1/7.2 (and how to fix the GPG key error)
hiphop php
Vote for “Hack” for HipHop/HHMV support (future style PHP) in PHPStorm 8
Hacking ATMs – A conference talk about the current security state of Windows XP driven cash machines
How to install/setup latest version of PHPMyAdmin on Ubuntu 12.04 LTS (Precise Pangolin)
hack-php
Wow! Facebook devs have rewritten and fixed PHP, releasing it as new language called “Hack” today
sass laravel
Extremely easy SASS in Laravel (with pure PHP)

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy