Tag Archives: PHP

Using ffmpeg to create a video from image sequences

This PHP script will take a sequence of archive images stored in time-specific sub folders hh/mm and renumber them sequentially. Then use ffmpeg to convert the sequence into a video file. The operation is looped over an array of cameras and a staging folder is cleared prior to each video creation.

<?php
include_once("config.php");
$ftppath = "/root/path/to/images/";
$stagepath = $ftppath . 'staging/';
$date = date('ymd');

foreach ($cams as $cam) {
 exec('rm ' . $stagepath . '*');
 $camfile = $cam['fileroot'];
 $idx = 1;
 for ($j=0; $j<24; $j++)
 {
 for ($i=0; $i<60; $i++)
 {
 $n = sprintf("%02d/%02d", $j, $i);
 $pathfile = $ftppath . $n . '/' . $camfile . '_320x240.jpg';
 if (file_exists($pathfile)) {
 $sidx = sprintf("%04d", $idx);
 copy($pathfile, $stagepath . $camfile . $sidx . '.jpg');
 $idx++;
 }
 }
 }
 exec('ffmpeg -r 4 -i ' . $stagepath . $camfile . '%04d.jpg ' . $ftppath . $camfile . '_' . $date . '.mp4');
}
?>

Variable range scrollback for archive images

For a given image of class=’camview’  this jQuery javascript will scroll back a variable amount depending on the mouse location over the image.

The mouse over right edge of the image represents the most recent image from the archive. The bottom-left corner represents 24 hours ago. But to have finer granularity at reviewing more recent images, scrolling the mouse over the top of the image will scroll back one time-increment every 10 pixels. Moving the mouse from right to left over the bottom of the image will scroll back over a full 24 hours (1440 one-minute images) skipping frames as needed to fit the mouse resolution. Mouse heights between the top and bottom edges will have a linearly scaled proportion of archive history.

$('.camview').mousemove(function(event) {
 var x = event.pageX - $(this).offset().left;
 var y = event.pageY - $(this).offset().top;
 var mindx = 0.1;
 var maxdx = 1440.0 / $(this).width();
 var dy = 1.0 - y / $(this).height();
 var dx = dy * mindx + (1.0-dy) * maxdx;
 var arc = ($(this).width() - x) * dx;
 $(this).find('img').attr('src', 'camjpg.php?cam=' + $(this).attr('id') + '&arc=' + arc.toFixed(0));
});

Archiving thumnails of FTP camera images

This php script will iterate over an array $cams that has an element file that is the filename.

It uses the PHP Imagick package to resize the image and overlay a timestamp. The file timestamp is used so the time of the last ftp image is used, which may differ from the current time if the ftp has stalled. The image is then copied to an Hour/Minute specific folder for a 24 hour rolling archive.

$H = date('H');
$i = date('i');
$arcpath = $ftppath . $H . '/';
mkdir($arcpath);
$arcpath = $ftppath . $H . '/' . $i . '/';

foreach ($cams as $cam)
{
  $pathfile = $ftppath . $cam['file'];
  if (file_exists($pathfile)) {
    $thumbname = $cam['file'] . '_320x240';

    $thumb = new Imagick($pathfile);
    $thumb->scaleImage(320,240);

    $draw = new ImagickDraw();
    $draw->setFontSize(12);
    $draw->setFillColor(new ImagickPixel("#ffffff"));

    $draw->setTextAlignment(LEFT);
    $draw->annotation(5, 12,  date ("F d Y H:i:s", filemtime($pathfile)));
    $thumb->drawImage($draw);

    $thumb->setImageCompression(imagick::COMPRESSION_JPEG);
    $thumb->setImageCompressionQuality(80);
    $thumb->stripImage();
    $thumb->writeImage($ftppath . $thumbname);
    $thumb->clear();
    $thumb->destroy();
    copy($ftppath . $thumbname, $arcpath . $thumbname);
  }
}

Http proxy for accessing local area network images

To access multiple cameras on a local area network from a single web page, you need to proxy the requests from your web server. Since many netcams require BasicAuth to access images,

A PHP script to handle the proxy request can be found here: http://www.troywolf.com/articles/php/class_http/
To process a set of cameras, store the camera access data in an array such as this

$cams = array(
 "c30a" => array(
 "name" => "c30a",
 "type" => "http",
 "http" => "http://192.168.3.71/SnapshotJPEG?Resolution=640x480&Quality=Standard&Count=1",
 "http_user" => "<user>",
 "http_pass" => "<password>",
 "title" => "Backyard",
 "live" => "//192.168.3.71/CgiStart?page=Single&Language=0"
 ),
 "c20a" => array (
 "name" => "c20a",
 "type" => "http",
 "http" => "http://192.168.3.73/SnapshotJPEG?Resolution=640x480&Quality=Standard&Count=1",
 "http_user" => "<user>",
 "http_pass" => "<password>",
 "title" => "Garage",
 "live" => "//192.168.3.73/CgiStart?page=Single&Language=0"
 ),
}
?>

Include the above config.php in the php script that will handle the proxy request. It will look up the local area network destination from the ‘http’ element of the array, and pass the request along the camera, then copy the image returned from the camera to the caller.

url = $cam['http'];
 $h->postvars = $_POST;
 if (!$h->fetch($h->url, 0, "", $http_user, $http_pass)) {
   header("HTTP/1.0 501 Script Error");
   echo "proxy.php had an error attempting to query the url";
   exit();
 }

 $http_user = "";
 $http_pass = "";
 if (isset($cam['http_user'])) $http_user = $cam['http_user'];
 if (isset($cam['http_pass'])) $http_pass = $cam['http_pass'];

 $h->url = $cam['http'];
 $h->postvars = $_POST;
 if (!$h->fetch($h->url, 0, "", $http_user, $http_pass)) {
 header("HTTP/1.0 501 Script Error");
 echo "proxy.php had an error attempting to query the url";
 exit();
 }

 // Forward the headers to the client.
 $ary_headers = explode("\n", $h->header);
 foreach($ary_headers as $hdr) { header($hdr); }

 // Send the response body to the client.
 echo $h->body;
?>

To use this proxy from a web page, the javascript with jQuery would request an image from the same page as the main web site.

 $('#c30a img').attr('src', 'proxy.php?cam=c30a');