function update_fields_with_file_data($stream) { $url = $this->get_protocol() . '://' . $stream->get_media_url($this->manager->get_value('id')); $this->manager->set_value('url', $url); $this->manager->set_value('reason_managed_media', 1); $bytes = $stream->get_media_size_bytes($this->manager->get_value('id')); if (!empty($bytes)) { $this->manager->set_value('media_size_in_bytes', $bytes); $this->manager->set_value('media_size', format_bytes_as_human_readable($bytes)); } if (!empty($this->checksum)) { $this->manager->set_value('media_md5_sum', $this->checksum); } $duration = $stream->get_media_duration($this->manager->get_value('id')); if (!empty($duration)) { $human_readable_duration = $this->get_human_readable_duration($duration); if (!empty($human_readable_duration)) { $this->manager->set_value('media_duration', $human_readable_duration); } } $this->send_email_notification(); }
function _get_current_file_display($current) { if (!$current || $this->too_big) { return ''; } if ($current->path && $current->uri) { list($width, $height) = getimagesize($current->path); $disk_size = format_bytes_as_human_readable($current->size); $dimensions = "{$width}×{$height}"; $uri = htmlspecialchars($current->uri) . '?_nocache=' . time(); $img_style = ' style="width: ' . $width . 'px; ' . 'height: ' . $height . 'px;"'; $div_style = ''; } else { $uri = $img_style = $dimensions = $disk_size = ''; $div_style = ' style="display: none;"'; } $image_size = '<span class="dimensions">' . $dimensions . '</span> ' . '(<span class="filesize">' . $disk_size . '</span>)'; return '<div class="uploaded_file uploaded_image"' . $div_style . '>' . '<span class="smallText">Uploaded image:</span><br />' . '<img src="' . $uri . '"' . $img_style . ' class="representation" />' . '<br /><span class="size">' . $image_size . '</span></div>'; }
/** * The upload element is added to the form. */ function _add_file_upload_element() { if ($this->manager->manages_media && $this->manager->get_value('transcoding_status') != 'converting') { $authenticator = array("reason_username_has_access_to_site", $this->manager->get_value("site_id")); $params = array('authenticator' => $authenticator, 'acceptable_extensions' => $this->recognized_extensions, 'max_file_size' => $this->_get_actual_max_upload_size(), 'head_items' => &$this->manager->head_items); $this->manager->add_element('upload_file', 'ReasonUpload', $params); $this->manager->set_comments('upload_file', form_comment('If the file is on your computer, browse to it here.') . form_comment('File must have one of the following extensions: .' . implode(', .', $this->recognized_extensions)) . form_comment('<div class="maxUploadSizeNotice">Maximum file size for uploading is ' . format_bytes_as_human_readable($this->_get_actual_max_upload_size()) . '. </div>')); $this->manager->add_element('upload_url'); $this->manager->add_comments('upload_url', form_comment('Or, you can place the media in any web-accessible location and paste its web address in here. <em>Tip: try pasting the address into another tab first, to make sure you have the address right!</em>')); if ($this->manager->get_value('transcoding_status') == 'ready') { $this->manager->set_display_name('upload_file', 'Upload Replacement File'); } } }
/** * 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.'); } }
} /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// // display the page /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// echo $page; // if we're using a cache, make sure to store the new result // also, record stats about misses here $page_gen_time = round(1000 * (get_microtime() - $s)); if ($use_cache and !$cache_hit) { $cache->set_page_generation_time($page_gen_time); $cache->store(get_current_url(), $page); } if (is_developer() && !$requested_api) { $str = $page_gen_time . ' ms | ' . format_bytes_as_human_readable(memory_get_peak_usage(true)) . ' | '; if ($use_cache) { $str .= 'caching is ON: '; if ($cache_hit) { $str .= 'hit'; } else { $str .= 'miss'; } } else { $str .= 'caching is OFF: ' . implode(', ', $no_cache_reasons); } echo "\n" . '<div id="reasonDeveloper" style="background-color:#ddd;color:#555;font-size:0.75em;padding:1px 1em;">'; echo '<p>' . $str . '</p>'; if (isset($t) && method_exists($t, 'display_developer_section')) { $t->display_developer_section(); }
function foot() { echo '</td></tr></table>' . "\n"; echo '</div>' . "\n"; if (defined('THIS_IS_A_DEVELOPMENT_REASON_INSTANCE') && THIS_IS_A_DEVELOPMENT_REASON_INSTANCE) { echo '<div id="reasonDeveloper" style="background-color:#ddd;color:#555;font-size:0.75em;padding:1px 1em;">'; $time = round((microtime(true) - $this->start_time) * 1000, 2); printf('<p>%s ms | %s</p>', $time, format_bytes_as_human_readable(memory_get_peak_usage(true))); echo '</div>'; } echo '</body>' . "\n"; echo '</html>' . "\n"; }
/** * Creates a media file whose url is a file that doesn't need to go through Zencoder because * it's either an mp3 or ogg. * @param $filepath string * @param $media_work entity = * @param $user_id int */ public static function create_audio_source_file($filepath, $media_work, $user_id) { $filename = basename($filepath); $extension = end(explode('.', $filename)); if ($extension == 'mp3') { $mime_type = 'audio/mpeg'; } else { $mime_type = 'audio/ogg'; } $needs_deleting = false; if (strpos($filepath, 'http://') !== 0 && strpos($filepath, 'https://') !== 0) { $base_path = str_replace($filename, '', $filepath); $filepath = $base_path . $filename; $filesize = filesize($filepath); } $media_file_id = reason_create_entity($media_work->get_owner()->id(), id_of('av_file'), $user_id, $media_work->get_value('name') . ' ' . $extension, array('av_type' => 'Audio', 'flavor_id' => 'original')); create_relationship($media_work->id(), $media_file_id, relationship_id_of('av_to_av_file')); $web_url = self::get_storage_class()->store_media($media_file_id, $filepath, '', $media_work, 'original'); $values = array('media_format' => 'HTML5', 'url' => $web_url, 'download_url' => $web_url, 'mime_type' => $mime_type, 'media_size_in_bytes' => $filesize, 'media_size' => format_bytes_as_human_readable($filesize), 'media_quality' => self::_get_bitrate($filepath) . ' kbps', 'media_duration' => self::_get_duration($filepath)); reason_update_entity($media_file_id, $media_work->get_owner()->id(), $values, false); }
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. } } }
/** * Called in the Zencoder notification receiver after the Reason entity has been created. * This hooks up the original mp3 or ogg file to a Reason entity, if needed. * * @param $filepath string * @param $media_work entity * @param $num_outputs int * @param $shim obj * @param $netid string */ public static function post_process_audio($filepath, $media_work, $num_outputs, $shim, $netid) { // We need to create a media file for the source mp3 or ogg file if ($num_outputs == 2) { $filename = end(explode('/', $filepath)); $extension = end(explode('.', $filename)); if ($extension == 'mp3') { $mime_type = 'audio/mpeg'; } elseif ($extension == 'ogg') { $mime_type = 'audio/ogg'; } else { trigger_error('Invalid extension: ' . $extension); } $path = self::get_path(false, $filename, $media_work, 'original'); $base_url = self::get_base_url(); $client = self::_get_client(); if (!$client) { trigger_error('Could not create S3 object.'); return; } $web_url = $base_url . $path; $obj_info_arr = $client->getObjectInfo(S3_BUCKET_NAME, $path); $filesize = $obj_info_arr['size']; $name = basename($web_url); $web_url = str_replace($name, urlencode($name), $web_url); $values = array('av_type' => 'Audio', 'flavor_id' => 'original', 'media_format' => 'HTML5', 'url' => $web_url, 'download_url' => $web_url, 'mime_type' => $mime_type, 'media_size_in_bytes' => $filesize, 'media_size' => format_bytes_as_human_readable($filesize), 'media_quality' => $shim->_get_bitrate($filepath) . ' kbps', 'media_duration' => $shim->_get_duration($filepath)); $media_file_id = reason_create_entity($media_work->get_owner()->id(), id_of('av_file'), get_user_id($netid), $media_work->get_value('name') . ' ' . $extension, $values); create_relationship($media_work->id(), $media_file_id, relationship_id_of('av_to_av_file')); } }
function update_entity_values($media_work, $data) { $flavor_assets = $this->kaltura_shim->get_flavor_assets_for_entry($data['entry_id'], $data['puser_id']); $valid_flavors = $this->get_valid_flavors($media_work, $flavor_assets, $data); $owner = $media_work->get_owner(); // Determine what the av_type field should be. Is it a Video or Audio file? if ($data['media_type'] == KalturaMediaType::VIDEO) { $media_type = 'Video'; } elseif ($data['media_type'] == KalturaMediaType::AUDIO) { $media_type = 'Audio'; } else { trigger_error('invalid media type from Kaltura Server notification: ' . $data['media_type']); } foreach ($valid_flavors as $asset) { $asset_id = $asset->id; $convert_asset_to_reason = true; if ($media_type == 'Audio' && count($flavor_assets) > 2 && $asset->flavorParamsId == 0) { $convert_asset_to_reason = false; } else { if ($media_type == 'Video' && $asset->flavorParamsId == 0) { $convert_asset_to_reason = false; } } // don't create a Reason Media File for the source flavor of videos or a source flavor for a non-mp4 or non-ogg audio file if ($convert_asset_to_reason) { $format = $this->kaltura_shim->CONTAINER_TO_MIME_MAP[$asset->containerFormat]; $values = array('url' => $data['data_url'] . '/flavor/' . $asset_id, 'download_url' => $this->kaltura_shim->get_flavor_download_url($asset_id), 'media_size_in_bytes' => $asset->size * 1024, 'media_size' => format_bytes_as_human_readable($asset->size * 1024), 'default_media_delivery_method' => 'progressive_download', 'media_format' => 'HTML5', 'width' => $asset->width, 'height' => $asset->height, 'media_is_progressively_downloadable' => true, 'media_is_streamed' => false, 'media_quality' => $asset->bitrate . ' kbps', 'flavor_id' => $asset_id, 'mime_type' => strtolower($media_type) . '/' . $format, 'media_duration' => $this->get_human_readable_duration($data['length_in_msecs']), 'av_type' => $media_type); $flavor_name = $media_work->get_value('name') . ' - ' . $format; if ($media_type == 'Video') { $flavor_name .= ' (' . $asset->width . 'x' . $asset->height . ')'; } $flavor_entity_id = reason_create_entity($owner->id(), id_of('av_file'), get_user_id($data['puser_id']), $flavor_name, $values); $media_files[] = new entity($flavor_entity_id); create_relationship($media_work->id(), $flavor_entity_id, relationship_id_of('av_to_av_file')); } } if ($this->all_flavors_complete == true) { $media_work_values = array('transcoding_status' => 'ready', 'media_duration' => $this->get_human_readable_duration($data['length_in_msecs'])); reason_update_entity($media_work->id(), $data['puser_id'], $media_work_values, false); } else { $media_work_values = array('transcoding_status' => 'converting'); reason_update_entity($media_work->id(), $data['puser_id'], $media_work_values, false); } }