使用php和curl将Mjpeg转换为jpg
我想在摄像机拍摄东西的视频html标签中显示视频流 我必须通过身份验证连接到相机,所以我使用curl。我设法做到了这一点,并检索到了流。 现在我必须治疗它。 我得到这个流的JPEG图像。除此之外,无法转换图片中的数据 下面是显示一幅图像的部分测试代码:使用php和curl将Mjpeg转换为jpg,php,html,curl,video-streaming,mjpeg,Php,Html,Curl,Video Streaming,Mjpeg,我想在摄像机拍摄东西的视频html标签中显示视频流 我必须通过身份验证连接到相机,所以我使用curl。我设法做到了这一点,并检索到了流。 现在我必须治疗它。 我得到这个流的JPEG图像。除此之外,无法转换图片中的数据 下面是显示一幅图像的部分测试代码: $_SESSION['img'] = ""; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://$ip/cgi-bin/mjpg/video.cgi'); curl_setopt
$_SESSION['img'] = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://$ip/cgi-bin/mjpg/video.cgi');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, '$user:$pass');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $str) use ($i) {
$pos = strpos($str, '--myboundary');
if ($pos !== FALSE && strlen($_SESSION['img']) > 0) {
//Complete the image data
$imagePart = substr($str, 0, $pos-1);
$_SESSION['img'] .= $imagePart;
//Parse data
$imgstr = str_replace("--myboundary\r\n", '', $_SESSION['img']);
$imgstr = str_replace("Content-Type: image/jpeg\r\n", '', $imgstr);
$imgstr = str_replace("Content-Length: ", '', $imgstr);
$imgstr = preg_replace("/\d+\r\n\r\n/", '', $imgstr);
//save in file - file not loaded
file_put_contents('img/test.jpg', $imgstr);
//generate image - doesn't work
$im = imagecreatefromstring($imgstr);
if ($im !== false) {
header("Content-Type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
}
session_destroy();
die();
}
$_SESSION['img'] .= $str;
return strlen($str);
});
$output = curl_exec($ch);
curl_close($ch);
如果你有想法,让我知道
多谢各位