Rev 6849 | 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;
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
{
$videos_procesados = 0;
$videos_ignorados = 0;
$videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
$videos = $videoConvertMapper->fetchBatch();
foreach($videos as $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($full_filename)) {
switch($video->type)
{
case VideoConvert::TYPE_FEED :
$size = $this->config['leaderslinked.image_sizes.feed_video_size'];
$imageSize = $this->config['leaderslinked.image_sizes.feed_image_size'];
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,duration $full_filename";
$response = trim(shell_exec($cmd));
$source_duration = 0;
$source_width = 0;
$source_height = 0;
$lines = explode("\n", $response);
foreach($lines as $line)
{
$line = trim(strtolower($line));
if(strpos($line, 'width') !== false) {
$values = explode('=', $line);
$source_width = intval($values[1], 10);
}
if(strpos($line, 'height') !== false) {
$values = explode('=', $line);
$source_height = intval($values[1], 10);
}
if(strpos($line, 'duration') !== false) {
$values = explode('=', $line);
$source_duration = intval($values[1], 10);
}
}
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";
//$cmd = "/usr/local/Cellar/ffmpeg/5.0.1/bin/ffmpeg -y -i $source -preset medium -crf 24 -codec:v libx264 $target";
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';
}
}
$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -preset medium -crf 24 -codec: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 $second_extract -f mjpeg -t 1 -r 1 -y -s $imageSize $full_filename_poster";
$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y -s $imageSize $full_filename_poster";
$output->writeln("command : $cmd" );
exec($cmd);
}
@unlink($full_tempname);
$videos_procesados++;
$videoConvertMapper->delete($video);
}
} else {
$videoConvertMapper->delete($video);
$output->writeln("El video no existe : " . $full_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;
}
}