Jump to content

webcam - use PHP to grab mjpeg from webcam or a single frame


fsusr

Recommended Posts

Hello,
I have a webcam on my home LAN that can be accessed on the net.  The router port forwards the port XXX to the port number for the webcam's mjpeg stream.  The webcam doesn't have any password protection features, so I just pick a really difficult to guess port number :).

I also have a shared host account.  I'd like to use PHP to display the webcam (either as a single image or mjpeg stream).  This way, I'm hiding the IP address and port number of the my home webcam, and I can wrap the whole thing around a simple password prompt, and all the sensitive info would be on the server side.

I played with PHP on a home PC with nginx installed, and found this code works.  But when I use the same PHP code on my shared host, it doesn't.  The share host seems to be going to my home network and try to download the mjpeg stream (which would never complete) instead of stream it back to me real time.  That is my guess on what's happening anyways.  On the browser, all I see is the "waiting for host..." message.

So, I might switch strategies and instead of streaming the mjpeg stream, I would like a way to just grab a single frame of the mjpeg stream.  But I don't have a clue on how to do this.

Here's the code I've been using to grab the entire stream.  It works locally, but not when the code is on the shared host.  Locally, I just change the dynamic dns address to the IP of the webcam, and the port number to the internal port number of the webcam stream.  Also, I copied this code from another site, and have very little understanding of what the code is actually doing.


set_time_limit(0);
$fp = fsockopen ("xxxx.dyndns.org", 20804, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br>\n";
} else {
    fputs ($fp, "GET / HTTP/1.0\r\n\r\n");
    while ($str = trim(fgets($fp, 4096)))
       header($str);
    fpassthru($fp);
    fclose($fp);
    }

Thanks for any help.
 

Link to comment
Share on other sites

Your're probably corrupting the stream with the way you're doing it now. Also if the webcam never closes the stream, your script will never end, thus the reason why it's always "waiting for host."

 

What I would do is first of, try just outtputing the proper Content-type: header using header and then use readfile with the URL of the webcam. If that does not work, then you'd probably need to figure out how to extract a single jpeg from the stream and output that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.