/** * * Cron job will run this function * to do all the pending video conversion * * @since Jomsocial 1.2.0 */ public function runConvert() { $config = CFactory::getConfig(); $videoFolder = $config->get('videofolder'); $deleteOriginal = $config->get('deleteoriginalvideos'); if (!$config->get('enablevideos')) { $this->errorMsg[] = 'Video is disabled. Video conversion will not run. '; return; } $zencoder = $config->get('enable_zencoder'); $model = CFactory::getModel('videos'); $videos = $model->getPendingVideos(); if (count($videos) < 1) { $this->errorMsg[] = 'No videos pending for conversion. '; return; } if (!$zencoder && !$this->hasFFmpegSupport()) { $this->errorMsg[] = 'FFmpeg cannot be executed.'; return; } // First of all, lock the videos $table = JTable::getInstance('Video', 'CTable'); foreach ($videos as $video) { $table->load($video->id); $table->save(array('status' => 'locked')); $table->reset(); } // Process each video $videoCounter = 0; if ($zencoder) { $s3BucketPath = $config->get('storages3bucket'); $s3outputBaseUrl = 'http://' . $s3BucketPath . '.s3.amazonaws.com'; $videoFolder = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME; list($outputWidth, $outputHeight) = explode('x', $config->get('videosSize'), 2); $outputThumbSize = $config->get('videosThumbSize'); $test = false; $path = JPATH_ROOT . '/components/com_community/libraries/zencoder.php'; if (JFile::exists($path)) { include_once $path; } else { exit('Missing File' . JPATH_ROOT . '/components/com_community/libraries/zencoder.php'); } $job = new CZencoderJob(); foreach ($videos as $video) { $outputFilename = $videoFolder . '/' . $video->creator . '/' . CStringHelper::getRandom() . '.mp4'; $outputThumbUrl = $s3outputBaseUrl . '/' . $videoFolder . '/' . $video->creator . '/' . VIDEO_THUMB_FOLDER_NAME . '/'; //Error: "Filename should only be a file name. It should not include path information." //zencoder restricted the filename, so we move the path from filename $outputBaseUrl = $s3outputBaseUrl . '/' . $videoFolder . '/' . $video->creator; $randomFilename = CStringHelper::getRandom(); $outputFilename = $randomFilename . '.mp4'; $input = JURI::root() . $video->path; $oriPath = JPATH::clean(JPATH_ROOT . '/' . $video->path); $videoName = explode('/', $video->path); $outputObj = new CZencoderOutput(); $outputObj->setBaseUrl($outputBaseUrl); $outputObj->setWidth($outputWidth); $outputObj->setHeight($outputHeight); $outputObj->setFilename($outputFilename); $outputObj->setThumbnailsNumber(1); $outputObj->setThumbnailsSize($outputThumbSize); $outputObj->setThumbnailsBaseUrl($outputThumbUrl); $outputObj->setThumbnailsPublic(1); $outputObj->setPublic(1); $outputObj->setNotifications(array(array('format' => 'xml', 'url' => CRoute::getExternalURL('index.php?option=com_community&view=videos&task=zencodercallback&videoid=' . $video->id . '&videoname=' . $videoName[count($videoName) - 1])))); //Thumbname need to be video specific now $outputObj->setThumbnailsPrefix($randomFilename); // we only need 1 output file $output = array(); $output[] = $outputObj->build(); $result = $job->create($input, $output, $test); if ($result) { // save into db //$video->path = $outputFilename; $video->path = $videoFolder . '/' . $video->creator . '/' . $outputFilename; $video->thumb = $outputThumbUrl . $randomFilename . '_0000.png'; $video->duration = 0; $video->storage = 's3'; $video->status = 'ready'; $storageTable = JTable::getInstance('StorageS3', 'CTable'); $storageTable->storageid = $video->path; $storageTable->resource_path = $video->path; $storageTable->store(); $table->reset(); $table->bind($video); if ($table->store()) { $this->errorMsg[] = $table->getError(); } $this->addVideoActivity($table); $videoCounter++; } else { $this->errorMsg[] = $job->getError(); } } } else { $videoSize = CVideosHelper::getVideoSize(); $injectMetadata = JFile::exists($this->flvtool2); foreach ($videos as $video) { $videoInFile = JPATH::clean(JPATH_ROOT . '/' . $video->path); $videoOutFolder = JPATH::clean(JPATH_ROOT . '/' . $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator); $videoFilename = $this->convertVideo($videoInFile, $videoOutFolder, $videoSize, $deleteOriginal); if ($videoFilename) { $videoFullPath = $videoOutFolder . '/' . $videoFilename; if ($injectMetadata) { $this->injectMetadata($videoFullPath); } // Read duration $videoInfo = $this->getVideoInfo($videoFullPath); if (!$videoInfo) { $thumbName = null; $this->errorMsg[] = 'Could not read video information. Video id: ' . $video->id; } else { $videoFrame = CVideosHelper::formatDuration((int) ($videoInfo['duration']['sec'] / 2), 'HH:MM:SS'); // Create thumbnail $thumbFolder = JPATH::clean(JPATH_ROOT . '/' . $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator . '/' . VIDEO_THUMB_FOLDER_NAME); $thumbSize = CVideos::thumbSize(); $thumbFileName = $this->createVideoThumb($videoFullPath, $thumbFolder, $videoFrame, $thumbSize); } if (!$thumbFileName) { $this->errorMsg[] = 'Could not create thumbnail for video. Video id: ' . $video->id; } // Save into DB $config = CFactory::getConfig(); $videoFolder = $config->get('videofolder'); $video->path = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator . '/' . $videoFilename; $video->thumb = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator . '/' . VIDEO_THUMB_FOLDER_NAME . '/' . $thumbFileName; $video->duration = $videoInfo['duration']['sec']; $video->status = 'ready'; $table->reset(); $table->bind($video); if (!$table->store()) { $this->errorMsg[] = $table->getError(); } // Add into activity streams $this->addVideoActivity($table); $videoCounter++; $params = new CParameter(''); $params->set('url', JURI::root() . $table->getViewURI()); $params->set('target', $video->creator); CNotificationLibrary::add('videos_convert_success', NULL, $video->creator, JText::sprintf('COM_COMMUNITY_VIDEOS_CONVERT_SUCCESS_SUBJECT', $table->getTitle()), '', 'videos.convert.success', $params, true, '', '', false); } else { $this->errorMsg[] = 'Could not convert video id: ' . $video->id; } $table->reset(); unset($video); } // end foreach pending videos } // Lastly, unlock the videos foreach ($videos as $video) { $table->load($video->id); if ($table->status == 'locked') { $table->save(array('status' => 'pending')); } } $this->errorMsg[] = $videoCounter ? $videoCounter . ' videos converted successfully...' : 'No videos was converted'; $returnMsg = ''; foreach ($this->errorMsg as $msg) { $returnMsg .= "{$msg}\r\n"; } return $returnMsg; }
/** * * Cron job will run this function * to do all the pending video conversion * * @since Jomsocial 1.2.0 */ function runConvert() { $config = CFactory::getConfig(); $videofolder = $config->get('videofolder'); if (!$config->get('enablevideos')) { $this->errorMsg[] = 'Video is disabled. Video conversion will not run. '; return; } $zencoder = $config->get('enable_zencoder'); $model = CFactory::getModel('videos'); $videos = $model->getPendingVideos(); if (count($videos) < 1) { $this->errorMsg[] = 'No videos pending for conversion. '; return; } if (!$zencoder && !$this->hasFFmpegSupport()) { $this->errorMsg[] = 'FFmpeg cannot be executed.'; return; } // First of all, lock the videos $table = JTable::getInstance('Video', 'CTable'); foreach ($videos as $video) { $table->load($video->id); $table->save(array('status' => 'locked')); $table->reset(); } // Process each video $videoCounter = 0; if ($zencoder) { $s3BucketPath = $config->get('storages3bucket'); $outputBaseUrl = 'http://' . $s3BucketPath . '.s3.amazonaws.com'; $videoFolder = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME; list($outputWidth, $outputHeight) = explode('x', $config->get('videosSize'), 2); $outputThumbSize = $config->get('videosThumbSize'); $test = false; CFactory::load('helpers', 'string'); CFactory::load('libraries', 'zencoder'); $job = new CZencoderJob(); foreach ($videos as $video) { $outputFilename = $videoFolder . '/' . $video->creator . '/' . CStringHelper::getRandom() . '.mp4'; $outputThumbUrl = $outputBaseUrl . '/' . $videoFolder . '/' . $video->creator . '/' . VIDEO_THUMB_FOLDER_NAME . '/'; //Error: "Filename should only be a file name. It should not include path information." //zencoder restricted the filename, so we move the path from filename $outputBaseUrl = $outputBaseUrl . '/' . $videoFolder . '/' . $video->creator; $randomFilename = CStringHelper::getRandom(); $outputFilename = $randomFilename . '.mp4'; $input = JURI::root() . $video->path; $outputObj = new CZencoderOutput(); $outputObj->setBaseUrl($outputBaseUrl); $outputObj->setWidth($outputWidth); $outputObj->setHeight($outputHeight); $outputObj->setFilename($outputFilename); $outputObj->setThumbnailsNumber(1); $outputObj->setThumbnailsSize($outputThumbSize); $outputObj->setThumbnailsBaseUrl($outputThumbUrl); $outputObj->setThumbnailsPublic(1); $outputObj->setPublic(1); //Thumbname need to be video specific now $outputObj->setThumbnailsPrefix($randomFilename); /* HTGMOD START - DJ */ $outputObj->setWatermark(JURI::root() . 'images/originalvideos/watermark.png', '-10', '-10', '100'); /* HTGMOD END - DJ */ // we only need 1 output file $output = array(); $output[] = $outputObj->build(); $result = $job->create($input, $output, $test); if ($result) { /* HTGMOD START - DJ */ $videoInfo = $this->getVideoInfo(JPATH::clean(JPATH_ROOT . DS . $video->path)); /* HTGMOD END - DJ */ // save into db //$video->path = $outputFilename; $video->path = $videoFolder . '/' . $video->creator . '/' . $outputFilename; $video->thumb = $outputThumbUrl . $randomFilename . '_0000.png'; $video->duration = $videoInfo['duration']['sec']; // HTGMOD $video->storage = 's3'; $video->status = 'ready'; $storageTable = JTable::getInstance('StorageS3', 'CTable'); $storageTable->storageid = $video->path; $storageTable->resource_path = $video->path; $storageTable->store(); $table->reset(); $table->bind($video); if ($table->store()) { $this->errorMsg[] = $table->getError(); } if ($table->isPublic()) { $my = CFactory::getUser($table->creator); $act = new stdClass(); $act->cmd = 'videos.upload'; $act->actor = $my->id; $act->target = 0; $act->title = JText::sprintf('CC ACTIVITIES POST VIDEO', '{video_url}', $table->title); $act->app = 'videos'; $act->cid = $table->id; $act->content = '<img src="' . $table->getThumbnail() . '" style="border: 1px solid #eee;margin-right: 3px;" />'; $params = new JParameter(''); $params->set('video_url', $table->getViewURI(false)); // Add activity logging CFactory::load('libraries', 'activities'); CActivityStream::add($act, $params->toString()); } $videoCounter++; } else { $this->errorMsg[] = $job->getError(); } } } else { $videoSize = CVideosHelper::getVideoSize(); $deleteOriginal = $config->get('deleteoriginalvideos'); $injectMetadata = JFile::exists($this->flvtool2); foreach ($videos as $video) { $videoInFile = JPATH::clean(JPATH_ROOT . DS . $video->path); $videoOutFolder = JPATH::clean(JPATH_ROOT . DS . $config->get('videofolder') . DS . VIDEO_FOLDER_NAME . DS . $video->creator); $videoFilename = $this->convertVideo($videoInFile, $videoOutFolder, $videoSize, $deleteOriginal); if ($videoFilename) { $videoFullPath = $videoOutFolder . DS . $videoFilename; if ($injectMetadata) { $this->injectMetadata($videoFullPath); } // Read duration $videoInfo = $this->getVideoInfo($videoFullPath); if (!$videoInfo) { $thumbName = null; $this->errorMsg[] = 'Could not read video information. Video id: ' . $video->id; } else { $videoFrame = CVideosHelper::formatDuration((int) ($videoInfo['duration']['sec'] / 2), 'HH:MM:SS'); // Create thumbnail $thumbFolder = JPATH::clean(JPATH_ROOT . DS . $config->get('videofolder') . DS . VIDEO_FOLDER_NAME . DS . $video->creator . DS . VIDEO_THUMB_FOLDER_NAME); $thumbSize = CVideos::thumbSize(); $thumbFileName = $this->createVideoThumb($videoFullPath, $thumbFolder, $videoFrame, $thumbSize); } if (!$thumbFileName) { $this->errorMsg[] = 'Could not create thumbnail for video. Video id: ' . $video->id; } // Save into DB $config = CFactory::getConfig(); $videoFolder = $config->get('videofolder'); $video->path = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator . '/' . $videoFilename; $video->thumb = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $video->creator . '/' . VIDEO_THUMB_FOLDER_NAME . '/' . $thumbFileName; $video->duration = $videoInfo['duration']['sec']; $video->status = 'ready'; $table->reset(); $table->bind($video); if (!$table->store()) { $this->errorMsg[] = $table->getError(); } // Add into activity streams if ($table->isPublic()) { $my = CFactory::getUser($table->creator); $act = new stdClass(); $act->cmd = 'videos.upload'; $act->actor = $my->id; $act->target = 0; $act->title = JText::sprintf('CC ACTIVITIES POST VIDEO', '{video_url}', $table->title); $act->app = 'videos'; $act->cid = $table->id; $act->content = '<img src="' . $table->getThumbnail() . '" style="border: 1px solid #eee;margin-right: 3px;" />'; $params = new JParameter(''); $params->set('video_url', $table->getViewURI(false)); // Add activity logging CFactory::load('libraries', 'activities'); CActivityStream::add($act, $params->toString()); } $videoCounter++; } else { $this->errorMsg[] = 'Could not convert video id: ' . $video->id; } $table->reset(); unset($video); } // end foreach pending videos } // Lastly, unlock the videos foreach ($videos as $video) { $table->load($video->id); if ($table->status == 'locked') { $table->save(array('status' => 'pending')); } } $this->errorMsg[] = $videoCounter ? $videoCounter . ' videos converted successfully...' : 'No videos was converted'; $returnMsg = ''; foreach ($this->errorMsg as $msg) { $returnMsg .= "{$msg}\r\n"; } return $returnMsg; }