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.
         }
     }
 }
/**
 * Our scheme for processing audio files is much simpler than videos. We simply receive a notification
 * from Zencoder containing data about our desired output formats and create media files with it.
 *
 * @param $media_work entity
 * @param $notification object
 * @param $mime_type_map array
 * @param $netid string
 */
function process_audio($media_work, $notification, $mime_type_map, $netid)
{
    echo 'Processing Audio...' . "\n";
    $output = current($notification->job->outputs);
    $label = $output->label;
    $label_parts = explode('_', $label);
    $format = reset($label_parts);
    $id = end($label_parts);
    // get Reason media file for this output file
    $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 = get_human_readable_duration(intval($output->duration_in_ms));
    if ($media_file) {
        echo 'Processing media file ' . $media_file->id();
        $url = $output->url;
        $name = basename($url);
        $url = str_replace($name, urlencode($name), $url);
        $values = array('av_type' => 'Audio', 'media_format' => 'HTML5', 'media_size_in_bytes' => $output->file_size_in_bytes, 'media_size' => format_bytes_as_human_readable(intval($output->file_size_in_bytes)), 'media_quality' => $output->audio_bitrate_in_kbps . ' kbps', 'mime_type' => $mime_type_map[$format], 'media_duration' => get_human_readable_duration(intval($output->duration_in_ms)), 'url' => $url);
        ZencoderShim::get_storage_class()->update_audio_media_file_in_notification_receiver($values, $format, $media_work, $media_file);
    } else {
        echo 'Media File with id' . $id . ' could not be stored.' . "\n";
        trigger_error('Media File with id ' . $id . ' could not be stored.');
    }
}
/**
 * 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";
    }
}