/** * Will take a single KalturaBatchJob and extract the media info for the given file */ private function extract(KalturaBatchJob $job, KalturaExtractMediaJobData $data) { KalturaLog::debug("extract({$job->id})"); $srcFileSyncDescriptor = reset($data->srcFileSyncs); $mediaFile = null; if ($srcFileSyncDescriptor) { $mediaFile = trim($srcFileSyncDescriptor->fileSyncLocalPath); } if (!$this->pollingFileExists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } KalturaLog::debug("mediaFile [{$mediaFile}]"); $this->updateJob($job, "Extracting file media info on {$mediaFile}", KalturaBatchJobStatus::QUEUED); $mediaInfo = null; try { $mediaFile = realpath($mediaFile); KalturaLog::debug("file path [{$mediaFile}]"); $engine = KBaseMediaParser::getParser($job->jobSubType, $mediaFile, self::$taskConfig, $job); if ($engine) { KalturaLog::debug("Found engine [" . get_class($engine) . "]"); $mediaInfo = $engine->getMediaInfo(); } else { $err = "No media info parser engine found for job sub type [{$job->jobSubType}]"; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::ENGINE_NOT_FOUND, $err, KalturaBatchJobStatus::FAILED); } } catch (Exception $ex) { KalturaLog::err($ex->getMessage()); $mediaInfo = null; } if (is_null($mediaInfo)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: {$mediaFile}", KalturaBatchJobStatus::RETRY); } KalturaLog::debug("flavorAssetId [{$data->flavorAssetId}]"); $mediaInfo->flavorAssetId = $data->flavorAssetId; $mediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo); $data->mediaInfoId = $mediaInfo->id; $this->updateJob($job, "Saving media info id {$mediaInfo->id}", KalturaBatchJobStatus::PROCESSED, $data); $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED); return $job; }
/** * Creates instance of class and initializes properties * @param string $type * @param string $filePath * @param KSchedularTaskConfig $taskConfig */ public function __construct($type, $filePath, KSchedularTaskConfig $taskConfig, KalturaBatchJob $job, $wamsAssetId) { $this->wamsAssetId = $wamsAssetId; $this->filePath = $filePath; $this->mediaInfoParser = parent::getParser($type, $filePath, $taskConfig, $job); $this->partnerId = $job->partnerId; DbManager::setConfig(kConf::getDB()); DbManager::initialize(); $fileSync = FileSyncPeer::retrieveByWamsAssetId($this->wamsAssetId); if ($fileSync) { $flavorAsset = kFileSyncUtils::retrieveObjectForFileSync($fileSync); if ($flavorAsset instanceof asset) { $this->originalMediaInfo = mediaInfoPeer::retrieveOriginalByEntryId($flavorAsset->getEntryId()); $entry = $flavorAsset->getentry(); if ($entry) { $this->mediaType = $entry->getMediaType(); } } } }
/** * @param KalturaBatchJob $job * @param KalturaPostConvertJobData $data * @return KalturaBatchJob */ private function postConvert(KalturaBatchJob $job, KalturaPostConvertJobData $data) { if ($data->flavorParamsOutputId) { $data->flavorParamsOutput = KBatchBase::$kClient->flavorParamsOutput->get($data->flavorParamsOutputId); } try { $srcFileSyncDescriptor = reset($data->srcFileSyncs); $mediaFile = null; if ($srcFileSyncDescriptor) { $mediaFile = trim($srcFileSyncDescriptor->fileSyncLocalPath); } if (!$data->flavorParamsOutput || !$data->flavorParamsOutput->sourceRemoteStorageProfileId) { if (!$this->pollingFileExists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } } $this->updateJob($job, "Extracting file media info on {$mediaFile}", KalturaBatchJobStatus::QUEUED); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } $mediaInfo = null; try { $engine = KBaseMediaParser::getParser($job->jobSubType, realpath($mediaFile), KBatchBase::$taskConfig, $job); if ($engine) { KalturaLog::info("Media info engine [" . get_class($engine) . "]"); $mediaInfo = $engine->getMediaInfo(); } else { $err = "Media info engine not found for job subtype [" . $job->jobSubType . "]"; KalturaLog::info($err); return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::ENGINE_NOT_FOUND, $err, KalturaBatchJobStatus::FAILED); } } catch (Exception $ex) { KalturaLog::err("Error: " . $ex->getMessage()); $mediaInfo = null; } /* @var $mediaInfo KalturaMediaInfo */ if (is_null($mediaInfo)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: {$mediaFile}", KalturaBatchJobStatus::FAILED); } /* * Look for silent/black conversions. Curently checked only for Webex/ARF products */ $detectMsg = null; if (isset($data->flavorParamsOutput) && isset($data->flavorParamsOutput->operators) && strstr($data->flavorParamsOutput->operators, "webexNbrplayer.WebexNbrplayer") != false) { $rv = $this->checkForValidityOfWebexProduct($data, realpath($mediaFile), $mediaInfo, $detectMsg); if ($rv == false) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::BLACK_OR_SILENT_CONTENT, $detectMsg, KalturaBatchJobStatus::FAILED); } } try { $mediaInfo->flavorAssetId = $data->flavorAssetId; $createdMediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo); /* @var $createdMediaInfo KalturaMediaInfo */ // must save the mediaInfoId before reporting that the task is finished $msg = "Saving media info id {$createdMediaInfo->id}"; if (isset($detectMsg)) { $msg .= "({$detectMsg})"; } $this->updateJob($job, $msg, KalturaBatchJobStatus::PROCESSED, $data); $data->thumbPath = null; if (!$data->createThumb) { return $this->closeJob($job, null, null, "Media info id {$createdMediaInfo->id} saved", KalturaBatchJobStatus::FINISHED, $data); } // creates a temp file path $rootPath = KBatchBase::$taskConfig->params->localTempPath; $this->createDir($rootPath); // creates the path $uniqid = uniqid('thumb_'); $thumbPath = $rootPath . DIRECTORY_SEPARATOR . $uniqid; $videoDurationSec = floor($mediaInfo->videoDuration / 1000); $data->thumbOffset = max(0, min($data->thumbOffset, $videoDurationSec)); if ($mediaInfo->videoHeight) { $data->thumbHeight = $mediaInfo->videoHeight; } if ($mediaInfo->videoBitRate) { $data->thumbBitrate = $mediaInfo->videoBitRate; } // generates the thumbnail $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $thumbPath, KBatchBase::$taskConfig->params->FFMpegCmd); $created = $thumbMaker->createThumnail($data->thumbOffset, $mediaInfo->videoWidth, $mediaInfo->videoHeight, null, null, $mediaInfo->videoDar); if (!$created || !file_exists($thumbPath)) { $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, 'Thumbnail not created', KalturaBatchJobStatus::FINISHED, $data); } $data->thumbPath = $thumbPath; $job = $this->moveFile($job, $data); if ($this->checkFileExists($job->data->thumbPath)) { return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $data); } $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FINISHED, $data); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } }
/** * @param string $filePath * @param string $cmdPath */ public function __construct($filePath, $cmdPath = "mediainfo") { $this->cmdPath = $cmdPath; parent::__construct($filePath); }
/** * @param string $filePath * @param string $cmdPath */ public function __construct($filePath, $cmdPath = 'ffmpeg') { $this->cmdPath = $cmdPath; parent::__construct($filePath); }
/** * @param string $filePath * @param string $cmdPath */ public function __construct($filePath, $cmdPath = "ffmpeg", $ffprobeBin = "ffprobe") { $this->cmdPath = $cmdPath; $this->ffprobeBin = $ffprobeBin; parent::__construct($filePath); }
/** * @param KalturaBatchJob $job * @param KalturaPostConvertJobData $data * @return KalturaBatchJob */ private function postConvert(KalturaBatchJob $job, KalturaPostConvertJobData $data) { DbManager::setConfig(kConf::getDB()); DbManager::initialize(); $flavorAsset = assetPeer::retrieveByPK($data->flavorAssetId); if (!empty($flavorAsset) && $this->checkMediaInfoExists($data->flavorAssetId) && $flavorAsset->getIsOriginal() && $flavorAsset->getSize() !== 0) { $data->createThumb = false; return $this->closeJob($job, null, null, 'Media info already exists', KalturaBatchJobStatus::FINISHED, $data); } if ($data->flavorParamsOutputId) { $data->flavorParamsOutput = $this->kClient->flavorParamsOutput->get($data->flavorParamsOutputId); } try { $mediaFile = trim($data->srcFileSyncLocalPath); $mediaFileExt = pathinfo($mediaFile, PATHINFO_EXTENSION); if (!is_null($data->srcFileSyncWamsAssetId)) { $tempFile = kWAMS::getInstance($job->partnerId)->createTempFileForAssetId($data->srcFileSyncWamsAssetId, $mediaFileExt); if (file_exists($tempFile)) { $mediaFile = $tempFile; } } if (!$data->flavorParamsOutput || !$data->flavorParamsOutput->sourceRemoteStorageProfileId) { if (!$this->pollingFileExists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } } KalturaLog::debug("mediaFile [{$mediaFile}]"); $this->updateJob($job, "Extracting file media info on {$mediaFile}", KalturaBatchJobStatus::QUEUED, 1); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } $mediaInfo = null; try { $engine = KBaseMediaParser::getParser($job->jobSubType, realpath($mediaFile), $this->taskConfig, $job, $data->srcFileSyncWamsAssetId); if ($engine) { KalturaLog::info("Media info engine [" . get_class($engine) . "]"); $mediaInfo = $engine->getMediaInfo(); if (!empty($data->srcFileSyncWamsAssetId)) { kWAMS::getInstance($job->partnerId)->deleteTempFileForAssetId($data->srcFileSyncWamsAssetId, $mediaFileExt); } } else { $err = "Media info engine not found for job subtype [" . $job->jobSubType . "]"; KalturaLog::info($err); return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::ENGINE_NOT_FOUND, $err, KalturaBatchJobStatus::FAILED); } } catch (Exception $ex) { KalturaLog::err("Error: " . $ex->getMessage()); $mediaInfo = null; } /* @var $mediaInfo KalturaMediaInfo */ if (is_null($mediaInfo)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: {$mediaFile}", KalturaBatchJobStatus::FAILED); } try { $mediaInfo->flavorAssetId = $data->flavorAssetId; $createdMediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo); if ($createdMediaInfo instanceof KalturaMediaInfo) { $mediaInfo = $createdMediaInfo; } // must save the mediaInfoId before reporting that the task is finished $this->updateJob($job, "Saving media info id {$createdMediaInfo->id}", KalturaBatchJobStatus::PROCESSED, 50, $data); $data->thumbPath = null; if (!$data->createThumb) { return $this->closeJob($job, null, null, "Media info id {$createdMediaInfo->id} saved", KalturaBatchJobStatus::FINISHED, $data); } // creates a temp file path $rootPath = $this->taskConfig->params->localTempPath; $this->createDir($rootPath); // creates the path $uniqid = uniqid('thumb_'); $thumbPath = realpath($rootPath) . "/{$uniqid}"; $videoDurationSec = floor($mediaInfo->videoDuration / 1000); $data->thumbOffset = max(0, min($data->thumbOffset, $videoDurationSec)); if ($mediaInfo->videoHeight) { $data->thumbHeight = $mediaInfo->videoHeight; } if ($mediaInfo->videoBitRate) { $data->thumbBitrate = $mediaInfo->videoBitRate; } // generates the thumbnail if (!is_null($data->srcFileSyncWamsAssetId)) { $thumbMaker = new KWAMSThumbnailMaker($data->srcFileSyncWamsAssetId, $thumbPath); $created = $thumbMaker->createThumbnail($data->thumbOffset, $mediaInfo->videoWidth, $mediaInfo->videoHeight, $mediaInfo->videoDar); } else { $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $thumbPath, $this->taskConfig->params->FFMpegCmd); $created = $thumbMaker->createThumnail($data->thumbOffset, $mediaInfo->videoWidth, $mediaInfo->videoHeight, null, null, $mediaInfo->videoDar); } if (!$created || !file_exists($thumbPath)) { $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, 'Thumbnail not created', KalturaBatchJobStatus::FINISHED, $data); } $data->thumbPath = $thumbPath; $job = $this->moveFile($job, $data); if ($this->checkFileExists($job->data->thumbPath)) { return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $data); } $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FINISHED, $data); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } }