[Phphtmllib-devel] SF.net SVN: phphtmllib:[3299] trunk/open2300/lib/core
Status: Beta
Brought to you by:
hemna
From: <he...@us...> - 2009-11-25 22:23:30
|
Revision: 3299 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3299&view=rev Author: hemna Date: 2009-11-25 22:23:15 +0000 (Wed, 25 Nov 2009) Log Message: ----------- added Added Paths: ----------- trunk/open2300/lib/core/daemon/ trunk/open2300/lib/core/daemon/AviProcessorDaemon.inc Added: trunk/open2300/lib/core/daemon/AviProcessorDaemon.inc =================================================================== --- trunk/open2300/lib/core/daemon/AviProcessorDaemon.inc (rev 0) +++ trunk/open2300/lib/core/daemon/AviProcessorDaemon.inc 2009-11-25 22:23:15 UTC (rev 3299) @@ -0,0 +1,222 @@ +<?php + +/** + * This class is responsible for processing + * the daily dir of images into an flv + * and shipping it off to the hosting provider + * where wx.hemna.com lives. + * + * + * @author waboring + * + */ +class AviProcessorDaemon extends Daemon { + + /** + * User ID + */ + protected $user_id = 1000; + + /** + * Group ID + */ + protected $group_id = 100; + + /** + * how long to wait in between + * processing. in seconds + */ + protected $wait_period = 5; + + + /** + * the log object. + */ + protected $log; + + protected $imagesdir; + + + /** + * Do any initialization here. + * + */ + protected function init() { + $this->log = new Log(); + $this->log->add_writer(FileLogWriter::factory(realpath($GLOBALS["path_base"])."/logs/daemon.log")); + + $config = Config::singleton(); + $this->imagesdir = $config->get("imagesdir"); + } + + /** + * any cleanup here + */ + protected function cleanup() { + + } + + /** + * This method is used to pause the scheduler so + * we aren't constantly running as fast as we can. + * + */ + protected function wait_state() { + $this->log->debug("::wait_state() sleeping for ".$this->wait_period." seconds"); + sleep($this->wait_period); + } + + + /** + * do the actual work here. + * + */ + protected function do_task() { + //now do a wait state + //so we aren't constantly running. + $this->wait_state(); + + //used for directory + $ymdDate = date("Ymd"); + + $hour = date("G"); + + $dirname = $this->imagesdir."/".$ymdDate; + //make sure the images dir exists. + $this->test_images_dir($dirname); + + + $num_images = $this->count_images($dirname); + if ($num_images == 0) { + //we have nothing to do + $this->log->info("::do_task() found no images to process"); + } else { + $this->log->info("::do_task() found ".$num_images." to process into a movie."); + $rate = $this->calculate_rate($num_images); + + $this->log->info("::do_task() rate is ".$rate." -> movie length = ".$num_images/$rate."s"); + //generate the avi + $cmd = "/home/waboring/bin/imgs2avi.sh -w 640 -h 480 -r ".$rate; + $this->exec_command($dirname, $cmd); + + //now convert the avi movie to a flash file + $aviname = "timelapse_640x480_".$rate."fps.avi"; + $cmd = "/usr/bin/ffmpeg -y -i ".$aviname." -b 1900k -f flv timelapse.flv"; + $this->exec_command($dirname, $cmd); + + //now send it to the hosting provider + $cmd = "/usr/bin/scp timelapse.flv wx.hemna.com:wx.hemna.com/htdocs/video/".$ymdDate; + $this->exec_command($dirname, $cmd); + } + + $this->log->info("Exiting"); + exit; + } + + /** + * This is used to execute an external command + * + */ + protected function exec_command($dirname, $cmd) { + $command = "cd ".$dirname."; ".$cmd." 2>&1 >>".$dirname."/processing.log"; + $this->log->info("::do_task() exec ".$command); + $last_line = system($command, $return); + } + + /** + * calculate the frame rate to use based + * on the # of images we have to process. + * + * @param $numimages + * @return rate + */ + protected function calculate_rate($num_images) { + $rate = 30; + if ($num_images < 10) { + $rate = 1; + } else if ($num_images < 100) { + $rate = 10; + } else if ($num_images < 400) { + $rate = 15; + } else if ($num_images < 800) { + $rate = 20; + } else if ($num_images < 1200) { + $rate = 25; + } + + return $rate; + } + + + + + /** + * Find out how many images are in the dir + * + * @return int + */ + protected function count_images($dirname) { + $count = 0; + + $this->log->debug("::count_images() : check ".$dirname); + if (file_exists($dirname)) { + if ($dir = opendir($dirname)) { + while (($file = readdir($dir)) !== false) { + if (strstr($file, '.jpg')) { + $count++; + } + } + closedir($dir); + } + } + + $this->log->debug("::count_images() : found ".$count); + return $count; + } + + + /** + * make sure the video dir exists. + * + */ + protected function test_images_dir($dirname) { + if (!file_exists($dirname)) { + $this->create_img_dir($dirname); + } + } + + /** + * Create the image directory + * (this is also done from the camera) + * + */ + protected function create_img_dir($dirname) { + $this->log->info("Creating directory (".$dirname.")"); + $ret = mkdir($dirname); + if (!$ret) { + $ex = new DaemonException("couldn't create directory (".$dirname.")"); + $this->log->err("Failed to creat directory", $ex); + throw $ex; + } + } + + + + + /** + * Create the process id file on disk + * so that we can store it for later use. + * + * @return string + */ + public function generate_pid_filename() { + return realpath($GLOBALS["path_base"]).'/bin/'.__CLASS__.'.pid'; + } + + protected function log_message($message, + $level=0, + $e=NULL) { + } +} + +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |