<?php /** * include FFmpeg class **/ include DIRNAME(DIRNAME(__FILE__)) . '/src/ffmpeg.class.php'; /** * get options from database **/ $options = array('duration' => 99, 'position' => 0, 'itsoffset' => 2); /** * Create command */ $FFmpeg = new FFmpeg('/usr/local/bin/ffmpeg'); $FFmpeg->input('/var/media/original.avi'); $FFmpeg->transpose(0)->vflip()->grayScale()->vcodec('h264')->frameRate('30000/1001'); $FFmpeg->acodec('aac')->audioBitrate('192k'); foreach ($options as $option => $values) { $FFmpeg->call($option, $values); } $FFmpeg->output('/var/media/new.mp4', 'mp4'); print $FFmpeg->command;
/** * Serve a converted web compatible video * URL structure: projekktor/<guid>/<format>/ * * @param array $page Page segments array */ function elgg_file_viewer_projekktor_video($page) { $enable_ffmpeg = elgg_get_plugin_setting('enable_ffmpeg', 'elgg_file_viewer'); if ($enable_ffmpeg != 'yes') { return false; } $guid = elgg_extract(0, $page, null); $file = get_entity($guid); if (!elgg_instanceof($file, 'object', 'file')) { return false; } $info = pathinfo($file->getFilenameOnFilestore()); $filename = $info['filename']; $format = elgg_extract(1, $page); $output = new ElggFile(); $output->owner_guid = $file->owner_guid; $output->setFilename("projekktor/{$file->guid}/{$filename}.{$format}"); if (!$output->size()) { try { $filestorename = $output->getFilenameOnFilestore(); $output->open('write'); $output->close(); $ffmpeg_path = elgg_get_plugin_setting('ffmpeg_path', 'elgg_file_viewer'); $FFmpeg = new FFmpeg($ffmpeg_path); $FFmpeg->input($file->getFilenameOnFilestore())->output($filestorename)->ready(); elgg_log("Converting file {$file->guid} to {$format}: {$FFmpeg->command}", 'NOTICE'); } catch (Exception $e) { elgg_log($e->getMessage(), 'ERROR'); } } $mime = elgg_file_viewer_get_mime_type($file); $base_type = substr($mime, 0, strpos($mime, '/')); header("Pragma: public"); header("Content-type: {$base_type}/{$format}"); header("Content-Disposition: attachment; filename=\"{$filename}.{$format}\""); ob_clean(); flush(); readfile($output->getFilenameOnFilestore()); }
/** * Convert a video/audio file to a web compatible format * * @param ElggFile $file File entity * @param string $format Format to convert to (extension) * @return ElggFile|false */ function elgg_file_viewer_convert_file($file, $format) { if (!$file instanceof ElggFile || !$format) { return false; } $ffmpeg_path = elgg_get_plugin_setting('ffmpeg_path', 'elgg_file_viewer'); if (!$ffmpeg_path) { return false; } $info = pathinfo($file->getFilenameOnFilestore()); $filename = $info['filename']; $output = new ElggFile(); $output->owner_guid = $file->owner_guid; $output->setFilename("projekktor/{$file->guid}/{$filename}.{$format}"); $output->open('write'); $output->close(); try { $FFmpeg = new FFmpeg($ffmpeg_path); if (!$file->icontime) { $icon = new ElggFile(); $icon->owner_guid = $file->owner_guid; $icon->setFilename("projekktor/{$file->guid}/{$filename}.jpg"); $FFmpeg->input($file->getFilenameOnFilestore())->thumb(0, 1)->output($icon->getFilenameOnFilestore())->ready(); if ($icon->exists()) { $file->icontime = time(); $file->ffmpeg_thumb = $icon->getFilename(); $prefix = 'file/'; $filestorename = $file->icontime . $filename . '.jpg'; $thumbnail = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $file->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $file->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } } } $FFmpeg->input($file->getFilenameOnFilestore())->output($output->getFilenameOnFilestore())->ready(); elgg_log("Converting file {$file->guid} to {$format}: {$FFmpeg->command}", 'NOTICE'); } catch (Exception $ex) { elgg_log($ex->getMessage(), 'ERROR'); return false; } return $output; }
<?php /** * include FFmpeg class **/ include DIRNAME(DIRNAME(__FILE__)) . '/src/FFmpeg.php'; $start = 1; $frames = 10; $size = '100x100'; $file = '/var/www/file.mp4'; $FFmpeg = new FFmpeg(); $FFmpeg->input($file)->thumb($size, $start, $frames)->ready();