PHP Script : Create yesterday Events to mpeg one file

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
X-Files
Posts: 1
Joined: Sat Jun 13, 2009 5:33 pm

PHP Script : Create yesterday Events to mpeg one file

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

?>
User avatar
kingofkya
Posts: 1110
Joined: Mon Mar 26, 2007 6:07 am
Location: Las Vegas, Nevada

Post by kingofkya »

not trying to discount what you did but zoneminder already does this

see filters
ekiboy69
Posts: 101
Joined: Tue Jun 22, 2004 9:26 pm
Location: Kaarina-Finland

Filters do not create _one_ mpeg

Post 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.
User avatar
robi
Posts: 477
Joined: Sat Mar 17, 2007 10:48 am

Re: Filters do not create _one_ mpeg

Post 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.
v1.25.0 + Ubuntu Linux 12.04 Server
trendkill
Posts: 35
Joined: Sat Mar 10, 2007 1:48 pm

Post by trendkill »

great!
how can I run this script?

/usr/bin/php <filename> command writes the code to the terminal...
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Post 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... :(
trendkill
Posts: 35
Joined: Sat Mar 10, 2007 1:48 pm

Post 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... :(
trendkill
Posts: 35
Joined: Sat Mar 10, 2007 1:48 pm

Post 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... :(
bazso
Posts: 11
Joined: Wed May 14, 2008 6:50 pm

Post 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
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Post by whatboy »

Anyone??? any tips on how to run this script??? Pretty please!!!
kwire
Posts: 48
Joined: Mon Jan 05, 2009 12:56 am
Location: Ada, Ohio, USA

Post 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
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Post 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!!!
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Post by whatboy »

DAMN IT... the script sounds soo good to not have it!!! F...
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Post by whatboy »

Ok. I PM X-Files, we just have to wait and see if he responds...
arielf
Posts: 83
Joined: Thu May 14, 2009 1:24 pm
Location: Argentina

Post by arielf »

Question: Generates a video with all cameras or video monitor set individually for each?
Post Reply