コード例 #1
0
ファイル: ThumbQueue.php プロジェクト: hackingman/TubeX
 public static function Run()
 {
     chdir(realpath(dirname(__FILE__) . '/../'));
     require_once 'includes/global.php';
     $doc_root = Config::Get('document_root');
     $DB = GetDB();
     self::Log('Starting...');
     self::MarkRunning();
     while (true) {
         // See if we were requested to stop
         if (self::ShouldStop()) {
             self::Log('User requested stop...');
             break;
         }
         self::Ping();
         $DB->Connect();
         $queue_item = $DB->Row('SELECT * FROM `tbx_thumb_queue` ORDER BY `queued` LIMIT 1');
         if (!empty($queue_item)) {
             $video = $DB->Row('SELECT * FROM `tbx_video` WHERE `video_id`=?', array($queue_item['video_id']));
             if (!empty($video)) {
                 $DB->Update('UPDATE `tbx_thumb_queue` SET `date_started`=? WHERE `video_id`=?', array(Database_MySQL::Now(), $video['video_id']));
                 $clips = $DB->FetchAll('SELECT * FROM `tbx_video_clip` WHERE `video_id`=? AND `type`!=? ORDER BY `clip_id`', array($queue_item['video_id'], 'Embed'));
                 $dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id']));
                 Video_FrameGrabber::SetLogFile($dir->GetBaseDir() . '/thumbnailer.log');
                 $thumb_start = time();
                 try {
                     if (!empty($clips)) {
                         $thumbs = array();
                         $duration = 0;
                         // Number of thumbs to create per clip
                         $amount = round(Config::Get('thumb_amount') / count($clips));
                         // Move existing thumbnails
                         $dir->MoveFiles($dir->GetThumbsDir(), $dir->GetTempDir(), JPG_EXTENSION);
                         // Process each clip
                         foreach ($clips as $clip) {
                             self::Ping();
                             // Remote video
                             if (preg_match('~https?://~i', $clip['clip'])) {
                                 $http = new HTTP();
                                 if ($http->Get($clip['clip'], $clip['clip'])) {
                                     $video_file = $dir->AddOriginalFromVar($http->body, File::Extension($clip['clip']));
                                     $vi = new Video_Info($video_file);
                                     $vi->Extract();
                                     $duration += $vi->length;
                                     $temp_thumbs = Video_FrameGrabber::Grab($video_file, $dir->GetProcessingDir(), $amount, Config::Get('thumb_quality'), Config::Get('thumb_size'), $vi);
                                     // Move generated thumbs from the processing directory
                                     foreach ($temp_thumbs as $temp_thumb) {
                                         $thumbs[] = $dir->AddThumbFromFile($temp_thumb);
                                     }
                                     @unlink($video_file);
                                 }
                             } else {
                                 $temp_thumbs = Video_FrameGrabber::Grab($doc_root . '/' . $clip['clip'], $dir->GetProcessingDir(), $amount, Config::Get('thumb_quality'), Config::Get('thumb_size'));
                                 // Move generated thumbs from the processing directory
                                 foreach ($temp_thumbs as $temp_thumb) {
                                     $thumbs[] = $dir->AddThumbFromFile($temp_thumb);
                                 }
                             }
                         }
                         // Get the relative URL for each thumb and add to database
                         $thumb_ids = array();
                         foreach ($thumbs as $thumb) {
                             $thumb = str_replace($doc_root, '', $thumb);
                             $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video['video_id'], 'thumbnail' => $thumb));
                         }
                         // Determine number of thumbnails and select random display thumbnail
                         $num_thumbnails = count($thumbs);
                         $display_thumbnail = null;
                         if ($num_thumbnails > 0) {
                             // Select display thumbnail randomly from the first 40%
                             $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
                         }
                         $update = array('video_id' => $video['video_id'], 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail);
                         if (empty($video['duration']) && !empty($duration)) {
                             $update['duration'] = $duration;
                         }
                         DatabaseUpdate('tbx_video', $update);
                         // Remove old thumbnails
                         $DB->Update('DELETE FROM `tbx_video_thumbnail` WHERE `video_id`=?' . (!empty($thumb_ids) ? ' AND`thumbnail_id` NOT IN (' . join(',', $thumb_ids) . ')' : ''), array($video['video_id']));
                         $dir->ClearTemp();
                     }
                 } catch (Exception $e) {
                     // Restore old thumbnails
                     $dir->MoveFiles($dir->GetTempDir(), $dir->GetThumbsDir(), JPG_EXTENSION);
                     Video_FrameGrabber::Log($e->getMessage() . (strtolower(get_class($e)) == 'baseexception' ? $e->getExtras() : '') . "\n" . $e->getTraceAsString());
                     self::UpdateStatsProcessed($thumb_start, $thumb_end, $queue_item['queued'], true);
                 }
                 $thumb_end = time();
                 $DB->Update('DELETE FROM `tbx_thumb_queue` WHERE `video_id`=?', array($queue_item['video_id']));
                 self::UpdateStatsProcessed($thumb_start, $thumb_end, $queue_item['queued']);
             }
         } else {
             break;
         }
     }
     self::MarkStopped();
     self::Log('Exiting...');
 }
