Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidio601/f62ea5cf7cf3d8eef0a1 to your computer and use it in GitHub Desktop.
Save lidio601/f62ea5cf7cf3d8eef0a1 to your computer and use it in GitHub Desktop.
PHP script to output an mjpeg video from a jpeg file directory
<?php
$photos = array();
// $photos comes with a list of files (maybe with their absolute path)
// of images (here they are jpeg files!) to output
// http://en.wikipedia.org/wiki/Motion_JPEG
// https://github.com/berak/netcam/blob/master/netcam/main.cpp
ignore_user_abort(false);
set_time_limit(30);
$boundary='mjpeg'.md5(rand());
header("Server: Mozarella/2.2");
header("Accept-Range: bytes");
header("Connection: close");
header('Content-Type: multipart/x-mixed-replace; boundary='.$boundary);
$j=0;
$buf = array();
// while the connection is up
// output the video in loop mode
// each image frame is repeated twice
while(count($photos)) {
if(!isset($buf[$j])) {
$buf[$j] = @file_get_contents(end(explode(":",$photos[$j])));
}
for($i=0;$i<2;$i++) {
echo "--".$boundary."\r\n";
echo 'Content-Type: image/jpeg'."\r\n";
echo 'Content-Description: '.basename($photo_name)."\r\n";
echo 'Content-Length: '.strlen($buf[$j])."\r\n";
echo "\r\n".$buf[$j]."\r\n\r\n";
}
// Did the connection fail?
if(connection_status() != CONNECTION_NORMAL) {
break;
}
$j = ( $j + 1 ) % count($photos);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment