avconv webcam image to standard output for using in php

434 views Asked by At

I try to get a webcam image direct to my PHP script on a debian system. For this, I tried to open a file handling to /dev/video0, but that doesn't work. Using the software "streamer" I got an image to the disk, the webcam works on /dev/video0

I don't want to save the image to the disk first, because I need to refresh it in a short interval. My idea is to get an image direct to the standard output and use php passthru to pipe the output to the client browser:

$header("Content-type: image/jpeg");
passthru('avconv /dev/video0 someparamters for direct output');

I hoped that avconv has the option to push the image to the standard output, but I could not find any option like this. Is there a possibility to get the webcam image (via avconv or another tool) direct in php as an binary stream?

Thanks a lot! Sebastian

2

There are 2 answers

0
Megasaturnv On BEST ANSWER

This works without the need of a temporary file:

<?php
header("content-type: image/jpeg");
echo passthru("avconv -f video4linux2 -i /dev/video0 -vframes 1 -s 1280x1024 pipe:.jpg 2>/dev/null");
?>

(from https://gist.github.com/megasaturnv/a42ed77d3d08d0d3d91725dbe06a0efe)

This also works in an img tag: https://gist.github.com/megasaturnv/6e5965732d4cff91f2e976e7a39efbaa

0
Juan Carlos Carrillo On

The only way I found was save to a file, then 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";
}