private function sentToCenversion($write_to_log = true)
 {
     try {
         $debug = array("before getFileToConvert");
         list($before_archiving_file_path, $file_name, $in_proc) = $this->getFileToConvert($write_to_log);
         $debug[] = "after getFileToConvert [{$before_archiving_file_path}] [{$file_name}]";
         if (!$before_archiving_file_path) {
             return;
         }
         // TODO - check if this file failed too many times ...
         //if ( !$this->shouldHandleFile ( $file_name ) )
         $entry_id = self::getEntryIdFromFileName($file_name);
         $debug[] = "entry_id [{$entry_id}]";
         // we have to retrieve the path of the entry - do so by setting the data to the file path (now rather than at the end)
         $entry = entryPeer::retrieveByPK($entry_id);
         $conv_profile = myPartnerUtils::getConversionProfileForEntry($entry_id);
         $debug[] = "conversion profile of class [" . get_class($conv_profile) . "]";
         if (!$entry) {
             KalturaLog::debug("entry id [{$entry_id}] not found!");
             return;
         }
         // the conversion target should be the entry's dataPath
         $flv_file_name = kConversionHelper::flvFileName($before_archiving_file_path);
         $debug[] = "flv_file_name [{$flv_file_name}]";
         $entry->setData(null);
         $entry->setData($flv_file_name);
         // we assume the output will be of type FLV
         $entry->save();
         $archive_file_sync = $this->archiveFile($before_archiving_file_path);
         $archived_file_path = $archive_file_sync->getFullPath();
         $debug[] = "archived_file_path [{$archived_file_path}]";
         if ($conv_profile->getBypassFlv() && kConversionHelper::isFlv($archived_file_path)) {
             $conv_cmd = $this->createConversionCommandFromConverionProfile($archived_file_path, $archived_file_path, $conv_profile, $entry);
             $debug[] = "before createConversionInDb[{$entry_id}] [{$archived_file_path}]";
             // first update the DB
             $this->createConversionInDb($entry_id, $archived_file_path, $conv_cmd);
             // TODO - check if there is a set of convParams for this FLV profile and manye some conversion should be done
             // for the edit version ??
             KalturaLog::debug("Bypassing conversion for entry_id [{$entry_id}] file [{$file_name}]");
             $conv_res = new kConversionResult($conv_cmd);
             $conv_res_info = new kConvResInfo();
             $conv_res_info->target = $archived_file_path;
             $start = microtime(true);
             // FileSync - soft copy
             $archived_sync_key = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_ARCHIVE);
             $data_sync_key = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA);
             kFileSyncUtils::softCopy($archived_sync_key, $data_sync_key);
             $end = microtime(true);
             $conv_res_info->duration = $end - $start;
             $conv_res_info->conv_str = "NO CONVERT";
             $conv_res->appendResInfo($conv_res_info);
             $this->updateConvertedEntry(true, $entry, $conv_res);
             $this->removeInProc($in_proc);
             return;
         }
         // FileSync - create file sync for the future place of the converted data
         $data_sync_key = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA);
         kFileSyncUtils::createSyncFileForKey($data_sync_key, false, false);
         $full_target_path = kFileSyncUtils::getLocalFilePathForKey($data_sync_key, true);
         if ($conv_profile->getProfileTypeSuffix() == "edit") {
             // FileSync - create file sync for the future place of the converted data in edit flavor
             $data_edit_sync_key = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA_EDIT);
             kFileSyncUtils::createSyncFileForKey($data_edit_sync_key, false, false);
         }
         $debug[] = "full_target_path [{$full_target_path}]";
         $conv_cmd = $this->createConversionCommandFromConverionProfile($archived_file_path, $full_target_path, $conv_profile, $entry);
         $debug[] = "before createConversionInDb[{$entry_id}] [{$archived_file_path}]";
         // first update the DB
         $this->createConversionInDb($entry_id, $archived_file_path, $conv_cmd);
         KalturaLog::debug("Setting ConversionCommand for file [{$file_name}]\n" . print_r($conv_cmd, true));
         $debug[] = "before saveConversionCommand";
         // then save the conversion command
         $cmd_file_path = $this->saveConversionCommand();
         $this->removeInProc($in_proc);
         KalturaLog::debug("Set ConversionCommand for file [{$file_name}] in [{$cmd_file_path}]");
         $debug[] = "end";
     } catch (kConversionException $kcoe) {
         $this->removeInProc($in_proc);
         KalturaLog::debug("Error:\n" . $kcoe->getMessage() . "\n" . $kcoe->getTraceAsString() . "\n" . print_r($debug));
         // update the entry with the error sttus and the error message to the conversion result
         $conv_res = new kConversionResult($conv_cmd);
         $conv_res->appendResult($kcoe->getMessage());
         $this->updateConvertedEntry(false, $entry, $conv_res);
     } catch (Exception $ex) {
         $this->removeInProc($in_proc);
         KalturaLog::debug("Error:\n" . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n" . print_r($debug));
         // if this failed for some unknown reason - set it for reconversion
         $indicator = $this->setFileToReConvert($before_archiving_file_path, $file_name);
         KalturaLog::debug("... will reconvert [" . print_r($indicator, true) . "]");
         throw $ex;
     }
 }
 public function convert(kConversionCommand $conv_cmd, kConversionResult $conv_result, $start_params_index = 0, $end_params_index = -1)
 {
     if (!file_exists($conv_cmd->source_file)) {
         TRACE("File [{$conv_cmd->source_file} does not exist");
         return array(false, 0);
     }
     // make sure all the output directories exist - if not create
     kFile::fullMkdir($conv_cmd->target_file);
     kFile::fullMkdir($conv_cmd->log_file);
     self::fixConvParams($conv_cmd);
     $conv_params_list = $conv_cmd->conversion_params_list;
     if ($end_params_index == -1) {
         $end_params_index = count($conv_params_list);
     }
     $start_i = max($start_params_index, 0);
     $end_i = min($end_params_index, count($conv_params_list));
     for ($i = $start_i; $i < $end_i; ++$i) {
         $conv_res_info = new kConvResInfo();
         $conv_res_info->engine = $this->getName();
         $conv_res_info->index = $i;
         $conv_params = @$conv_cmd->conversion_params_list[$i];
         if ($conv_params) {
             $conv_res_info->conv_params_name = $conv_params->name;
         }
         $log_file = $conv_cmd->getLogFileWithSuffix($i);
         $conv_res_info->target = $conv_cmd->getTargetFileWithSuffix($i);
         list($execution_command_str, $conversion_str) = $this->getExecutionCommandAndConversionString($conv_cmd, $i);
         $conv_res_info->conv_str = $conversion_str;
         // assume there always will be this index
         self::logMediaInfo($conv_cmd->source_file);
         self::addToLogFile($log_file, $execution_command_str);
         self::addToLogFile($log_file, $conversion_str);
         $return_value = "";
         $conv_result->appendResult($this->getName() . ": " . $execution_command_str);
         TRACE($execution_command_str);
         $start = microtime(true);
         exec($execution_command_str, $output, $return_value);
         $end = microtime(true);
         TRACE($this->getName() . ": [{$return_value}]");
         // $return_value == 0 is success. if not - return the index of the failed conversion
         $conv_result->appendResult($this->getName() . ": [{$return_value}]");
         $conv_res_info->duration = $end - $start;
         $conv_res_info->res = $return_value;
         $conv_result->appendResInfo($conv_res_info);
         if ($return_value != 0) {
             return array(false, $i);
         }
         self::logMediaInfo($conv_cmd->getTargetFileWithSuffix($i));
     }
     return array(true, -1);
     // indicate all was converted properly
 }