Rev 242 | 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);
if(file_exists($video->filename)) {
switch($video->type)
{
case VideoConvert::TYPE_FEED :
$size = $this->config['leaderslinked.image_sizes.feed_video_size'];
break;
case VideoConvert::TYPE_MICRO_LEARNING :
$size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
break;
default :
$size = [];
}
if($size) {
$sizes = explode('x', $size);
$target_width = $sizes[0];
$target_height = $sizes[0];
$source_width = 0;
$source_height = 0;
$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_width) {
$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 -i $full_tempname -vf scale=$sizeVideo $full_filename";
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;
}
}