| 1 |
www |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Command;
|
|
|
6 |
|
|
|
7 |
use Symfony\Component\Console\Command\Command;
|
|
|
8 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
9 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
10 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
11 |
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
|
|
|
12 |
use Laminas\Log\LoggerInterface;
|
|
|
13 |
use LeadersLinked\Mapper\VideoConvertMapper;
|
|
|
14 |
use LeadersLinked\Model\VideoConvert;
|
|
|
15 |
|
|
|
16 |
class ProcessQueueVideoConvertCommand extends Command
|
|
|
17 |
{
|
|
|
18 |
/**
|
|
|
19 |
*
|
|
|
20 |
* @var AdapterInterface
|
|
|
21 |
*/
|
|
|
22 |
private $adapter;
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
*
|
|
|
27 |
* @var AbstractAdapter
|
|
|
28 |
*/
|
|
|
29 |
private $cache;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
*
|
|
|
33 |
* @var LoggerInterface
|
|
|
34 |
*/
|
|
|
35 |
private $logger;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
*
|
|
|
39 |
* @var array
|
|
|
40 |
*/
|
|
|
41 |
private $config;
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
* @param AdapterInterface $adapter
|
|
|
47 |
* @param AbstractAdapter $cache
|
|
|
48 |
* @param LoggerInterface $logger
|
|
|
49 |
* @param array $config
|
|
|
50 |
*/
|
|
|
51 |
public function __construct($adapter, $cache, $logger, $config)
|
|
|
52 |
{
|
|
|
53 |
$this->adapter = $adapter;
|
|
|
54 |
$this->cache = $cache;
|
|
|
55 |
$this->logger = $logger;
|
|
|
56 |
$this->config = $config;
|
|
|
57 |
|
|
|
58 |
parent::__construct();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
protected function execute(InputInterface $input, OutputInterface $output) : int
|
|
|
63 |
{
|
| 242 |
efrain |
64 |
|
|
|
65 |
|
|
|
66 |
|
| 1 |
www |
67 |
$videos_procesados = 0;
|
|
|
68 |
$videos_ignorados = 0;
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
$videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
|
|
|
72 |
$videos = $videoConvertMapper->fetchBatch();
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
foreach($videos as $video)
|
|
|
76 |
{
|
|
|
77 |
$videoConvertMapper->delete($video);
|
|
|
78 |
|
|
|
79 |
$full_filename = $video->filename;
|
|
|
80 |
$full_filename = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $full_filename);
|
|
|
81 |
|
| 244 |
efrain |
82 |
$full_filename_poster = substr($full_filename, 0, strlen($full_filename) - 3) . 'jpg';
|
| 242 |
efrain |
83 |
|
|
|
84 |
|
| 1 |
www |
85 |
if(file_exists($video->filename)) {
|
|
|
86 |
|
|
|
87 |
switch($video->type)
|
|
|
88 |
{
|
|
|
89 |
case VideoConvert::TYPE_FEED :
|
|
|
90 |
$size = $this->config['leaderslinked.image_sizes.feed_video_size'];
|
| 254 |
efrain |
91 |
$imageSize = '';
|
| 253 |
efrain |
92 |
|
|
|
93 |
|
| 1 |
www |
94 |
break;
|
|
|
95 |
|
|
|
96 |
case VideoConvert::TYPE_MICRO_LEARNING :
|
|
|
97 |
$size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
|
| 254 |
efrain |
98 |
$imageSize = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
|
|
|
99 |
|
| 1 |
www |
100 |
break;
|
|
|
101 |
|
|
|
102 |
default :
|
| 244 |
efrain |
103 |
$size = '';
|
| 253 |
efrain |
104 |
$imageSize = '';
|
| 1 |
www |
105 |
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if($size) {
|
| 244 |
efrain |
109 |
$target_width = 0 ;
|
|
|
110 |
$target_height = 0;
|
| 1 |
www |
111 |
|
| 244 |
efrain |
112 |
$candidate_sizes = [];
|
|
|
113 |
$arrSizes = explode(',', $size);
|
|
|
114 |
foreach($arrSizes as $elementSize)
|
|
|
115 |
{
|
|
|
116 |
$sizes = explode('x', $elementSize);
|
|
|
117 |
array_push($candidate_sizes, [
|
|
|
118 |
'width' => $sizes[0],
|
|
|
119 |
'height' => $sizes[1]
|
|
|
120 |
]);
|
|
|
121 |
}
|
| 1 |
www |
122 |
|
| 3471 |
efrain |
123 |
$cmd = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration $full_filename";
|
| 1 |
www |
124 |
$response = trim(shell_exec($cmd));
|
|
|
125 |
|
|
|
126 |
$lines = explode("\n", $response);
|
|
|
127 |
foreach($lines as $line)
|
|
|
128 |
{
|
|
|
129 |
$line = trim(strtolower($line));
|
|
|
130 |
if(strpos($line, 'width') !== false) {
|
|
|
131 |
$values = explode('=', $line);
|
| 3471 |
efrain |
132 |
$source_width = intval($values[1], 10);
|
| 1 |
www |
133 |
}
|
|
|
134 |
if(strpos($line, 'height') !== false) {
|
|
|
135 |
$values = explode('=', $line);
|
| 3471 |
efrain |
136 |
$source_height = intval($values[1], 10);
|
| 1 |
www |
137 |
}
|
| 3471 |
efrain |
138 |
if(strpos($line, 'duration') !== false) {
|
|
|
139 |
$values = explode('=', $line);
|
|
|
140 |
$source_duration = intval($values[1], 10);
|
|
|
141 |
}
|
| 1 |
www |
142 |
}
|
|
|
143 |
|
|
|
144 |
|
| 244 |
efrain |
145 |
if($source_width && $source_height) {
|
|
|
146 |
$orientation = $source_width > $source_height ? 'L' : 'P';
|
|
|
147 |
|
|
|
148 |
foreach($candidate_sizes as $size)
|
|
|
149 |
{
|
|
|
150 |
|
|
|
151 |
if($orientation == 'P' && $target_height < $size['height']) {
|
|
|
152 |
$target_width = $size['width'];
|
|
|
153 |
$target_height = $size['height'];
|
|
|
154 |
}
|
|
|
155 |
|
| 245 |
efrain |
156 |
if($orientation == 'L' && $target_width < $size['width']) {
|
| 244 |
efrain |
157 |
$target_width = $size['width'];
|
|
|
158 |
$target_height = $size['height'];
|
| 251 |
efrain |
159 |
}
|
| 244 |
efrain |
160 |
}
|
|
|
161 |
} else {
|
|
|
162 |
$target_width = $candidate_sizes[0]['width'];
|
|
|
163 |
$target_height = $candidate_sizes[0]['height'];
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
if($source_width && $source_height) {
|
| 1 |
www |
167 |
$width_ratio = $target_width / $source_width;
|
|
|
168 |
$height_ratio = $target_height / $source_height;
|
|
|
169 |
$radio = $width_ratio > $height_ratio ? $height_ratio : $width_ratio;
|
|
|
170 |
|
|
|
171 |
$resized_width = round($source_width * $radio);
|
|
|
172 |
$resized_height = round($source_height * $radio);
|
|
|
173 |
} else {
|
|
|
174 |
$resized_width = $target_width;
|
|
|
175 |
$resized_height = $target_height;
|
|
|
176 |
}
|
| 251 |
efrain |
177 |
|
| 1 |
www |
178 |
|
|
|
179 |
$values = explode('.', $full_filename);
|
|
|
180 |
$full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
|
|
|
181 |
|
|
|
182 |
rename($full_filename, $full_tempname);
|
|
|
183 |
$sizeVideo = $resized_width . ':' . $resized_height;
|
| 244 |
efrain |
184 |
|
| 258 |
efrain |
185 |
//$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";
|
| 2194 |
efrain |
186 |
//$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo -preset veryslow -crf 10 -c:v libx264 $full_filename";
|
|
|
187 |
//$cmd = "/usr/local/Cellar/ffmpeg/5.0.1/bin/ffmpeg -y -i $source -preset medium -crf 24 -codec:v libx264 $target";
|
| 258 |
efrain |
188 |
|
| 2194 |
efrain |
189 |
|
|
|
190 |
$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -preset medium -crf 24 -codec:v libx264 $full_filename";
|
|
|
191 |
|
| 251 |
efrain |
192 |
$output->writeln("command : $cmd" );
|
|
|
193 |
exec($cmd);
|
|
|
194 |
|
| 253 |
efrain |
195 |
if($imageSize) {
|
| 242 |
efrain |
196 |
|
| 256 |
efrain |
197 |
$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";
|
| 253 |
efrain |
198 |
$output->writeln("command : $cmd" );
|
|
|
199 |
exec($cmd);
|
|
|
200 |
}
|
|
|
201 |
|
| 257 |
efrain |
202 |
@unlink($full_tempname);
|
| 1 |
www |
203 |
$videos_procesados++;
|
|
|
204 |
}
|
|
|
205 |
} else {
|
|
|
206 |
$videos_ignorados++;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
$output->writeln('Videos procesados:' . $videos_procesados);
|
|
|
215 |
$output->writeln('Videos ignorados:' . $videos_ignorados);
|
|
|
216 |
|
|
|
217 |
$output->writeln('Fin del proceso de la cola de Videos');
|
|
|
218 |
|
|
|
219 |
return 0;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
}
|