Detect Supported Video Formats with JavaScript

By  on  
Detect Video Playback Over the past few years we've been seeing new audio, video, and image formats take shape to challenge the legacy formats that we've used since the web's inception.  This is a great development as we have more processing power and better compression algorithms have been developed, leading to faster load times and rendering.  Hooray for speed! The problem:  as with every other features added to the browser, some browsers get media format support faster than others, and some browsers refuse to support given formats.  Simply put:  we now need to do feature detection on media, something we've not traditionally had to do.  A while back I posted about how you can detect WEBP support in the browser and now I'd like to show you how to detect supported video formats -- it's easier than you think!

HTMLVideoElement.prototype.canPlayType

canPlayType is the secret to detecting video format support in the browser.  To use this method you must first create a <video> element instance:
const video = document.createElement('video');
You then call canPlayType, passing in the format's MIME type and additional details as needed:
video.canPlayType('video/webm; codecs="vp8, vorbis"'); // "probably"
There are three possible results from canPlayType:
  • "probably" : The media type appears to be playable
  • "maybe": Cannot tell if the media type is playable without playing it
  • "": The media type is not playable

Format Detection Function

Modernizr includes a small function to detect video format support; I've taken a moment to take that logic and create my own function for general purpose use without Modernizr:
function supportsVideoType(type) {
  // Allow user to create shortcuts, i.e. just "webm"
  const formats = {
    ogg: 'video/ogg; codecs="theora"',
    h264: 'video/mp4; codecs="avc1.42E01E"',
    webm: 'video/webm; codecs="vp8, vorbis"',
    vp9: 'video/webm; codecs="vp9"',
    hls: 'application/x-mpegURL; codecs="avc1.42E01E"'
  };

  const video = document.createElement('video');
  return video.canPlayType(formats[type] || type);
}

// Usage
if(supportsVideoType('webm') === "probably") {
  // Set the video to webm
}
else {
  // Set the video to mpeg or mp4
}
I use feature detection function efficiency to only create one element and simply return the result if called more than once.  Using this function today, Chrome reports the following: Detect Video Playback The canPlayType method allowed you to feature detect the best compression and supported formats and server them to your users, thus making the most of your browser capabilities.  Get aggressive with your media formats to make your site as quick as possible!

Recent Features

  • By
    Create a Sheen Logo Effect with CSS

    I was inspired when I first saw Addy Osmani's original ShineTime blog post.  The hover sheen effect is simple but awesome.  When I started my blog redesign, I really wanted to use a sheen effect with my logo.  Using two HTML elements and...

  • By
    I&#8217;m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Send Email Notifications for Broken Images Using MooTools AJAX

    One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible.

  • By
    MooTools 1.3 Browser Object

    MooTools 1.3 was just released and one of the big additions is the Browser object.  The Browser object is very helpful in that not only do you get information about browser type and browser versions, you can gain information about the user's OS, browser plugins, and...

Discussion

  1. Radek Havelka

    Thank you for this article and info, but I am sorry, this is insane. WTF MAYBE, PROBABLY means??? Why not TRUE, FALSE, null or whatever similar?

  2. Daniel

    hls returns false everywhere, except Edge. But works everywhere except IE…

  3. Fernando Valle

    This is a great tip! Does anyone know what the proper MIME type would be for a .mov file with HEVC/h.265 encoding for safari?

  4. Mad

    Am I missing something?

    function supportsVideoType(type) {
      let video; // <-- this is a local variable
    
      // Allow user to create shortcuts, i.e. just "webm"
      let formats = {
        ogg: 'video/ogg; codecs="theora"',
        h264: 'video/mp4; codecs="avc1.42E01E"',
        webm: 'video/webm; codecs="vp8, vorbis"',
        vp9: 'video/webm; codecs="vp9"',
        hls: 'application/x-mpegURL; codecs="avc1.42E01E"'
      };
    
      if(!video) { // <-- video will always be undefined, so !video will always be true
        video = document.createElement('video')
      }
    
      return video.canPlayType(formats[type] || type);
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!