Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect supported video formats for the HTML5 video tag?

Tags:

I am making an application in HTML5 using the video tag, in the application the user chooses a video file and I play that file. This all happens locally because I only link to that file in the user's machine.

I want to allow only formats the browser can play to be played in my application, and to show an error for unsupported formats. The problem is that different browsers can play different formats.

I know I can check for the browser and match it with the formats I know it can play, but what if the browser updates to support another format? I'll have to update my application with the new information and meanwhile users won't be able to play supported formats. Is there a way to check just for the supported video formats?

like image 680
Cokegod Avatar asked Sep 17 '11 00:09

Cokegod


People also ask

Which video format is supported by HTML5?

The HTML5 video format capabilities include three options to play: MP4, WebM, and Ogg.

Which video formats are supported by the video tag?

There are three supported video formats in HTML: MP4, WebM, and OGG.

Is video tag supported in HTML5?

The <video> tag is supported in all major modern browsers.

Is MP4 supported by HTML5?

The minimum for HTML5 video is MP4 + WebM or Ogg (or both), using the MP4 version for Flash fallback. 2. For mobile support, one H. 264/MP4 output can take you a long way.


1 Answers

You can check codecs for different video types with HTMLVideoElement.prototype.canPlayType. There is also a great HTML5 feature detection library, Modernizr.

var testEl = document.createElement( "video" ),     mpeg4, h264, ogg, webm; if ( testEl.canPlayType ) {     // Check for MPEG-4 support     mpeg4 = "" !== testEl.canPlayType( 'video/mp4; codecs="mp4v.20.8"' );      // Check for h264 support     h264 = "" !== ( testEl.canPlayType( 'video/mp4; codecs="avc1.42E01E"' )         || testEl.canPlayType( 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"' ) );      // Check for Ogg support     ogg = "" !== testEl.canPlayType( 'video/ogg; codecs="theora"' );      // Check for Webm support     webm = "" !== testEl.canPlayType( 'video/webm; codecs="vp8, vorbis"' ); } 
like image 162
Will Avatar answered Sep 19 '22 13:09

Will