Snippets

Krzysztof Ożóg Example processing command

Created by Krzysztof Ożóg

File command.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+class ProcessVideoQueueCommand extends ContainerAwareCommand
+{
+
+    protected $videoManager = null;
+    protected $em = null;
+    protected $videoRepository = null;
+    protected $ffmpeg = null;
+
+    protected function configure()
+    {
+        $this
+            ->setName('video:convert:queue')
+            ->setDescription('Run queue of video encoding')
+        ;
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $container = $this->getContainer();
+
+        $this->videoManager = $container->get('VideoManager');
+        $this->em = $container->get('doctrine.orm.entity_manager');
+        $this->videoRepository = $this->em->getRepository('AppBundle:Video');
+        $logger = $container->get('logger');
+
+        $ffmprobe = FFMPEG/FFProbe::create()
+        $this->ffmpeg =FFMPEG/FFMpeg::create();
+        $this->ffmpeg->setProber($ffmprobe);
+
+        $videos = $this->videoRepository->getPendingVideos();
+
+        $logger->info('Number of VIDEOS TO PROCCESS: '.count($videos));
+
+        foreach ($videos as $video) {
+            $this->encode($video);
+        }        
+    }
+
+    
+    protected function encode($video)
+    {
+        $container = $this->getContainer();
+        $logger = $container->get('logger');
+        $logger->info('Proccessing video: '.$video->getId());
+
+        $video->setStatus(Video::STATUS_ENCODING);
+        $video->setEncodingStarted(new \DateTime());
+
+        $x264Format = new FFMpeg\Format\Video\X264();
+
+        $formats = array($x264Format);
+
+        try {
+
+            // MP4
+            $current = null;
+
+            foreach ($formats as $format) {
+
+                $current = $format;
+
+                $this->ffmpeg
+                    ->open($this->videoManager->getVideoRootFile($video))
+                    ->encode(
+                        $format,
+                        $this->videoManager->getVideoRootPath($video).'/'.$format->getFilename()
+                    )
+                    ->close()
+                ;
+            }
+
+
+            $video->setUpdated(new \DateTime('now'));
+            $video->setEncodingFinished(new \DateTime('now'));
+            $video->setStatus(Video::STATUS_COMPLETED);
+
+            $this->em->flush();
+          
+        } catch (\FFMpeg\Exception\RuntimeException $e) {
+
+            $logger->info('MSG: '.$e->getMessage());
+
+            $video->setStatus(Video::STATUS_ERROR);
+            $video->setEncodingErrors(
+                $video->getEncodingErrors()."\n".
+                'Format failed: '.get_class($current)
+            );
+
+            $this->em->flush();
+        }
+    }
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.