PHP Script : Create yesterday Events to mpeg one file
Posted: Sat Jun 13, 2009 8:13 pm
Code: Select all
#!/usr/bin/php
</Version>/dev/null 2>/dev/null
Tested in 1.24.1 - OK
*/
$zm_events_path = "/home/web/www.site.com/zm/events";
$zm_tmp_path = "/tmp/zm_tmp";
$zm_archive_video_path = "/home/web/www.site.com/zm/ARCHIVE";
// tmp
$array_tmp_monitor = array();
$YESTERDAY=date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-1, date("Y")) );
$STARTTIME="$YESTERDAY 00:00:00";
$ENDTIME="$YESTERDAY 23:59:59";
// end tmp
$sql_host = "localhost";
$sql_login = "zmuser";
$sql_pass = "zmpass";
$sql_base = "zm";
// SQL connect
if (!@mysql_connect($sql_host, $sql_login, $sql_pass)) die("Can't connect to MySQL databse");
if (!@mysql_select_db($sql_base)) die("Can't use database : $sql_base");
function scanDirectories($rootDir) {
$invisibleFileNames = array(".", "..");
$dirContent = scandir($rootDir);
$allData = array();
$fileCounter = 0;
foreach($dirContent as $key => $content) {
$path = $rootDir.'/'.$content;
if(!in_array($content, $invisibleFileNames)) {
if(is_file($path) && is_readable($path)) {
$tmpPathArray = explode("/",$path);
$allData[$fileCounter]['fileName'] = end($tmpPathArray);
$allData[$fileCounter]['filePath'] = $path;
$filePartsTmp = explode(".", end($tmpPathArray));
$allData[$fileCounter]['fileExt'] = end($filePartsTmp);
$fileCounter++;
}elseif(is_dir($path) && is_readable($path)) {
$dirNameArray = explode('/',$path);
$allData[$path]['dirPath'] = $path;
$allData[$path]['dirName'] = end($dirNameArray);
$allData[$path]['content'] = scanDirectories($path);
}
}
}
return $allData;
}
function number_pad($number,$n) {
return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
}
$sql_DB_Events=mysql_query("
SELECT MonitorId,Id,Name FROM Events
WHERE StartTime > '".$STARTTIME."' and StartTime <= '".$ENDTIME."'
ORDER BY MonitorID,Id
");
$n_DB_Events=mysql_num_rows($sql_DB_Events);
for ($i_DB_Events=0;$i_DB_Events<n_DB_Events> $content) {
if ($content[fileExt] != 'jpg') {continue;}
$array_tmp_monitor[$row_DB_Events[MonitorId]]=$array_tmp_monitor[$row_DB_Events[MonitorId]]+1;
$dir_from = $content[filePath];
$dir_to = $zm_tmp_path."/".$row_DB_Events[MonitorId]."/".number_pad($array_tmp_monitor[$row_DB_Events[MonitorId]],3).".jpg";
rename($dir_from,$dir_to);
}
system("rm -rf ".$zm_events_path."/".$row_DB_Events[MonitorId]."/".$row_DB_Events[Id]);
}
foreach($array_tmp_monitor as $key => $content) {
if (!is_dir($zm_archive_video_path)){mkdir ($zm_archive_video_path, 0777);print "Status: Dir Added ".$zm_archive_video_path." !!! \n";}
if (!is_dir($zm_archive_video_path."/".$key)){mkdir ($zm_archive_video_path."/".$key, 0777);print "Status: Dir Added ".$zm_archive_video_path."/".$key." !!! \n";}
if (!is_dir($zm_archive_video_path."/".$key."/".$YESTERDAY)){mkdir ($zm_archive_video_path."/".$key."/".$YESTERDAY, 0777);print "Status: Dir Added ".$zm_archive_video_path."/".$key."/".$YESTERDAY." !!! \n";}
system("ffmpeg -y -i ".$zm_tmp_path."/".$key."/%03d.jpg -s 640x480 -r 30 ".$zm_archive_video_path."/".$key."/".$YESTERDAY."/cam_nr-".$key."_date".$YESTERDAY.".mpg");
system("rm -rf ".$zm_tmp_path."/".$key);
}
echo "END !!!";
?>