/** * 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; }
} else { $basename = $video->getFilenameWithoutExtension(); $filename = "video/{$video->getGUID()}/{$basename}_{$resolution}.{$format}"; // Create a new entity that represents the physical file $source = new VideoSource(); $source->format = $format; $source->setFilename($filename); $source->setMimeType("video/{$format}"); $source->resolution = $resolution; $source->bitrate = $bitrate; $source->owner_guid = $video->getOwnerGUID(); $source->container_guid = $video->getGUID(); $source->access_id = $video->access_id; $source->save(); } try { $converter = new VideoConverter(); $converter->setInputfile($video->getFilenameOnFilestore()); $converter->setOutputfile($source->getFilenameOnFilestore()); $converter->setResolution($resolution); $converter->setBitrate($bitrate); $converter->convert(); $source->conversion_done = true; system_message(elgg_echo('video:convert:success', array($format))); } catch (exception $e) { // Delete the faulty format $source->delete(); $message = elgg_echo('VideoException:ConversionFailed', array($outputfile, $e->getMessage(), $converter->getCommand())); register_error($message); } forward("admin/video/view?guid={$guid}");