Proyectos de Subversion LeadersLinked - Services

Rev

Rev 159 | Rev 333 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Laminas\Db\Adapter\AdapterInterface;

use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\VideoConvertMapper;
use LeadersLinked\Model\VideoConvert;
use Laminas\Mvc\I18n\Translator;
use LeadersLinked\Cache\CacheInterface;
use LeadersLinked\Library\Storage;

class ProcessQueueVideoConvertCommand extends Command
{
    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;
    
    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;
    
    
    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;
    
    /**
     *
     * @var array
     */
    private $config;
    
    
    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;
    
    
    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param \Laminas\Log\LoggerInterface
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        $this->translator   = $translator;
        
        parent::__construct();
    }

    
    protected function execute(InputInterface $input, OutputInterface $output) : int
    {

        
        $checkExists = true;
        $videos_procesados = 0;
        $videos_ignorados = 0;
        
        
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
        $videos = $videoConvertMapper->fetchBatch();
        
        $storage = Storage::getInstance($this->config);
        
        foreach($videos as $video)
        {
            
            if($video->type == VideoConvert::TYPE_FEED) {
                $targe_path = $storage->getPathFeed();
            } 
            else if($video->type == VideoConvert::TYPE_MICRO_LEARNING_SLIDES) {
                $targe_path = $storage->getPathMicrolearningSlide();
            }
            else if($video->type == VideoConvert::TYPE_MEDIA) {
                $targe_path = $storage->getPathMedia();
            } else {
                $targe_path = '';
            }
            
            if(empty($targe_path)) {
                $videos_ignorados++;
                $videoConvertMapper->delete($video);
                continue;
            }

           
           
            $url = $storage->getGenericFile($targe_path, $video->uuid, $video->filename, $checkExists);
            if(empty($url)) {
                $videos_ignorados++;
                $videoConvertMapper->delete($video);
                continue;
            }
            
            $s = explode('.', $video->filename);
            
            $tmpname  =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $s[0] . '-tmp.' . $s[1];
            $filename =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $video->filename;
            
            $content = file_get_contents($url);
            if(empty($content)) {
                $videos_ignorados++;
                $videoConvertMapper->delete($video);
                continue;
            }
            file_put_contents($tmpname, $content);
            
            if(file_exists($tmpname)) {
                
                $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $full_filename";
                $response   = trim(shell_exec($cmd));
                
                $source_duration = 0;

                
                $lines = explode("\n", $response);
                foreach($lines as $line)
                {
                    $line = trim(strtolower($line));
                     if(strpos($line, 'duration') !== false) {
                         $values = explode('=', $line);
                         $source_duration = intval($values[1], 10);
                     }
                }

                
                if($source_duration == 0) {
                    $second_extract = '00:00:02';
                } else {
                    if($source_duration > 10) {
                        $second_extract = '00:00:10';
                    } else {
                        $second_extract = '00:00:02';
                    }
                    
                }
                
                $poster = substr($video->filename, 0, strrpos($video->filename, '.')).  '.jpg';
                $poster =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $poster;
                
                $cmd = "/usr/bin/ffmpeg -y -i $tmpname  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y $poster";
                $output->writeln("command  : $cmd" );
                exec($cmd);
                
                $storage->putFile($targe_path, $video->uuid, $poster); 
                                     
                $cmd = "/usr/bin/ffmpeg -y -i $tmpname  -preset medium -crf 24 -codec:v libx264 $filename";  
                $output->writeln("command  : $cmd" );
                exec($cmd);

                $storage->putFile($targe_path, $video->uuid, $filename);            
                
                $videos_procesados++;
                $videoConvertMapper->delete($video);
            
                
            } else {
                $videoConvertMapper->delete($video);
                $output->writeln("El video no existe  : " . $video->filename);
                $videos_ignorados++;
                
            }
        }
       
        
        $output->writeln('Videos procesados:'  . $videos_procesados);
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
        
        $output->writeln('Fin del proceso de la cola de  Videos');
        
        return 0;
    }
    
    
    
}