Beispiel #1
0
/**
 * Trigger the video conversion
 */
function video_conversion_cron($hook, $entity_type, $returnvalue, $params)
{
    $ia = elgg_set_ignore_access(true);
    $videos = elgg_get_entities_from_metadata(array('type' => 'object', 'subtype' => 'video', 'limit' => 2, 'metadata_name_value_pairs' => array('name' => 'conversion_done', 'value' => 0)));
    elgg_load_library('elgg:video');
    foreach ($videos as $video) {
        $sources = $video->getSources();
        $success = true;
        foreach ($sources as $source) {
            // Converted sources may exist if previous conversion has been interrupted
            if ($source->conversion_done == true) {
                continue;
            }
            try {
                $filename = $source->getFilenameOnFilestore();
                // Create a new video file to data directory
                $converter = new VideoConverter();
                $converter->setInputFile($video->getFilenameOnFilestore());
                $converter->setOutputFile($filename);
                $converter->setResolution($source->resolution);
                $converter->setBitrate($source->bitrate);
                $result = $converter->convert();
                // Save video details
                $info = new VideoInfo($source);
                $source->resolution = $info->getResolution();
                $source->bitrate = $info->getBitrate();
                $source->conversion_done = true;
                $source->save();
                echo "<p>Successfully created video file {$source->getFilename()}</p>";
            } catch (Exception $e) {
                // Print simple error to screen
                echo "<p>Failed to create video file {$source->getFilename()}</p>";
                $success = false;
                // Print detailed error to error log
                $message = elgg_echo('VideoException:ConversionFailed', array($filename, $e->getMessage(), $converter->getCommand()));
                error_log($message);
                elgg_add_admin_notice('conversion_error', $message);
            }
        }
        if ($success) {
            $video->conversion_done = true;
            add_to_river('river/object/video/create', 'create', $video->getOwnerGUID(), $video->getGUID());
        }
    }
    elgg_set_ignore_access($ia);
    return $returnvalue;
}