/** * @param BatchJob $parentJob * @param int $srcParamsId */ public static function generateThumbnailsFromFlavor($entryId, BatchJob $parentJob = null, $srcParamsId = null) { $profile = null; try { $profile = myPartnerUtils::getConversionProfile2ForEntry($entryId); } catch (Exception $e) { KalturaLog::err('getConversionProfile2ForEntry Error: ' . $e->getMessage()); } if (!$profile) { KalturaLog::notice("Profile not found for entry id [{$entryId}]"); return; } $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { KalturaLog::notice("Entry id [{$entryId}] not found"); return; } $assetParamsIds = flavorParamsConversionProfilePeer::getFlavorIdsByProfileId($profile->getId()); if (!count($assetParamsIds)) { KalturaLog::notice("No asset params objects found for profile id [" . $profile->getId() . "]"); return; } // the alternative is the source or the highest bitrate if source not defined $alternateFlavorParamsId = null; if (is_null($srcParamsId)) { $flavorParamsObjects = flavorParamsPeer::retrieveByPKs($assetParamsIds); foreach ($flavorParamsObjects as $flavorParams) { if ($flavorParams->hasTag(flavorParams::TAG_SOURCE)) { $alternateFlavorParamsId = $flavorParams->getId(); } } if (is_null($alternateFlavorParamsId)) { $srcFlavorAsset = flavorAssetPeer::retrieveHighestBitrateByEntryId($entryId); $alternateFlavorParamsId = $srcFlavorAsset->getFlavorParamsId(); } if (is_null($alternateFlavorParamsId)) { KalturaLog::notice("No source flavor params object found for entry id [{$entryId}]"); return; } } // create list of created thumbnails $thumbAssetsList = array(); $thumbAssets = thumbAssetPeer::retrieveByEntryId($entryId); if (count($thumbAssets)) { foreach ($thumbAssets as $thumbAsset) { if (!is_null($thumbAsset->getFlavorParamsId())) { $thumbAssetsList[$thumbAsset->getFlavorParamsId()] = $thumbAsset; } } } $thumbParamsObjects = thumbParamsPeer::retrieveByPKs($assetParamsIds); foreach ($thumbParamsObjects as $thumbParams) { // check if this thumbnail already created if (isset($thumbAssetsList[$thumbParams->getId()])) { continue; } if (is_null($srcParamsId) && is_null($thumbParams->getSourceParamsId())) { // alternative should be used $thumbParams->setSourceParamsId($alternateFlavorParamsId); } elseif ($thumbParams->getSourceParamsId() != $srcParamsId) { // only thumbnails that uses srcParamsId should be generated for now continue; } kBusinessPreConvertDL::decideThumbGenerate($entry, $thumbParams, $parentJob); } }
/** * decideThumbGenerate is the decision layer for a single thumbnail generation * * @param entry $entry * @param thumbParams $destThumbParams * @param BatchJob $parentJob * @return thumbAsset */ public static function decideThumbGenerate(entry $entry, thumbParams $destThumbParams, BatchJob $parentJob = null, $sourceAssetId = null, $runSync = false) { $srcAsset = null; assetPeer::resetInstanceCriteriaFilter(); if ($sourceAssetId) { $srcAsset = assetPeer::retrieveById($sourceAssetId); } else { if ($destThumbParams->getSourceParamsId()) { KalturaLog::debug("Look for flavor params [" . $destThumbParams->getSourceParamsId() . "]"); $srcAsset = assetPeer::retrieveByEntryIdAndParams($entry->getId(), $destThumbParams->getSourceParamsId()); } if (is_null($srcAsset)) { KalturaLog::debug("Look for original flavor"); $srcAsset = flavorAssetPeer::retrieveOriginalByEntryId($entry->getId()); } if (is_null($srcAsset) || $srcAsset->getStatus() != flavorAsset::FLAVOR_ASSET_STATUS_READY) { KalturaLog::debug("Look for highest bitrate flavor"); $srcAsset = flavorAssetPeer::retrieveHighestBitrateByEntryId($entry->getId()); } } if (is_null($srcAsset)) { throw new APIException(APIErrors::FLAVOR_ASSET_IS_NOT_READY); } $errDescription = null; $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($srcAsset->getId()); $destThumbParamsOutput = self::validateThumbAndMediaInfo($destThumbParams, $mediaInfo, $errDescription); $thumbAsset = thumbAssetPeer::retrieveByEntryIdAndParams($entry->getId(), $destThumbParams->getId()); if ($thumbAsset) { $description = $thumbAsset->getDescription() . "\n" . $errDescription; $thumbAsset->setDescription($description); } else { $thumbAsset = new thumbAsset(); $thumbAsset->setPartnerId($entry->getPartnerId()); $thumbAsset->setEntryId($entry->getId()); $thumbAsset->setDescription($errDescription); $thumbAsset->setFlavorParamsId($destThumbParams->getId()); } $thumbAsset->incrementVersion(); $thumbAsset->setStatus(flavorAsset::FLAVOR_ASSET_STATUS_CONVERTING); $thumbAsset->setTags($destThumbParamsOutput->getTags()); $thumbAsset->setFileExt($destThumbParamsOutput->getFileExt()); if (!$destThumbParamsOutput) { $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_ERROR); $thumbAsset->save(); return null; } $thumbAsset->save(); // save flavor params $destThumbParamsOutput->setPartnerId($entry->getPartnerId()); $destThumbParamsOutput->setEntryId($entry->getId()); $destThumbParamsOutput->setFlavorAssetId($thumbAsset->getId()); $destThumbParamsOutput->setFlavorAssetVersion($thumbAsset->getVersion()); $destThumbParamsOutput->save(); $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); $srcAssetType = $srcAsset->getType(); if (!$runSync) { $job = kJobsManager::addCapturaThumbJob($parentJob, $entry->getPartnerId(), $entry->getId(), $thumbAsset->getId(), $srcSyncKey, $srcAssetType, $destThumbParamsOutput); return $thumbAsset; } $errDescription = null; $capturedPath = self::generateThumbnail($srcAsset, $destThumbParamsOutput, $errDescription); // failed if (!$capturedPath) { $thumbAsset->incrementVersion(); $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_ERROR); $thumbAsset->setDescription($thumbAsset->getDescription() . "\n{$errDescription}"); $thumbAsset->save(); return $thumbAsset; } $thumbAsset->incrementVersion(); $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_READY); if (file_exists($capturedPath)) { list($width, $height, $type, $attr) = getimagesize($capturedPath); $thumbAsset->setWidth($width); $thumbAsset->setHeight($height); $thumbAsset->setSize(filesize($capturedPath)); } $logPath = $capturedPath . '.log'; if (file_exists($logPath)) { $thumbAsset->incLogFileVersion(); $thumbAsset->save(); // creats the file sync $logSyncKey = $thumbAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_CONVERT_LOG); kFileSyncUtils::moveFromFile($logPath, $logSyncKey); KalturaLog::debug("Log archived file to: " . kFileSyncUtils::getLocalFilePathForKey($logSyncKey)); } else { $thumbAsset->save(); } $syncKey = $thumbAsset->getSyncKey(thumbAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($capturedPath, $syncKey); KalturaLog::debug("Thumbnail archived file to: " . kFileSyncUtils::getLocalFilePathForKey($syncKey)); if ($thumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { // increment thumbnail version $entry->setThumbnail(".jpg"); $entry->save(); $entrySyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_THUMB); $syncFile = kFileSyncUtils::createSyncFileLinkForKey($entrySyncKey, $syncKey, false); if ($syncFile) { // removes the DEFAULT_THUMB tag from all other thumb assets $entryThumbAssets = thumbAssetPeer::retrieveByEntryId($thumbAsset->getEntryId()); foreach ($entryThumbAssets as $entryThumbAsset) { if ($entryThumbAsset->getId() == $thumbAsset->getId()) { continue; } if (!$entryThumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { continue; } $entryThumbAsset->removeTags(array(thumbParams::TAG_DEFAULT_THUMB)); $entryThumbAsset->save(); } } } if (!is_null($thumbAsset->getFlavorParamsId())) { kFlowHelper::generateThumbnailsFromFlavor($thumbAsset->getEntryId(), null, $thumbAsset->getFlavorParamsId()); } return $thumbAsset; }
/** * Tags the thumbnail as DEFAULT_THUMB and removes that tag from all other thumbnail assets of the entry. * Create a new file sync link on the entry thumbnail that points to the thumbnail asset file sync. * * @action setAsDefault * @param string $thumbAssetId * @throws KalturaErrors::THUMB_ASSET_ID_NOT_FOUND */ public function setAsDefaultAction($thumbAssetId) { $thumbAsset = thumbAssetPeer::retrieveById($thumbAssetId); if (!$thumbAsset) { throw new KalturaAPIException(KalturaErrors::THUMB_ASSET_ID_NOT_FOUND, $thumbAssetId); } $entry = $thumbAsset->getentry(); if (!$entry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $thumbAsset->getEntryId()); } $entryThumbAssets = thumbAssetPeer::retrieveByEntryId($thumbAsset->getEntryId()); foreach ($entryThumbAssets as $entryThumbAsset) { if ($entryThumbAsset->getId() == $thumbAsset->getId()) { continue; } if (!$entryThumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { continue; } $entryThumbAsset->removeTags(array(thumbParams::TAG_DEFAULT_THUMB)); $entryThumbAsset->save(); } if (!$thumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { $thumbAsset->addTags(array(thumbParams::TAG_DEFAULT_THUMB)); $thumbAsset->save(); } $entry->setThumbnail(".jpg"); $entry->save(); $thumbSyncKey = $thumbAsset->getSyncKey(thumbAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); $entrySyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_THUMB); kFileSyncUtils::createSyncFileLinkForKey($entrySyncKey, $thumbSyncKey, false); }