$output = current($notification->job->outputs);
        $label = $output->label;
        $label_parts = explode('_', $label);
        $format = reset($label_parts);
        $id = end($label_parts);
        echo 'Expunging Media File ' . $id . "\n";
        reason_expunge_entity($id, $user->id());
    } elseif ($output->state != "finished") {
        echo 'Output ' . $output->id . ' for job ' . $notification->job->id . ' not successful in encoding.' . "\n";
        echo 'There were errors or cancellations in the transcoding process.' . "\n";
        set_error($media_work);
    } else {
        if ($media_work->get_value('av_type') == 'Video') {
            process_video($media_work, $notification, $mime_type_map, $netid);
        } elseif ($media_work->get_value('av_type') == 'Audio') {
            process_audio($media_work, $notification, $mime_type_map, $netid);
        } else {
            echo 'Media Work with id ' . $media_work->id() . ' has invalid av_type.' . "\n";
            set_error($media_work);
        }
    }
} else {
    echo 'No media work with entry_id ' . addslashes($notification->job->id) . ' found in database.' . "\n";
    set_error($media_work);
}
////////////////////////////////////////////////////////////////////////////////////
/**
 * 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.
 *
    if ($media_work->get_value('transcoding_status') == 'converting') {
        // first check to see if all successfully transcoded
        $success = true;
        foreach ($notification->job->outputs as $label => $obj) {
            if ($obj->state != "finished") {
                $sucess = false;
                echo $obj->id . ' not successful in encoding.' . "\n";
                break;
            }
        }
        if ($success) {
            reason_update_entity($media_work->get_value('id'), $media_work->get_owner()->get_value('id'), array('transcoding_status' => 'finalizing'), false);
            if ($media_work->get_value('av_type') == 'Video') {
                process_video($media_work, $notification, $netid);
            } elseif ($media_work->get_value('av_type') == 'Audio') {
                process_audio($media_work, $notification, $netid);
            } else {
                echo 'Media Work with id ' . $media_work->id() . ' has invalid av_type.' . "\n";
                set_error($media_work);
                send_email($media_work, $notification->job, 'error', $netid);
            }
        } else {
            echo 'There were errors or cancellations in the transcoding process.' . "\n";
            set_error($media_work);
            send_email($media_work, $notification->job, 'error', $netid);
        }
    } elseif ($media_work->get_value('transcoding_status') == 'error') {
        send_email($media_work, $notification->job, 'error', $netid);
    }
} else {
    echo 'No media work with entry_id ' . addslashes($notification->job->id) . ' found in database.' . "\n";