/** * 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>'); } } }