/** * ConvertVideo run function. * This function is executed, when a worker is executing a task. * The return parameter will determine, if the task will be marked completed, or be requeued. * * @param array $data The array passed to QueuedTask->createJob() * @param int $id The id of the QueuedTask * @return bool Success */ public function run($data, $id = null) { /** @var \FFMpeg\FFMpeg $ffmpeg */ $ffmpeg = \FFMpeg\FFMpeg::create(array('ffmpeg.binaries' => Configure::read('ffmpeg_path'), 'ffprobe.binaries' => Configure::read('ffprobe_path'), 'timeout' => 0, 'ffmpeg.threads' => 12)); //For debug $ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener()); $ffmpeg->getFFMpegDriver()->on('debug', function ($message) { echo $message . "\n"; }); $filepath = $data['filepath']; $filename = $data['filename']; $ext = $data['ext']; $width = $data['width']; $height = $data['height']; $video = $ffmpeg->open($filepath . $filename . $ext); $video->filters()->resize(new FFMpeg\Coordinate\Dimension($width, $height), \FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_INSET)->synchronize(); $x264 = new FFMpeg\Format\Video\X264('libfdk_aac'); $x264->setKiloBitrate(5000); $ogg = new FFMpeg\Format\Video\Ogg(); $ogg->setKiloBitrate(5000); $webm = new FFMpeg\Format\Video\WebM(); $webm->setKiloBitrate(5000); $video->save($x264, $filepath . $filename . '_1080p.mp4')->save($ogg, $filepath . $filename . '_1080p.ogg')->save($webm, $filepath . $filename . '_1080p.webm'); $this->loadModel('Media'); $file = $this->Media->findById($data['media_id']); $converted = $file['Media']['converted'] + 1; $this->Media->updateAll(array('converted' => $converted), array('id' => $file['Media']['id'])); return true; }
public function execute(InputInterface $input, OutputInterface $output) { $origin = $input->getArgument('origin'); $destination = $input->getArgument('destination'); $videos = glob($origin . '/*.{MP4}', GLOB_BRACE); foreach ($videos as $video) { // Check if video is already converted $basename = basename($video); if (!file_exists($destination . $basename)) { $progress = $this->getHelper('progress'); $progress->start($output, 100); $output->writeln('<info>Converting ' . $basename . '</info>'); // Convert $ffmpeg = \FFMpeg\FFMpeg::create(['timeout' => 0]); $ffmpegvideo = $ffmpeg->open($video); // Codec $format = new \FFMpeg\Format\Video\X264(); // Progress $format->on('progress', function ($ffmpegvideo, $format, $percentage) use($progress, $output) { // Progress $progress->setCurrent($percentage); }); // Format $format->setKiloBitrate(1200)->setAudioChannels(2)->setAudioKiloBitrate(128)->setAudioCodec('libmp3lame'); // Resize $ffmpegvideo->filters()->resize(new \FFMpeg\Coordinate\Dimension(1280, 720))->synchronize(); // Convert $ffmpegvideo->save($format, $destination . $basename); $progress->finish(); } else { $output->writeln('<comment>' . $basename . ' already exists</comment>'); } } }
<?php #namespace FlotCMS; #require __DIR__.'/bootstrap/autoload.php'; require __DIR__ . '../vendor/autoload.php'; $ffmpeg = FFMpeg\FFMpeg::create(array('ffmpeg.binaries' => 'C:/ffmpeg/bin/ffmpeg.exe', 'ffprobe.binaries' => 'C:/ffmpeg/bin/ffprobe.exe', 'timeout' => 0)); $video = $ffmpeg->open('test-files/DSCN0003.AVI'); $video->filters()->rotate(FFMpeg\Filters\Video\RotateFilter::ROTATE_270); $format = new FFMpeg\Format\Video\X264(); $format->setAudioCodec("aac"); $video->save($format, 'test-files/video.avi');
/** * Converts a video into .webm and .mp4 formats for HTML5 video * * @return void */ public function webvideo() { try { set_time_limit(0); ini_set('memory_limit', '256M'); } catch (\Exception $e) { // Nothing! } // Get params: $video_path = \Cli::option('file', null); if ($video_path === null) { return; } // We need the log package loaded for Monolog $doc_root = realpath(APPPATH . '../../public') . '/'; $config = \Config::get('cmf.ffmpeg'); $logger = new \Monolog\Logger('WebVideoConverter'); $logger->pushHandler(new \Monolog\Handler\RotatingFileHandler(APPPATH . 'logs/ffmpeg.log')); // Get path info about the video $video_path = $doc_root . $video_path; $video_id = md5($video_path); $path_info = pathinfo($video_path); $converted_dir = $path_info['dirname'] . '/converted'; $progress_file = $video_path . '.progress'; touch($progress_file); if (!is_dir($converted_dir)) { $made_dir = @mkdir($converted_dir, 0775, true); } // Set up the FFMpeg instances $ffprobe = new \FFMpeg\FFProbe($config['ffprobe_binary'], $logger); $ffmpeg = new \FFMpeg\FFMpeg($config['ffmpeg_binary'], $logger); $ffmpeg->setProber($ffprobe); // Probe the video for info $format_info = json_decode($ffprobe->probeFormat($video_path)); $video_streams = json_decode($ffprobe->probeStreams($video_path)); $video_info = null; foreach ($video_streams as $num => $stream) { if ($stream->codec_type == 'video') { $video_info = $stream; break; } } // Serve up an error if we can't find a video stream if ($video_info === null) { return; } // Determine the frame rate if (isset($video_info->r_frame_rate)) { $video_framerate = strval($video_info->r_frame_rate); $parts = explode('/', $video_framerate); $video_framerate = round(intval($parts[0]) / intval($parts[1])); } else { $video_framerate = intval($config['default_framerate']); } // Get the size $video_width = intval(isset($video_info->width) ? $video_info->width : $config['default_size']['width']); $video_height = intval(isset($video_info->height) ? $video_info->height : $config['default_size']['height']); $video_kilobitrate = round($video_width * $video_height * 0.0019); $video_duration = floatval($format_info->duration); $still_frame_pos = $video_duration * 0.1; if (isset($format_info->bit_rate) && round($format_info->bit_rate / 1024) < $video_kilobitrate) { $video_kilobitrate = round($format_info->bit_rate / 1024); } // Set up the helper that outputs conversion progress $progressHelper = new \FFMpeg\Helper\VideoProgressHelper(function ($percent, $remaining, $rate) use($progress_file) { $data = array('percent' => $percent, 'remaining' => $remaining, 'rate' => $rate); file_put_contents($progress_file, json_encode($data)); }); // Finally, convert to each format: $webMFormat = new \FFMpeg\Format\Video\WebM(); $webMFormat->setDimensions($video_width, $video_height)->setFrameRate($video_framerate)->setKiloBitrate($video_kilobitrate)->setGopSize(25); $x264Format = new \FFMpeg\Format\Video\X264(); $x264Format->setDimensions($video_width, $video_height)->setFrameRate($video_framerate)->setKiloBitrate($video_kilobitrate)->setGopSize(25); $ffmpeg->open($video_path)->attachHelper($progressHelper)->encode($webMFormat, $converted_dir . '/' . $path_info['filename'] . '.webm')->encode($x264Format, $converted_dir . '/' . $path_info['filename'] . '.mp4')->extractImage($still_frame_pos, $path_info['dirname'] . '/' . $path_info['basename'] . '.jpg')->close(); // Delete the progress file to show that the process is complete unlink($progress_file); }