Recording live broadcast from an IP camera (MJPEG compression)

I have a live stream from Tenvis IP camera through HTTP streaming and its compression mjpeg.

I am trying to save it to a file and I tried using php for this. my code is as follows:

<?php $input = fopen("http://xxx.xxx.xxx.xxx:81/videostream.cgi?user=user&pwd=admin&resolution=8"); $output = fopen("video.mpg", "c+"); $end = time() + 60; do { fwrite($output, (fread($input, 30000)), 30000); } while (time() <= $end); fclose($output); fclose($input); echo "<h1>Recording</h1>"; ?> 

The code I create but writes nothing. Any suggestions would be appreciated.

+4
source share
2 answers

According to the MJPEG Wikipedia page ( http://en.wikipedia.org/wiki/Motion_JPEG#M-JPEG_over_HTTP ), the MJPEG stream via HTTP is basically a sequence of JPEG frames followed by a special mime type. To capture them and save them to a video file, I'm not sure if you can just write the incoming data into a .mpg file and have a working video.

Honestly, I'm not quite sure why your script doesn’t write anything at all, but I came across the following page, which, although written for specific software, contains examples of how to capture MJPEG and pass it to the browser: http: // www. lavrsen.dk/foswiki/bin/view/Motion/MjpegFrameGrabPHP

You can try one of your examples and save it to a file instead of transferring it to the browser. You can see that they read one image at a time:

 while (substr_count($r,"Content-Length") != 2) $r.=fread($input,512); $start = strpos($r,'ÿ'); $end = strpos($r,$boundary,$start)-1; $frame = substr("$r",$start,$end - $start); 

If this fixes the capture part of the stream but does not save it as video, another option is to save all the frames separately as a JPEG file and then stitch them together with a tool like ffmpeg to create the video: Image sequence for quality video

Update If you decide to take the ffmpeg road, you can also capture the stream with ffmpeg only. See this question for an example.

Hope this helps.

+1
source

In most cases, when the camera supports mjpeg, it also supports rtsp, and therefore you may want to continue this as a solution for what you are trying to accomplish. At the same time, it is quite easy to record using an application such as VLC.

0
source

Source: https://habr.com/ru/post/1498860/


All Articles