Example #1
0
 /**
  * Zencoder integration has been designed to work with either Amazon S3 or Reason file
  * storage.
  */
 public static function get_storage_class()
 {
     if (!self::$storage_class) {
         self::$storage_class = self::_get_storage_class();
     }
     return self::$storage_class;
 }
 function set_content_manager($manager)
 {
     $this->manager = $manager;
     $manager->recognized_extensions = array();
     $this->shim = new ZencoderShim();
     if ($manager->get_value('entry_id')) {
         $this->displayer_chrome = new ZencoderSizeSwitchDisplayerChrome();
         $this->displayer_chrome->set_media_work(new entity($manager->get_value('id')));
     }
     $this->recognized_extensions = ZencoderShim::get_recognized_extensions();
 }
/**
 * Attempt to attach a thumbnail to the media work.
 *
 * @param $obj object
 * @param $media_work entity
 * @param $netid string
 */
function attach_thumbnail($obj, $media_work, $netid)
{
    // check to see if this had thumbnails generated
    if (property_exists($obj, 'thumbnails')) {
        // First, create temp file
        $tmp_path = ZencoderShim::get_temp_dir() . 'temp_media_image_' . $media_work->get_value('entry_id') . '.jpg';
        $f = fopen($tmp_path, 'w');
        $image_url = $obj->thumbnails[0]->images[0]->url;
        $contents = get_reason_url_contents($image_url);
        fwrite($f, $contents);
        fclose($f);
        // Then, create image entity.  Attach it to the media work, too.
        if (!empty($tmp_path) and file_exists($tmp_path)) {
            // Create a new entity for the image
            if ($id = create_image_entity($media_work, $netid)) {
                $im = new ImageManager();
                //$im->convert_non_web_to = $this->convert_non_web_to;
                $im->thumbnail_width = REASON_STANDARD_MAX_THUMBNAIL_WIDTH;
                $im->thumbnail_height = REASON_STANDARD_MAX_THUMBNAIL_HEIGHT;
                $im->max_width = REASON_STANDARD_MAX_IMAGE_WIDTH;
                $im->max_height = REASON_STANDARD_MAX_IMAGE_HEIGHT;
                $im->load_by_type(id_of('image'), $id, get_user_id($netid));
                $im->handle_standard_image($id, $tmp_path);
                //$im->handle_original_image($id, $image);
                $im->create_default_thumbnail($id);
                $dimensions = $obj->thumbnails[0]->images[0]->dimensions;
                $arr = explode('x', $dimensions);
                $thumb_width = intval($arr[0]);
                $thumb_height = intval($arr[1]);
                if ($thumb_width > $im->max_width || $thumb_height > $im->max_height) {
                    $image_path = PHOTOSTOCK . reason_format_image_filename($id, 'jpg');
                    $original_path = add_name_suffix($image_path, '_orig');
                    @copy($image_path, $original_path);
                    resize_image($image_path, $im->max_width, $im->max_height);
                }
                // Pull the values generated in the content manager
                // and save them to the entity
                $values = array();
                foreach ($im->get_element_names() as $element_name) {
                    $values[$element_name] = $im->get_value($element_name);
                }
                reason_update_entity($id, get_user_id($netid), $values, false);
                // Remove any existing association with an image and replace it with this new one
                delete_relationships(array('entity_a' => $media_work->id(), 'type' => relationship_id_of('av_to_primary_image')));
                create_relationship($media_work->id(), $id, relationship_id_of('av_to_primary_image'));
            } else {
                trigger_error('Failed to create image entity.');
            }
        } else {
            trigger_error('No path to image: ' . $tmp_path);
        }
    }
}
 function run()
 {
     if (REASON_MAINTENANCE_MODE) {
         die("This module can't be run in Reason Maintenance Mode.");
     }
     //if ($this->media_work->get_value('transcoding_status') != 'ready') die('This media work must have a transcoding status of \'ready\' to use this module.');
     if (!array_key_exists('run', $this->admin_page->request) || !$this->admin_page->request['run']) {
         echo '<p>Attempt to re-download the media files from Zencoder. This will only work if the job was submitted less than 24 hours ago.</p>' . "\n";
         $url = carl_make_link(array('run' => '1'));
         echo '<p><a href="' . $url . '">Re-Download Files</a></p>' . "\n";
         if ($this->media_work->get_value('av_type') == 'Audio') {
             echo '<p>Re-encode the audio file with Zencoder.</p>' . "\n";
             $url = carl_make_link(array('run' => '2'));
             echo '<p><a href="' . $url . '">Re-Encode Audio</a></p>' . "\n";
         }
     } else {
         if ($this->admin_page->request['run'] == 1) {
             $zencoder = new Services_Zencoder(ZENCODER_FULL_ACCESS_KEY);
             $values = array('transcoding_status' => 'finalizing');
             reason_update_entity($this->media_work->id(), $this->media_work->get_owner()->id(), $values, false);
             $mime_type_map = array('mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'webm' => 'video/webm', 'mp4' => 'video/mp4');
             $outputs = $zencoder->jobs->details($this->media_work->get_value('entry_id'))->outputs;
             if ($this->media_work->get_value('av_type') == 'Video') {
                 foreach ($outputs as $label => $obj) {
                     if (!$label) {
                         continue;
                     }
                     $label_parts = explode('_', $label);
                     $format = reset($label_parts);
                     $id = end($label_parts);
                     $es = new entity_selector();
                     $es->add_type(id_of('av_file'));
                     $es->add_relation('entity.id = "' . addslashes($id) . '"');
                     $media_file = current(array_merge($es->run_one(), $es->run_one('', 'Pending')));
                     $duration = $this->get_human_readable_duration(intval($obj->duration_in_ms));
                     if (ZencoderShim::get_storage_class()->output_url_has_expired($obj->url)) {
                         echo '<p>The url for Media File ' . $media_file->id() . ' has expired. Unable to re-download.</p>';
                         continue;
                     }
                     if ($media_file) {
                         echo '<p>Processing media file ' . $media_file->id() . '</p>' . "\n";
                         $values = array('new' => 0, 'av_type' => 'Video', 'media_format' => 'HTML5', 'media_size_in_bytes' => $obj->file_size_bytes, 'media_size' => format_bytes_as_human_readable(intval($obj->file_size_bytes)), 'media_quality' => $obj->audio_bitrate_in_kbps . ' kbps', 'mime_type' => $mime_type_map[$format], 'media_duration' => $this->get_human_readable_duration(intval($obj->duration_in_ms)), 'width' => $obj->width, 'height' => $obj->height, 'media_is_progressively_downloadable' => true, 'url' => $obj->url);
                         ZencoderShim::get_storage_class()->update_video_media_file_in_notification_receiver($values, $format, $this->media_work, $media_file);
                     } else {
                         trigger_error('No Media File with id ' . $id . ' was found.');
                     }
                 }
                 $this->finish_processing($this->media_work, $this->user->get_value('name'), $duration);
             } else {
                 foreach ($outputs as $label => $obj) {
                     $label_parts = explode('_', $label);
                     $format = reset($label_parts);
                     $id = end($label_parts);
                     $es = new entity_selector();
                     $es->add_type(id_of('av_file'));
                     $es->add_relation('entity.id = "' . addslashes($id) . '"');
                     $media_file = current(array_merge($es->run_one(), $es->run_one('', 'Pending')));
                     $duration = $this->get_human_readable_duration(intval($obj->duration_in_ms));
                     if (ZencoderShim::get_storage_class()->output_url_has_expired($obj->url)) {
                         echo '<p>The url for Media File ' . $media_file->id() . ' has expired. Unable to re-download.</p>';
                         continue;
                     }
                     if ($media_file) {
                         echo '<p>Processing media file ' . $media_file->id() . '</p>' . "\n";
                         $values = array('av_type' => 'Audio', 'media_format' => 'HTML5', 'media_size_in_bytes' => $obj->file_size_bytes, 'media_size' => format_bytes_as_human_readable(intval($obj->file_size_bytes)), 'media_quality' => $obj->audio_bitrate_in_kbps . ' kbps', 'mime_type' => $mime_type_map[$format], 'media_duration' => $this->get_human_readable_duration(intval($obj->duration_in_ms)), 'url' => $obj->url);
                         ZencoderShim::get_storage_class()->update_audio_media_file_in_notification_receiver($values, $format, $this->media_work, $media_file);
                     } else {
                         echo '<p>Media File with id' . $id . ' could not be stored.</p>' . "\n";
                         trigger_error('Media File with id ' . $id . ' could not be stored.');
                     }
                 }
                 $this->finish_processing($this->media_work, $this->user->get_value('name'), $duration);
             }
         } elseif ($this->admin_page->request['run'] == 2) {
             // submit new job to Zencoder using original file
             reason_include_once('classes/media/zencoder/shim.php');
             $shim = new ZencoderShim();
             if (strpos($this->media_work->get_value('tmp_file_name'), 'http') === 0) {
                 $job = $shim->upload_audio($this->media_work->get_value('tmp_file_name'), $this->media_work, $this->user->get_value('name'), true, true);
             } else {
                 $path = $shim->get_storage_class()->get_path('', '', $this->media_work, 'original');
                 $job = $shim->upload_audio($path, $this->media_work, $this->user->get_value('name'), false, true);
             }
             if (!$job) {
                 echo '<p>There was an error during the upload process. Audio files could not be re-encoded.</p>' . "\n";
             } else {
                 echo '<p>Audio files have begun encoding with Zencoder.</p>' . "\n";
                 reason_update_entity($this->media_work->get_value('id'), $this->media_work->get_owner()->get_value('id'), array('transcoding_status' => 'converting', 'entry_id' => $job->id), false);
             }
             // update metadata of media work.  make sure the s3 stuff works.
         }
     }
 }
/**
 * Update some metadata to finalize the media work. Store the original file, if needed. And clean 
 * up the temporary file, if needed.
 *
 * @param $media_work entity
 * @param $notification object
 * @param $netid string
 */
function process_audio($media_work, $notification, $netid)
{
    if (!$notification->job->pass_through) {
        echo 'Processing Audio...' . "\n";
        $output = reset($notification->job->outputs);
        $duration = get_human_readable_duration(intval($output->duration_in_ms));
        $num_outputs = count($notification->job->outputs);
        $filepath = $media_work->get_value('tmp_file_name');
        if (strpos($filepath, 'http') !== 0) {
            $filepath = WEB_PATH . WEB_TEMP . $filepath;
        }
        $shim = new ZencoderShim();
        finish_processing($notification, $media_work, $netid, $duration);
        ZencoderShim::get_storage_class()->post_process_audio($filepath, $media_work, $num_outputs, $shim, $netid);
        // delete the original file from our server
        if (strpos($media_work->get_value('tmp_file_name'), 'http') !== 0) {
            unlink(WEB_PATH . WEB_TEMP . $media_work->get_value('tmp_file_name'));
        }
    } elseif ($notification->job->pass_through == 'reencoding_audio') {
        echo 'Processing reencoded Audio...' . "\n";
        $values = array('transcoding_status' => 'ready');
        reason_update_entity($media_work->id(), $media_work->get_owner()->id(), $values, false);
    } else {
        echo 'Unrecognized pass_through value: ' . $notification->job->pass_through . "\n";
    }
}