private function captureThumb(KalturaBatchJob $job, KalturaCaptureThumbJobData $data)
 {
     $thumbParamsOutput = self::$kClient->thumbParamsOutput->get($data->thumbParamsOutputId);
     try {
         $mediaFile = trim($data->srcFileSyncLocalPath);
         if (!file_exists($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, "Capturing thumbnail on {$mediaFile}", KalturaBatchJobStatus::QUEUED);
     } catch (Exception $ex) {
         return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
     }
     try {
         $data->thumbPath = null;
         // creates a temp file path
         $rootPath = self::$taskConfig->params->localTempPath;
         if (!is_dir($rootPath)) {
             if (!file_exists($rootPath)) {
                 KalturaLog::info("Creating temp thumbnail directory [{$rootPath}]");
                 mkdir($rootPath);
             } else {
                 // already exists but not a directory
                 KalturaLog::err("Cannot create temp thumbnail directory [{$rootPath}] due to an error. Please fix and restart");
                 die;
             }
         }
         $capturePath = $mediaFile;
         if ($data->srcAssetType == KalturaAssetType::FLAVOR) {
             // creates the path
             $uniqid = uniqid('thumb_');
             $capturePath = realpath($rootPath) . DIRECTORY_SEPARATOR . $uniqid;
             $mediaInfoWidth = null;
             $mediaInfoHeight = null;
             $mediaInfoDar = null;
             $mediaInfoVidDur = null;
             $mediaInfoFilter = new KalturaMediaInfoFilter();
             $mediaInfoFilter->flavorAssetIdEqual = $data->srcAssetId;
             $this->impersonate($job->partnerId);
             $mediaInfoList = self::$kClient->mediaInfo->listAction($mediaInfoFilter);
             $this->unimpersonate();
             if (count($mediaInfoList->objects)) {
                 $mediaInfo = reset($mediaInfoList->objects);
                 /* @var $mediaInfo KalturaMediaInfo */
                 $mediaInfoWidth = $mediaInfo->videoWidth;
                 $mediaInfoHeight = $mediaInfo->videoHeight;
                 $mediaInfoDar = $mediaInfo->videoDar;
                 if ($mediaInfo->videoDuration) {
                     $mediaInfoVidDur = $mediaInfo->videoDuration / 1000;
                 } else {
                     if ($mediaInfo->containerDuration) {
                         $mediaInfoVidDur = $mediaInfo->containerDuration / 1000;
                     } else {
                         if ($mediaInfo->audioDuration) {
                             $mediaInfoVidDur = $mediaInfo->audioDuration / 1000;
                         }
                     }
                 }
             }
             // generates the thumbnail
             $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $capturePath, self::$taskConfig->params->FFMpegCmd);
             $created = $thumbMaker->createThumnail($thumbParamsOutput->videoOffset, $mediaInfoWidth, $mediaInfoHeight, null, null, $mediaInfoDar, $mediaInfoVidDur);
             if (!$created || !file_exists($capturePath)) {
                 return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not created", KalturaBatchJobStatus::FAILED);
             }
             $this->updateJob($job, "Thumbnail captured [{$capturePath}]", KalturaBatchJobStatus::PROCESSING);
         }
         $uniqid = uniqid('thumb_');
         $thumbPath = $rootPath . DIRECTORY_SEPARATOR . $uniqid;
         $quality = $thumbParamsOutput->quality;
         $cropType = $thumbParamsOutput->cropType;
         $cropX = $thumbParamsOutput->cropX;
         $cropY = $thumbParamsOutput->cropY;
         $cropWidth = $thumbParamsOutput->cropWidth;
         $cropHeight = $thumbParamsOutput->cropHeight;
         $bgcolor = $thumbParamsOutput->backgroundColor;
         $width = $thumbParamsOutput->width;
         $height = $thumbParamsOutput->height;
         $scaleWidth = $thumbParamsOutput->scaleWidth;
         $scaleHeight = $thumbParamsOutput->scaleHeight;
         $density = $thumbParamsOutput->density;
         $rotate = $thumbParamsOutput->rotate;
         $cropper = new KImageMagickCropper($capturePath, $thumbPath, self::$taskConfig->params->ImageMagickCmd, true);
         $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor, $density, $rotate);
         if (!$cropped || !file_exists($thumbPath)) {
             return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not cropped", KalturaBatchJobStatus::FAILED);
         }
         $data->thumbPath = $thumbPath;
         $job = $this->moveFile($job, $data);
         if ($this->checkFileExists($job->data->thumbPath)) {
             $updateData = new KalturaCaptureThumbJobData();
             $updateData->thumbPath = $data->thumbPath;
             return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $updateData);
         }
         return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FAILED, $data);
     } catch (Exception $ex) {
         $this->unimpersonate();
         return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
     }
 }
 public static function generateThumbnail(asset $srcAsset, thumbParamsOutput $destThumbParamsOutput, &$errDescription)
 {
     $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
     list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false);
     if (!$fileSync || $fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) {
         $errDescription = 'Source asset could has no valid file sync';
         return false;
     }
     $srcPath = $fileSync->getFullPath();
     $uniqid = uniqid('thumb_');
     $destPath = kConf::get('temp_folder') . "/thumb/{$uniqid}.jpg";
     $logPath = $destPath . '.log';
     if (!file_exists($srcPath)) {
         $errDescription = "Source file [{$srcPath}] does not exist";
         return false;
     }
     if (!is_file($srcPath)) {
         $errDescription = "Source file [{$srcPath}] is not a file";
         return false;
     }
     try {
         if ($srcAsset->getType() == assetType::FLAVOR) {
             // generates the thumbnail
             $thumbMaker = new KFFMpegThumbnailMaker($srcPath, $destPath, kConf::get('bin_path_ffmpeg'));
             $created = $thumbMaker->createThumnail($destThumbParamsOutput->getVideoOffset());
             if (!$created || !file_exists($destPath)) {
                 $errDescription = "Thumbnail not captured";
                 return false;
             }
             $srcPath = $destPath;
             $uniqid = uniqid('thumb_');
             $destPath = kConf::get('temp_folder') . "/thumb/{$uniqid}.jpg";
         }
         $quality = $destThumbParamsOutput->getQuality();
         $cropType = $destThumbParamsOutput->getCropType();
         $cropX = $destThumbParamsOutput->getCropX();
         $cropY = $destThumbParamsOutput->getCropY();
         $cropWidth = $destThumbParamsOutput->getCropWidth();
         $cropHeight = $destThumbParamsOutput->getCropHeight();
         $bgcolor = $destThumbParamsOutput->getBackgroundColor();
         $width = $destThumbParamsOutput->getWidth();
         $height = $destThumbParamsOutput->getHeight();
         $scaleWidth = $destThumbParamsOutput->getScaleWidth();
         $scaleHeight = $destThumbParamsOutput->getScaleHeight();
         $cropper = new KImageMagickCropper($srcPath, $destPath, kConf::get('bin_path_imagemagick'), true);
         $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor);
         if (!$cropped || !file_exists($destPath)) {
             $errDescription = "Crop failed";
             return false;
         }
         return $destPath;
     } catch (Exception $ex) {
         $errDescription = $ex->getMessage();
         return false;
     }
 }
 private static function generateThumbnail(asset $srcAsset, thumbParamsOutput $destThumbParamsOutput, &$errDescription, $rotate = null)
 {
     $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
     list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false);
     if (!$fileSync || $fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) {
         $errDescription = 'Source asset could has no valid file sync';
         return false;
     }
     $srcPath = $fileSync->getFullPath();
     $uniqid = uniqid('thumb_');
     $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb';
     if (!file_exists($tempDir)) {
         mkdir($tempDir, 0700, true);
     }
     $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . '.jpg';
     $logPath = $destPath . '.log';
     if (!file_exists($srcPath)) {
         $errDescription = "Source file [{$srcPath}] does not exist";
         return false;
     }
     if (!is_file($srcPath)) {
         $errDescription = "Source file [{$srcPath}] is not a file";
         return false;
     }
     try {
         if ($srcAsset->getType() == assetType::FLAVOR) {
             /* @var $srcAsset flavorAsset */
             $dar = null;
             $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($srcAsset->getId());
             if ($mediaInfo) {
                 $dar = $mediaInfo->getVideoDar();
             }
             // generates the thumbnail
             $thumbMaker = new KFFMpegThumbnailMaker($srcPath, $destPath, kConf::get('bin_path_ffmpeg'));
             $created = $thumbMaker->createThumnail($destThumbParamsOutput->getVideoOffset(), $srcAsset->getWidth(), $srcAsset->getHeight(), null, null, $dar);
             if (!$created || !file_exists($destPath)) {
                 $errDescription = "Thumbnail not captured";
                 return false;
             }
             $srcPath = $destPath;
             $uniqid = uniqid('thumb_');
             $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb';
             if (!file_exists($tempDir)) {
                 mkdir($tempDir, 0700, true);
             }
             $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . '.jpg';
         }
         if ($srcAsset->getType() == assetType::THUMBNAIL) {
             $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb';
             if (!file_exists($tempDir)) {
                 mkdir($tempDir, 0700, true);
             }
             $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . "." . $srcAsset->getFileExt();
         }
         $quality = $destThumbParamsOutput->getQuality();
         $cropType = $destThumbParamsOutput->getCropType();
         $cropX = $destThumbParamsOutput->getCropX();
         $cropY = $destThumbParamsOutput->getCropY();
         $cropWidth = $destThumbParamsOutput->getCropWidth();
         $cropHeight = $destThumbParamsOutput->getCropHeight();
         $bgcolor = $destThumbParamsOutput->getBackgroundColor();
         $width = $destThumbParamsOutput->getWidth();
         $height = $destThumbParamsOutput->getHeight();
         $scaleWidth = $destThumbParamsOutput->getScaleWidth();
         $scaleHeight = $destThumbParamsOutput->getScaleHeight();
         $density = $destThumbParamsOutput->getDensity();
         $stripProfiles = $destThumbParamsOutput->getStripProfiles();
         $cropper = new KImageMagickCropper($srcPath, $destPath, kConf::get('bin_path_imagemagick'), true);
         $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor, $density, $rotate, $stripProfiles);
         if (!$cropped || !file_exists($destPath)) {
             $errDescription = "Crop failed";
             return false;
         }
         return $destPath;
     } catch (Exception $ex) {
         $errDescription = $ex->getMessage();
         return false;
     }
 }
 /**
  * @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);
     }
 }
 private function captureThumb(KalturaBatchJob $job, KalturaCaptureThumbJobData $data)
 {
     KalturaLog::debug("captureThumb({$job->id})");
     try {
         $mediaFile = trim($data->srcFileSyncLocalPath);
         if (!file_exists($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, "Capturing thumbnail on {$mediaFile}", KalturaBatchJobStatus::QUEUED, 1);
     } catch (Exception $ex) {
         return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
     }
     try {
         $data->thumbPath = null;
         // creates a temp file path
         $rootPath = $this->taskConfig->params->localTempPath;
         if (!is_dir($rootPath)) {
             if (!file_exists($rootPath)) {
                 KalturaLog::info("Creating temp thumbnail directory [{$rootPath}]");
                 mkdir($rootPath);
             } else {
                 // already exists but not a directory
                 KalturaLog::err("Cannot create temp thumbnail directory [{$rootPath}] due to an error. Please fix and restart");
                 die;
             }
         }
         $capturePath = $mediaFile;
         if ($data->srcAssetType == KalturaAssetType::FLAVOR) {
             // creates the path
             $uniqid = uniqid('thumb_');
             $capturePath = realpath($rootPath) . "/{$uniqid}";
             // generates the thumbnail
             $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $capturePath, $this->taskConfig->params->FFMpegCmd);
             $created = $thumbMaker->createThumnail($data->thumbParamsOutput->videoOffset);
             if (!$created || !file_exists($capturePath)) {
                 return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not created", KalturaBatchJobStatus::FAILED);
             }
             $this->updateJob($job, "Thumbnail captured [{$capturePath}]", KalturaBatchJobStatus::PROCESSING, 40);
         } else {
             KalturaLog::info("Source file is already an image");
         }
         $uniqid = uniqid('thumb_');
         $thumbPath = realpath($rootPath) . "/{$uniqid}";
         $quality = $data->thumbParamsOutput->quality;
         $cropType = $data->thumbParamsOutput->cropType;
         $cropX = $data->thumbParamsOutput->cropX;
         $cropY = $data->thumbParamsOutput->cropY;
         $cropWidth = $data->thumbParamsOutput->cropWidth;
         $cropHeight = $data->thumbParamsOutput->cropHeight;
         $bgcolor = $data->thumbParamsOutput->backgroundColor;
         $width = $data->thumbParamsOutput->width;
         $height = $data->thumbParamsOutput->height;
         $scaleWidth = $data->thumbParamsOutput->scaleWidth;
         $scaleHeight = $data->thumbParamsOutput->scaleHeight;
         $cropper = new KImageMagickCropper($capturePath, $thumbPath, $this->taskConfig->params->ImageMagickCmd, true);
         $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor);
         if (!$cropped || !file_exists($thumbPath)) {
             return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not cropped", KalturaBatchJobStatus::FAILED);
         }
         $data->thumbPath = $thumbPath;
         $job = $this->moveFile($job, $data);
         if ($this->checkFileExists($job->data->thumbPath)) {
             $updateData = new KalturaCaptureThumbJobData();
             $updateData->thumbPath = $data->thumbPath;
             return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $updateData);
         }
         return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FAILED, $data);
     } catch (Exception $ex) {
         return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
     }
 }
 /**
  * @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);
     }
 }
 private function postConvert(KalturaBatchJob $job, KalturaPostConvertJobData $data)
 {
     KalturaLog::debug("postConvert({$job->id})");
     try {
         $mediaFile = trim($data->srcFileSyncLocalPath);
         if (!file_exists($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 {
         //			if($this->taskConfig->params->useMediaInfo)
         $mediaInfo = $this->extractMediaInfo(realpath($mediaFile));
     } catch (Exception $ex) {
         KalturaLog::err("Error: " . $ex->getMessage());
         $mediaInfo = null;
     }
     try {
         //			if(is_null($mediaInfo) && $this->taskConfig->params->useFFMpeg)
         //				$mediaInfo = $this->extractFfmpegInfo(realpath($mediaFile));
         if (is_null($mediaInfo)) {
             return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: {$mediaFile}", KalturaBatchJobStatus::FAILED);
         }
         KalturaLog::debug("flavorAssetId [{$data->flavorAssetId}]");
         $mediaInfo->flavorAssetId = $data->flavorAssetId;
         $mediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo);
         // must save the mediaInfoId before reporting that the task is finished
         $this->updateJob($job, "Saving media info id {$mediaInfo->id}", KalturaBatchJobStatus::PROCESSED, 50, $data);
         $data->thumbPath = null;
         if ($data->createThumb) {
             // creates a temp file path
             $rootPath = $this->taskConfig->params->localTempPath;
             if (!is_dir($rootPath)) {
                 if (!file_exists($rootPath)) {
                     KalturaLog::info("Creating temp thumbnail directory [{$rootPath}]");
                     mkdir($rootPath);
                 } else {
                     // already exists but not a directory
                     KalutraLog::err("Cannot create temp thumbnail directory [{$rootPath}] due to an error. Please fix and restart");
                     die;
                 }
             }
             // creates the path
             $uniqid = uniqid('thumb_');
             $thumbPath = realpath($rootPath) . "/{$uniqid}";
             $videoDurationSec = floor($mediaInfo->videoDuration / 1000);
             $data->thumbOffset = max(0, min($data->thumbOffset, $videoDurationSec));
             $width = $mediaInfo->videoWidth;
             $height = $mediaInfo->videoHeight;
             // generates the thumbnail
             $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $thumbPath, $this->taskConfig->params->FFMpegCmd);
             $created = $thumbMaker->createThumnail($data->thumbOffset, $width, $height);
             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);
         }
         return $this->closeJob($job, null, null, "Media info id {$mediaInfo->id} saved", KalturaBatchJobStatus::FINISHED, $data);
     } catch (Exception $ex) {
         return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
     }
 }