Beispiel #1
0
 /**
  * @static
  * @param Asset_Video $asset
  * @param Asset_Video_Thumbnail_Config $config
  */
 public static function process(Asset_Video $asset, $config)
 {
     if (!Pimcore_Video::isAvailable()) {
         throw new Exception("No ffmpeg executable found, please configure the correct path in the system settings");
     }
     $instance = new self();
     $formats = array("mp4", "webm", "f4v");
     $instance->setProcessId(uniqid());
     $instance->setAssetId($asset->getId());
     $instance->setConfig($config);
     // check for running or already created thumbnails
     $customSetting = $asset->getCustomSetting("thumbnails");
     $existingFormats = array();
     if (is_array($customSetting) && array_key_exists($config->getName(), $customSetting)) {
         if ($customSetting[$config->getName()]["status"] == "inprogress") {
             if (is_file($instance->getJobFile($customSetting[$config->getName()]["processId"]))) {
                 return;
             }
         } else {
             if ($customSetting[$config->getName()]["status"] == "finished") {
                 // check if the files are there
                 $formatsToConvert = array();
                 foreach ($formats as $f) {
                     if (!is_file(PIMCORE_DOCUMENT_ROOT . $customSetting[$config->getName()]["formats"][$f])) {
                         $formatsToConvert[] = $f;
                     } else {
                         $existingFormats[$f] = $customSetting[$config->getName()]["formats"][$f];
                     }
                 }
                 if (!empty($formatsToConvert)) {
                     $formats = $formatsToConvert;
                 } else {
                     return;
                 }
             }
         }
     }
     foreach ($formats as $format) {
         $filename = "video_" . $asset->getId() . "__" . $config->getName() . "." . $format;
         $fsPath = PIMCORE_TEMPORARY_DIRECTORY . "/" . $filename;
         if (is_file($fsPath)) {
             @unlink($fsPath);
         }
         $converter = Pimcore_Video::getInstance();
         $converter->load($asset->getFileSystemPath());
         $converter->setAudioBitrate($config->getAudioBitrate());
         $converter->setVideoBitrate($config->getVideoBitrate());
         $converter->setFormat($format);
         $converter->setDestinationFile($fsPath);
         $transformations = $config->getItems();
         if (is_array($transformations) && count($transformations) > 0) {
             foreach ($transformations as $transformation) {
                 if (!empty($transformation)) {
                     $arguments = array();
                     $mapping = self::$argumentMapping[$transformation["method"]];
                     if (is_array($transformation["arguments"])) {
                         foreach ($transformation["arguments"] as $key => $value) {
                             $position = array_search($key, $mapping);
                             if ($position !== false) {
                                 $arguments[$position] = $value;
                             }
                         }
                     }
                     ksort($arguments);
                     if (count($mapping) == count($arguments)) {
                         call_user_func_array(array($converter, $transformation["method"]), $arguments);
                     } else {
                         $message = "Video Transform failed: cannot call method `" . $transformation["method"] . "´ with arguments `" . implode(",", $arguments) . "´ because there are too few arguments";
                         Logger::error($message);
                     }
                 }
             }
         }
         $instance->queue[] = $converter;
     }
     $customSetting = $asset->getCustomSetting("thumbnails");
     $customSetting = is_array($customSetting) ? $customSetting : array();
     $customSetting[$config->getName()] = array("status" => "inprogress", "formats" => $existingFormats, "processId" => $instance->getProcessId());
     $asset->setCustomSetting("thumbnails", $customSetting);
     $asset->save();
     $instance->convert();
     return $instance;
 }
Beispiel #2
0
 /**
  * @param $thumbnailName
  */
 public function getImageThumbnail($thumbnailName, $timeOffset = null, $imageAsset = null)
 {
     $cs = $this->getCustomSetting("image_thumbnail_time");
     $im = $this->getCustomSetting("image_thumbnail_asset");
     if (!$timeOffset && !$imageAsset && $cs) {
         $timeOffset = $cs;
     } else {
         if (!$timeOffset && !$imageAsset && $im) {
             $imageAsset = Asset::getById($im);
         }
     }
     // fallback
     if (!$timeOffset && !$imageAsset) {
         $timeOffset = 5;
     }
     if ($imageAsset instanceof Asset_Image) {
         return $imageAsset->getThumbnail($thumbnailName);
     }
     $thumbnail = $this->getImageThumbnailConfig($thumbnailName);
     $thumbnail->setName($thumbnail->getName() . "-" . $timeOffset);
     $converter = Pimcore_Video::getInstance();
     $converter->load($this->getFileSystemPath());
     $path = PIMCORE_TEMPORARY_DIRECTORY . "/video_" . $this->getId() . "__thumbnail_" . $timeOffset . ".png";
     if (!is_file($path)) {
         $converter->saveImage($path, $timeOffset);
     }
     if ($thumbnail) {
         try {
             $path = Asset_Image_Thumbnail_Processor::process($this, $thumbnail, $path);
         } catch (Exception $e) {
             Logger::error("Couldn't create image-thumbnail of video " . $this->getFullPath());
             Logger::error($e);
             return "/pimcore/static/img/filetype-not-supported.png";
         }
     }
     // if no thumbnail config is given return the original image
     if (empty($path)) {
         $fsPath = $this->getFileSystemPath();
         $path = str_replace(PIMCORE_DOCUMENT_ROOT, "", $fsPath);
         return $path;
     }
     return $path;
 }