Last active
August 29, 2015 14:01
-
-
Save lidio601/f62ea5cf7cf3d8eef0a1 to your computer and use it in GitHub Desktop.
PHP script to output an mjpeg video from a jpeg file directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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