/**
 * 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";
    }
}