/**
 * Create a Media File for the output file encoded by Zencoder, and then attach it
 * to the Media Work it belongs to. Also create a thumbnail for the Media Work and update 
 * some metadata on the original media work with info present in the output files.
 *
 * @param $media_work entity
 * @param $notification object
 * @param $mime_type_map array
 * @param $netid string
 */
function process_video($media_work, $notification, $mime_type_map, $netid)
{
    echo 'Processing Video Output file...' . "\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));
    // prevent this script from getting activated more than once due to large file transfers.
    // Zencoder retries calling scripts if no response is received fast enough.
    if ($media_file && $media_file->get_value('url') != 'downloading') {
        echo 'Processing media file ' . $media_file->id() . "\n";
        reason_update_entity($media_file->get_value('id'), $media_work->get_owner()->get_value('id'), array('url' => 'downloading'), false);
        $url = $output->url;
        $name = basename($url);
        $url = str_replace($name, urlencode($name), $url);
        $values = array('new' => 0, 'av_type' => 'Video', '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)), 'width' => $output->width, 'height' => $output->height, 'media_is_progressively_downloadable' => true, 'url' => $url);
        ZencoderShim::get_storage_class()->update_video_media_file_in_notification_receiver($values, $format, $media_work, $media_file);
    } elseif ($label == 'original') {
        attach_thumbnail($output, $media_work, $netid);
    } else {
        echo 'No Media File with id ' . $id . ' was found.' . "\n";
        trigger_error('No Media File with id ' . $id . ' was found.');
    }
}
    include_once 'reason_header.php';
}
if (REASON_MAINTENANCE_MODE) {
    die;
}
reason_include_once('classes/media/vimeo/shim.php');
$es = new entity_selector();
$es->add_type(id_of('av'));
$es->add_relation('media_work.av_type = "Video"');
$es->add_relation('media_work.integration_library = "vimeo"');
$es->add_relation('media_work.transcoding_status = "converting"');
$media_works = array_merge($es->run_one(), $es->run_one('', 'Pending'));
$shim = new VimeoShim();
foreach ($media_works as $media_work) {
    if (update_metadata($media_work, $shim)) {
        attach_thumbnail($media_work, $shim);
        $user = new entity($media_work->get_value('created_by'));
        $netid = $user->get_value('name');
        send_email($media_work, 'success', $netid);
    }
}
function attach_thumbnail($media_work, $shim)
{
    // create image file in the vimeo temp directory
    $image_url = $shim->get_thumbnail($media_work->get_value('entry_id'));
    if ($image_url) {
        $tmp_path = VimeoShim::get_temp_dir() . 'tmp_thumbnail_' . $media_work->get_value('id');
        $f = fopen($tmp_path, 'w');
        $contents = get_reason_url_contents($image_url);
        fwrite($f, $contents);
        fclose($f);