/** * */ public function generate() { $errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png'; $deferred = false; $generated = false; if (!$this->asset) { $this->filesystemPath = $errorImage; } elseif (!$this->filesystemPath) { $cs = $this->asset->getCustomSetting("image_thumbnail_time"); $im = $this->asset->getCustomSetting("image_thumbnail_asset"); if ($im || $this->imageAsset) { if ($this->imageAsset) { $im = Model\Asset::getById($im); } if ($im instanceof Image) { return $im->getThumbnail($this->getConfig()); } } $timeOffset = $this->timeOffset; if (!$this->timeOffset && $cs) { $timeOffset = $cs; } // fallback if (!$timeOffset) { $timeOffset = ceil($this->asset->getDuration() / 3); } $converter = \Pimcore\Video::getInstance(); $converter->load($this->asset->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/video-image-cache/video_" . $this->asset->getId() . "__thumbnail_" . $timeOffset . ".png"; if (!is_dir(dirname($path))) { File::mkdir(dirname($path)); } if (!is_file($path)) { $lockKey = "video_image_thumbnail_" . $this->asset->getId() . "_" . $timeOffset; Model\Tool\Lock::acquire($lockKey); // after we got the lock, check again if the image exists in the meantime - if not - generate it if (!is_file($path)) { $converter->saveImage($path, $timeOffset); $generated = true; } Model\Tool\Lock::release($lockKey); } if ($this->getConfig()) { $this->getConfig()->setFilenameSuffix("time-" . $timeOffset); try { $path = Image\Thumbnail\Processor::process($this->asset, $this->getConfig(), $path, $deferred, true, $generated); } catch (\Exception $e) { \Logger::error("Couldn't create image-thumbnail of video " . $this->asset->getRealFullPath()); \Logger::error($e); $path = $errorImage; } } $this->filesystemPath = $path; \Pimcore::getEventManager()->trigger("asset.video.image-thumbnail", $this, ["deferred" => $deferred, "generated" => $generated]); } }
/** * */ public function generate() { $errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png'; $generated = false; if (!$this->asset) { $this->filesystemPath = $errorImage; } elseif (!$this->filesystemPath) { $config = $this->getConfig(); $config->setName("document_" . $config->getName() . "-" . $this->page); try { $path = null; if (!$this->deferred) { $converter = \Pimcore\Document::getInstance(); $converter->load($this->asset->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/document-image-cache/document_" . $this->asset->getId() . "__thumbnail_" . $this->page . ".png"; if (!is_dir(dirname($path))) { \Pimcore\File::mkdir(dirname($path)); } $lockKey = "document-thumbnail-" . $this->asset->getId() . "-" . $this->page; if (!is_file($path) && !Model\Tool\Lock::isLocked($lockKey)) { Model\Tool\Lock::lock($lockKey); $converter->saveImage($path, $this->page); $generated = true; Model\Tool\Lock::release($lockKey); } elseif (Model\Tool\Lock::isLocked($lockKey)) { return "/pimcore/static6/img/please-wait.png"; } } if ($config) { $path = Image\Thumbnail\Processor::process($this->asset, $config, $path, $this->deferred, true, $generated); } $this->filesystemPath = $path; } catch (\Exception $e) { Logger::error("Couldn't create image-thumbnail of document " . $this->asset->getRealFullPath()); Logger::error($e); $this->filesystemPath = $errorImage; } \Pimcore::getEventManager()->trigger("asset.document.image-thumbnail", $this, ["deferred" => $this->deferred, "generated" => $generated]); } }
/** * @param Model\Asset\Video $asset * @param $config * @param array $onlyFormats * @return Processor * @throws \Exception */ public static function process(Model\Asset\Video $asset, $config, $onlyFormats = []) { if (!\Pimcore\Video::isAvailable()) { throw new \Exception("No ffmpeg executable found, please configure the correct path in the system settings"); } $instance = new self(); $formats = empty($onlyFormats) ? ["mp4"] : $onlyFormats; $instance->setProcessId(uniqid()); $instance->setAssetId($asset->getId()); $instance->setConfig($config); // check for running or already created thumbnails $customSetting = $asset->getCustomSetting("thumbnails"); $existingFormats = []; if (is_array($customSetting) && array_key_exists($config->getName(), $customSetting)) { if ($customSetting[$config->getName()]["status"] == "inprogress") { if (TmpStore::get($instance->getJobStoreId($customSetting[$config->getName()]["processId"]))) { return; } } elseif ($customSetting[$config->getName()]["status"] == "finished") { // check if the files are there $formatsToConvert = []; foreach ($formats as $f) { if (!is_file($asset->getVideoThumbnailSavePath() . $customSetting[$config->getName()]["formats"][$f])) { $formatsToConvert[] = $f; } else { $existingFormats[$f] = $customSetting[$config->getName()]["formats"][$f]; $existingFormats[$f] = $customSetting[$config->getName()]["formats"][$f]; } } if (!empty($formatsToConvert)) { $formats = $formatsToConvert; } else { return; } } elseif ($customSetting[$config->getName()]["status"] == "error") { throw new \Exception("Unable to convert video, see logs for details."); } } foreach ($formats as $format) { $thumbDir = $asset->getVideoThumbnailSavePath() . "/thumb__" . $config->getName(); $filename = preg_replace("/\\." . preg_quote(File::getFileExtension($asset->getFilename())) . "/", "", $asset->getFilename()) . "." . $format; $fsPath = $thumbDir . "/" . $filename; $tmpPath = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . $filename; if (!is_dir(dirname($fsPath))) { File::mkdir(dirname($fsPath)); } 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($tmpPath); $converter->setStorageFile($fsPath); $transformations = $config->getItems(); if (is_array($transformations) && count($transformations) > 0) { foreach ($transformations as $transformation) { if (!empty($transformation)) { $arguments = []; $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([$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 : []; $customSetting[$config->getName()] = ["status" => "inprogress", "formats" => $existingFormats, "processId" => $instance->getProcessId()]; $asset->setCustomSetting("thumbnails", $customSetting); $asset->save(); $instance->convert(); return $instance; }