Get jpeg-frame from mjpeg stream with PHP

by divide on July 8th, 2009

Code opens mjpeg stream and grabs one frame of it. After that it outputs the image.

Syntax: PHP - TinyURL for this snippet: http://tinyurl.com/ld3mpd
Show lines - Hide lines - Show in textbox - Download
<?
$camurl="http://myhost:8080/cameras/1?q=40";
$boundary="\n--";
$f = @fopen($camurl,"r") ;
   if(!$f)
   {
        //**** cannot open
        echo "error";
   }
    else
  {
        //**** URL OK
         while (substr_count($r,"Content-Length") != 2) $r.=@fread($f,512);
         $start = strpos($r,'ΓΏ');
         $end   = strpos($r,$boundary,$start)-1;
         $frame = substr("$r",$start,$end - $start);
         header("Content-type: image/jpeg");
         echo $frame;
   }
fclose($f);
?>

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS