Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 257 | Rev 3471 | 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\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\VideoConvertMapper;
use LeadersLinked\Model\VideoConvert;

class ProcessQueueVideoConvertCommand extends Command
{
    /**
     *
     * @var AdapterInterface
     */
    private $adapter;
    
    
    /**
     *
     * @var AbstractAdapter
     */
    private $cache;
    
    /**
     *
     * @var  LoggerInterface
     */
    private $logger;

    /**
     *
     * @var array
     */
    private $config;
    
    
    /**
     *
     * @param AdapterInterface $adapter
     * @param AbstractAdapter $cache
     * @param LoggerInterface $logger
     * @param array $config
     */
     public function __construct($adapter, $cache, $logger, $config)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        
        parent::__construct();
    }

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

        
        
        $videos_procesados = 0;
        $videos_ignorados = 0;
        
        
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
        $videos = $videoConvertMapper->fetchBatch();
        
        
        foreach($videos as $video)
        {
            $videoConvertMapper->delete($video);
            
            $full_filename = $video->filename;
            $full_filename = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $full_filename);
            
            $full_filename_poster = substr($full_filename, 0, strlen($full_filename) - 3) . 'jpg';
            
            
            if(file_exists($video->filename)) {
                
                switch($video->type) 
                {
                    case VideoConvert::TYPE_FEED : 
                        $size = $this->config['leaderslinked.image_sizes.feed_video_size'];
                        $imageSize = '';
                        
                        
                        break;
                        
                    case VideoConvert::TYPE_MICRO_LEARNING :
                        $size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
                        $imageSize = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
                        
                        break;
                        
                    default : 
                        $size = '';
                        $imageSize = '';
                        
                }
                
   
                
                if($size) {
                    
             
                    
                    
                    $target_width = 0 ; 
                    $target_height = 0;
                    
                    $candidate_sizes = [];
                    $arrSizes = explode(',', $size);
                    foreach($arrSizes as $elementSize)
                    {
                        $sizes = explode('x', $elementSize);
                        array_push($candidate_sizes, [
                            'width' =>  $sizes[0],
                            'height' => $sizes[1]
                        ]);    
                    }
                    
                    $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width  $full_filename";
                    $response   = trim(shell_exec($cmd));
                    
                    $lines = explode("\n", $response);
                    foreach($lines as $line)
                    {
                        $line = trim(strtolower($line));
                        if(strpos($line, 'width') !== false) {
                            $values = explode('=', $line);
                            $source_width = $values[1];
                        }
                        if(strpos($line, 'height') !== false) {
                            $values = explode('=', $line);
                            $source_height = $values[1];
                        }
                    }
                    
                    
                    if($source_width && $source_height) {
                        $orientation = $source_width > $source_height ? 'L' : 'P';
                        
                        foreach($candidate_sizes as $size) 
                        {

                            if($orientation == 'P' && $target_height < $size['height']) {
                                $target_width   = $size['width'];
                                $target_height  = $size['height'];
                            }

                            if($orientation == 'L' && $target_width <  $size['width']) {
                                $target_width   = $size['width'];
                                $target_height  = $size['height'];
                            }  
                        }
                    } else {
                        $target_width = $candidate_sizes[0]['width'];
                        $target_height = $candidate_sizes[0]['height'];
                    }
                    
                    if($source_width && $source_height) {
                        $width_ratio    = $target_width / $source_width;
                        $height_ratio   = $target_height / $source_height;
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
    
                        $resized_width = round($source_width * $radio);
                        $resized_height = round($source_height * $radio);
                    } else {
                        $resized_width = $target_width;
                        $resized_height = $target_height;
                    }

                    
                    $values = explode('.', $full_filename);
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
                    
                    rename($full_filename, $full_tempname);
                    $sizeVideo = $resized_width . ':' . $resized_height;
                    
                    //$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo -c:a copy -c:s copy -c:v libx264 -preset slower -crf 18  $full_filename";
                    $cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo  -preset veryslow -crf 10 -c:v libx264  $full_filename";
                    
                    $output->writeln("command  : $cmd" );
                    exec($cmd);

                    if($imageSize) {
                    
                        $cmd = "/usr/bin/ffmpeg -y -i $full_tempname  -pix_fmt yuvj422p -deinterlace -an -ss 00:00:02 -f mjpeg -t 1 -r 1 -y -s $imageSize $full_filename_poster";
                        $output->writeln("command  : $cmd" );
                        exec($cmd);
                    }
                    
                    @unlink($full_tempname);
                    $videos_procesados++;
                }
            } else {
                $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;
    }
    
    
    
}