/**
  * Will return for each OLD ConversionProfile a new one, IF NEEDED.
  * The old conversionProfile will hold a reference to the new one, so if already created a new one, it will be re-used.
  * All the necessary flavorParams will be added too to fit the old conversion params.
  *  
  * @param ConversionProfile $conversion_profile
  * @return conversionProfile2
  */
 public static function createConversionProfile2FromConversionProfile(ConversionProfile $old_conversion_profile)
 {
     if (!$old_conversion_profile) {
         throw new Exception("Cannot create new conversionProfile2 for null");
     }
     if ($old_conversion_profile->getConversionProfile2Id()) {
         $new_profile = conversionProfile2Peer::retrieveByPK($old_conversion_profile->getConversionProfile2Id());
         if ($new_profile) {
             // found a valid new profile - return it
             return $new_profile;
         }
     }
     // whether there was no id or no profile - create on now and set it to be the conversionProfile2Id
     $new_profile = new conversionProfile2();
     $new_profile->setPartnerId($old_conversion_profile->getPartnerId());
     $new_name = $old_conversion_profile->getName();
     $new_name = $new_name ? $new_name : "From [{$old_conversion_profile->getId()}]";
     $new_profile->setName($new_name . " " . $old_conversion_profile->getProfileType());
     if ($old_conversion_profile->getBypassFlv()) {
         $new_profile->setCreationMode(conversionProfile2::CONVERSION_PROFILE_2_CREATION_MODE_AUTOMATIC_BYPASS_FLV);
         $map = flavorParams::TAG_WEB . "," . flavorParams::TAG_MBR;
     } else {
         $new_profile->setCreationMode(conversionProfile2::CONVERSION_PROFILE_2_CREATION_MODE_AUTOMATIC);
         $map = flavorParams::TAG_WEB;
     }
     $new_profile->setInputTagsMap($map);
     // use the OLD code to simulate what was performed on the old_conversion_profile to retrieve the old_conversion_params list
     $conv_client = new kConversionClientBase("", "", "", "");
     $old_conversion_command = $conv_client->createConversionCommandFromConverionProfile("src", "target", $old_conversion_profile);
     $description = '';
     foreach ($old_conversion_command->conversion_params_list as $old_conversion_params) {
         // use the helper utility to fill the gaps
         $desc = '';
         self::fixDimensionsByAspectRatio($old_conversion_params, $desc);
         $description .= $desc;
     }
     $new_profile->setDescription($description);
     $new_profile->save();
     // at this point - the all $old_conversion_params are filled with the values used by the old conversion servers
     // transform from old to new ...
     // create the flavorParams and the flavorParamsConversionParams table
     foreach ($old_conversion_command->conversion_params_list as $old_conversion_params) {
         $new_flavor_params = new flavorParams();
         // set all the properties for the new flavor_params
         $new_flavor_params->setPartnerId($old_conversion_profile->getPartnerId());
         $new_flavor_params->setCreationMode(flavorParams::FLAVOR_PARAMS_CREATION_MODE_AUTOMATIC);
         $audio_bitrate = $old_conversion_params->audio_bitrate;
         if (!$audio_bitrate) {
             $audio_bitrate = 96;
         }
         // if empty - hard-code 96
         $new_flavor_params->setAudioBitrate($audio_bitrate);
         // default
         $new_flavor_params->setAudioChannels(0);
         // default
         $new_flavor_params->setAudioResolution(0);
         $new_flavor_params->setAudioSampleRate(0);
         if ($old_conversion_profile->getCommercialTranscoder()) {
             // first comes ON2...
             $new_flavor_params->setConversionEngines(conversionEngineType::ON2 . "," . conversionEngineType::ENCODING_COM . "," . conversionEngineType::FFMPEG . "," . conversionEngineType::FFMPEG_AUX . "," . conversionEngineType::MENCODER);
             //
             $new_flavor_params->setConversionEnginesExtraParams($old_conversion_params->flix_params . "|" . $old_conversion_params->flix_params . "|" . $old_conversion_params->ffmpeg_params . "|" . $old_conversion_params->ffmpeg_params . "|" . $old_conversion_params->mencoder_params);
         } else {
             // first comes ffmpeg ...
             $new_flavor_params->setConversionEngines(conversionEngineType::FFMPEG . "," . conversionEngineType::FFMPEG_AUX . "," . conversionEngineType::MENCODER . "," . conversionEngineType::ON2 . "," . conversionEngineType::ENCODING_COM, ",");
             //
             $new_flavor_params->setConversionEnginesExtraParams($old_conversion_params->ffmpeg_params . "|" . $old_conversion_params->ffmpeg_params . "|" . $old_conversion_params->mencoder_params . "|" . $old_conversion_params->flix_params . "|" . $old_conversion_params->flix_params);
         }
         $target_format = "flv";
         // this code will always be called for flv files
         // the format can be flv | mp4 | mov | avi | mp3
         // IMPORTANT:
         // except for the FLV videos, none of the formats should be assumed WEB - they are not supposed to be played using our player at first stage.
         switch ($target_format) {
             case "mp3":
                 $new_flavor_params->setFormat("flv");
                 $new_flavor_params->setAudioCodec(flavorParams::AUDIO_CODEC_MP3);
                 // set default mp3
                 $new_flavor_params->setVideoCodec(flavorParams::VIDEO_CODEC_VP6);
                 /* $new_flavor_params->setTags ( flavorParams::TAG_WEB ); */
                 break;
             case "mp4":
                 $new_flavor_params->setFormat($target_format);
                 $new_flavor_params->setAudioCodec(flavorParams::AUDIO_CODEC_AAC);
                 $new_flavor_params->setVideoCodec(flavorParams::VIDEO_CODEC_H264);
                 $new_flavor_params->setTags(',mp4_export');
                 break;
             case "mov":
                 $new_flavor_params->setFormat($target_format);
                 $new_flavor_params->setAudioCodec(flavorParams::AUDIO_CODEC_AAC);
                 $new_flavor_params->setVideoCodec(flavorParams::VIDEO_CODEC_H264);
                 $new_flavor_params->setTags('mov_export');
                 break;
             case "avi":
                 $new_flavor_params->setFormat($target_format);
                 $new_flavor_params->setAudioCodec(flavorParams::AUDIO_CODEC_MP3);
                 $new_flavor_params->setVideoCodec(flavorParams::VIDEO_CODEC_H264);
                 $new_flavor_params->setTags('avi_export');
                 break;
             case "flv":
                 $new_flavor_params->setFormat($target_format);
                 $new_flavor_params->setAudioCodec(flavorParams::AUDIO_CODEC_MP3);
                 $new_flavor_params->setVideoCodec(flavorParams::VIDEO_CODEC_VP6);
                 $new_flavor_params->setTags(flavorParams::TAG_WEB . "," . flavorParams::TAG_MBR);
                 break;
         }
         $new_flavor_params->setName($new_name);
         $new_flavor_params->setFrameRate(0);
         // DONT set the framerate $old_conversion_params->framerate );
         if ($old_conversion_params->gop_size == 5) {
             $new_flavor_params->setGopSize(5);
             $new_flavor_params->removeTag(flavorParams::TAG_MBR);
             $new_flavor_params->addTag(flavorParams::TAG_EDIT);
         } else {
             $new_flavor_params->setGopSize(0);
             // 0 will automatically allow default gopsize
         }
         $new_flavor_params->setWidth($old_conversion_params->width);
         $new_flavor_params->setHeight($old_conversion_params->height);
         $new_flavor_params->setVersion(1);
         $new_flavor_params->setReadyBehavior(flavorParamsConversionProfile::READY_BEHAVIOR_OPTIONAL);
         $new_flavor_params->setVideoBitrate($old_conversion_params->bitrate ? $old_conversion_params->bitrate : "");
         // TODO - fill the rest ...
         $new_flavor_params->save();
         // add to the 1-to-many table
         $flavor_params_conversion_profile = new flavorParamsConversionProfile();
         $flavor_params_conversion_profile->setConversionProfileId($new_profile->getId());
         $flavor_params_conversion_profile->setFlavorParamsId($new_flavor_params->getId());
         $flavor_params_conversion_profile->setReadyBehavior($new_flavor_params->getReadyBehavior());
         $flavor_params_conversion_profile->save();
     }
     // always add to the *source* flavotParams to the 1-to-many table
     $flavor_params_conversion_profile = new flavorParamsConversionProfile();
     $flavor_params_conversion_profile->setConversionProfileId($new_profile->getId());
     $flavor_params_conversion_profile->setFlavorParamsId(flavorParams::SOURCE_FLAVOR_ID);
     $flavor_params_conversion_profile->save();
     // point to the new profile and save the old one
     $old_conversion_profile->setConversionProfile2Id($new_profile->getId());
     $old_conversion_profile->save();
     return $new_profile;
 }