Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a browser supports MJPEG?

Modern browsers except IE handle MJPEG (Motion JPEG). Here is an example fiddle.

Can I detect support for MJPEG? I have looked through Modernizr in vain.

like image 365
Randomblue Avatar asked Dec 15 '11 14:12

Randomblue


3 Answers

Modernizr only supports the following formats for detection right now: ogg, webm and h264.

The video element has a call called canPlayType(format) that would really be your only option (if it works for mjpg). Your detection logic would look something like this (not the format would be different).

var videoElement = document.createElement('video');
if(!!videoElement.canPlayType)
{
  var browserConfidence = videoElement.canPlayType('video/mjpeg; codecs="insert, them"');
  if(browserConfidence == "probably")
  {
    // high confidence
  }
  else if(browserConfidence == "maybe")
  {
    // low confidence
  }
  else
  {
    // no confidence... it definately will not play
  }
}

Make sure you visit the W3C's information on canPlayType. It looks like the mime type should be "video/mjpeg" and not "video/mjpg" as you specified earlier.

like image 186
scottheckel Avatar answered Oct 19 '22 09:10

scottheckel


I've tried the most obvious way to detect if the image could be loaded or not:

$output = $('<img id="webcam">')
        .attr('src', src)
        .load(function(){alert('ok')})
        .error(function(){alert('error')});

In case image could be loaded load event will be fired, otherwise error. Checked this in recent Chrome and IE8. Works as expected.

like image 27
bjornd Avatar answered Oct 19 '22 09:10

bjornd


Sadly for this you would need to use an ActiveX control to support mjpg in IE. See How to embed mjpeg file on a webpage.

like image 1
Todd Baur Avatar answered Oct 19 '22 08:10

Todd Baur