コード例 #2
0
ファイル: ConversionQueue.php プロジェクト: hackingman/TubeX
 public static function Run()
 {
     chdir(realpath(dirname(__FILE__) . '/../'));
     require_once 'includes/global.php';
     $doc_root = Config::Get('document_root');
     $DB = GetDB();
     self::Log('Starting...');
     self::MarkRunning();
     while (true) {
         // See if we were requested to stop
         if (self::ShouldStop()) {
             self::Log('User requested stop...');
             break;
         }
         self::Ping();
         $DB->Connect();
         $queue_item = $DB->Row('SELECT *,`tbx_conversion_queue`.`video_id` AS `video_id`,`tbx_conversion_queue`.`queued` AS `queued` FROM `tbx_conversion_queue` LEFT JOIN ' . '`tbx_thumb_queue` USING (`video_id`) WHERE `tbx_thumb_queue`.`video_id` IS NULL ORDER BY `tbx_conversion_queue`.`queued` LIMIT 1');
         if (!empty($queue_item)) {
             $video = $DB->Row('SELECT * FROM `tbx_video` WHERE `video_id`=?', array($queue_item['video_id']));
             if (!empty($video)) {
                 $DB->Update('UPDATE `tbx_video` SET `conversion_failed`=0 WHERE `video_id`=?', array($video['video_id']));
                 $DB->Update('UPDATE `tbx_conversion_queue` SET `date_started`=? WHERE `video_id`=?', array(Database_MySQL::Now(), $video['video_id']));
                 $clips = $DB->FetchAll('SELECT * FROM `tbx_video_clip` WHERE `video_id`=? ORDER BY `clip_id`', array($queue_item['video_id']));
                 $dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id']));
                 Video_Converter::SetLogFile($dir->GetBaseDir() . '/convert.log');
                 $convert_start = time();
                 $conversion_failed = false;
                 foreach ($clips as $clip) {
                     $clip_path = null;
                     $old_path = null;
                     try {
                         // Stored locally, move to originals directory
                         if ($clip['clip'][0] == '/') {
                             $old_path = $doc_root . $clip['clip'];
                             $clip_path = $dir->AddOriginalFromFile($old_path);
                         } else {
                             $http = new HTTP();
                             if ($http->Get($clip['clip'], $clip['clip'])) {
                                 $clip_path = $dir->AddOriginalFromVar($http->body, File::Extension($clip['clip']));
                             } else {
                                 throw new BaseException('Could not download clip for conversion: ' . $http->error);
                             }
                         }
                         $output_file = Video_Converter::Convert($clip_path, $dir->GetProcessingDir(), Config::Get('video_format'), Config::Get('video_bitrate'), Config::Get('audio_bitrate'), Config::Get('video_size'), array('ConversionQueue', 'Ping'));
                         $converted_video = $dir->AddClipFromFile($output_file);
                         $DB->Disconnect();
                         $DB->Connect();
                         $DB->Update('UPDATE `tbx_video_clip` SET `clip`=?,`filesize`=? WHERE `clip_id`=?', array(str_replace($doc_root, '', $converted_video), filesize($converted_video), $clip['clip_id']));
                     } catch (Exception $e) {
                         if (!empty($old_path) && !empty($clip_path)) {
                             rename($clip_path, $old_path);
                         }
                         Video_Converter::Log($e->getMessage() . (strtolower(get_class($e)) == 'baseexception' ? $e->getExtras() : '') . "\n" . $e->getTraceAsString());
                         $conversion_failed = true;
                     }
                 }
                 $convert_end = time();
                 $dir->ClearProcessing();
                 $dir->ClearTemp();
                 $DB->Connect();
                 $DB->Update('DELETE FROM `tbx_conversion_queue` WHERE `video_id`=?', array($queue_item['video_id']));
                 if ($conversion_failed) {
                     self::UpdateStatsProcessed($convert_start, $convert_end, $queue_item['queued'], true);
                     $DB->Update('UPDATE `tbx_video` SET `conversion_failed`=1 WHERE `video_id`=?', array($video['video_id']));
                 } else {
                     // Update stats
                     self::UpdateStatsProcessed($convert_start, $convert_end, $queue_item['queued']);
                     $status = empty($video['next_status']) ? STATUS_ACTIVE : $video['next_status'];
                     // Set video status
                     $DB->Update('UPDATE `tbx_video` SET `status`=? WHERE `video_id`=?', array($status, $video['video_id']));
                     if ($video['status'] != $status && $status == STATUS_ACTIVE && !$video['is_private']) {
                         Tags::AddToFrequency($video['tags']);
                     }
                     UpdateCategoryStats($video['category_id']);
                 }
             }
         } else {
             break;
         }
     }
     self::MarkStopped();
     self::Log('Exiting...');
 }