我尝试将网络摄像头图像直接获取到 debian 系统上的 PHP 脚本。为此,我尝试打开一个处理 /dev/video0 的文件,但这不起作用。使用软件“streamer”我将图像保存到磁盘上,网络摄像头在 /dev/video0 上工作
我不想先将图像保存到磁盘,因为我需要在短时间内刷新它。我的想法是将图像直接发送到标准输出,并使用 php passthru 将输出通过管道传输到客户端浏览器:
$header("Content-type: image/jpeg");
passthru('avconv /dev/video0 someparamters for direct output');
我希望avconv有将图像推送到标准输出的选项,但我找不到这样的选项。是否有可能直接在 php 中以二进制流形式获取网络摄像头图像(通过 avconv 或其他工具)?
非常感谢! 塞巴斯蒂安
这无需临时文件即可工作:
<?php
header("content-type: image/jpeg");
echo passthru("avconv -f video4linux2 -i /dev/video0 -vframes 1 -s 1280x1024 pipe:.jpg 2>/dev/null");
?>
(来自 https://gist.github.com/megasaturnv/a42ed77d3d08d0d3d91725dbe06a0efe)
这也适用于 img 标签:https://gist.github.com/megasaturnv/6e5965732d4cff91f2e976e7a39efbaa
我发现的唯一方法是保存到文件,然后 file_get_contents
$filename = "snapshot.jpg";
system("avconv -f video4linux2 -i /dev/video0 -vframes 1 -s 1280x800 ".$filename);
if( file_exists( $filename ) ){
header("content-type: image/jpeg");
echo file_get_contents( $filename );
} else {
echo "Error loading the snapshot";
}