Page 1 of 2

PHP Script : Create yesterday Events to mpeg one file

Posted: Sat Jun 13, 2009 8:13 pm
by X-Files

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 !!!";

?>

Posted: Tue Jun 16, 2009 5:39 am
by kingofkya
not trying to discount what you did but zoneminder already does this

see filters

Filters do not create _one_ mpeg

Posted: Mon Jun 22, 2009 9:09 pm
by ekiboy69
If the code does what the topic says is does something quite different compared to filters. Filters create one video file per event. If this really creates one big mpeg file it is truly a different thing IMHO.

Re: Filters do not create _one_ mpeg

Posted: Mon Jun 22, 2009 9:13 pm
by robi
ekiboy69 wrote:If the code does what the topic says is does something quite different compared to filters. Filters create one video file per event. If this really creates one big mpeg file it is truly a different thing IMHO.
Agree.

Posted: Fri Oct 16, 2009 11:18 pm
by trendkill
great!
how can I run this script?

/usr/bin/php <filename> command writes the code to the terminal...

Posted: Sun Oct 18, 2009 12:55 am
by whatboy
I think it would be better to create a crontab entry and run it every day say at 00:10

Like if you run the gui with one user lets say... watcher

copy the script and put it where the user can execute it... like /home/watcher/bin

10 00 * * * umask 002; /home/watcher/bin/mkmovie


Am I right???

Hmmm... guess NOT... ok, so how do you execute this code??? I try put ./mkmovie and it just text out on the terminal... :(

Posted: Sun Oct 18, 2009 11:34 pm
by trendkill
yes this is the main problem
I don't know how php works, but i have to use this script... :)

whatboy wrote: Hmmm... guess NOT... ok, so how do you execute this code??? I try put ./mkmovie and it just text out on the terminal... :(

Posted: Thu Oct 22, 2009 10:39 pm
by trendkill
Anybody?????

trendkill wrote:yes this is the main problem
I don't know how php works, but i have to use this script... :)

whatboy wrote: Hmmm... guess NOT... ok, so how do you execute this code??? I try put ./mkmovie and it just text out on the terminal... :(

Posted: Sat Oct 24, 2009 7:31 am
by bazso
Hi,

Nice work, only the fix 30FPS in the ffmpeg parameters seems a bit problematic (Honestly I haven't tried the script yet), because how can I be sure that all events have this frame rate?

Kind Regards,
BaZso

Posted: Tue Oct 27, 2009 6:39 pm
by whatboy
Anyone??? any tips on how to run this script??? Pretty please!!!

Posted: Tue Oct 27, 2009 10:33 pm
by kwire
whatboy and trendkill,

I am not a PHP programmer either, but what have you done to execute the script?

I did not try the script as presented, but I have run a few scripts before... Try to follow the instructions hereand report back.

Keith

Posted: Wed Oct 28, 2009 12:34 am
by whatboy
I think that script is incomplete or part of another script... I check another script and it need to define something like this...

# Include from system perl paths only
use ZoneMinder::Base qw(:all);
use ZoneMinder::Config qw(:all);
use ZoneMinder::Debug qw(:all);
use ZoneMinder::General qw(:all);
use ZoneMinder::Database qw(:all);
use ZoneMinder::ConfigAdmin qw( :functions );

or sumtin!!! I think!!!

Posted: Wed Oct 28, 2009 12:45 am
by whatboy
DAMN IT... the script sounds soo good to not have it!!! F...

Posted: Thu Oct 29, 2009 1:12 am
by whatboy
Ok. I PM X-Files, we just have to wait and see if he responds...

Posted: Thu Oct 29, 2009 1:22 pm
by arielf
Question: Generates a video with all cameras or video monitor set individually for each?