| 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'];
|
|
|
91 |
|
|
|
92 |
break;
|
|
|
93 |
|
|
|
94 |
case VideoConvert::TYPE_MICRO_LEARNING :
|
|
|
95 |
$size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
|
|
|
96 |
break;
|
|
|
97 |
|
|
|
98 |
default :
|
| 244 |
efrain |
99 |
$size = '';
|
| 1 |
www |
100 |
|
|
|
101 |
}
|
|
|
102 |
|
| 244 |
efrain |
103 |
|
|
|
104 |
|
| 1 |
www |
105 |
if($size) {
|
|
|
106 |
|
| 244 |
efrain |
107 |
$target_width = 0 ;
|
|
|
108 |
$target_height = 0;
|
| 1 |
www |
109 |
|
| 244 |
efrain |
110 |
$candidate_sizes = [];
|
|
|
111 |
$arrSizes = explode(',', $size);
|
|
|
112 |
foreach($arrSizes as $elementSize)
|
|
|
113 |
{
|
|
|
114 |
$sizes = explode('x', $elementSize);
|
|
|
115 |
array_push($candidate_sizes, [
|
|
|
116 |
'width' => $sizes[0],
|
|
|
117 |
'height' => $sizes[1]
|
|
|
118 |
]);
|
|
|
119 |
}
|
| 1 |
www |
120 |
|
| 244 |
efrain |
121 |
|
| 245 |
efrain |
122 |
//$cmd = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width $full_filename";
|
| 1 |
www |
123 |
$cmd = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width $full_filename";
|
| 245 |
efrain |
124 |
|
|
|
125 |
echo $cmd . PHP_EOL. PHP_EOL. PHP_EOL;
|
|
|
126 |
|
| 1 |
www |
127 |
$response = trim(shell_exec($cmd));
|
| 245 |
efrain |
128 |
echo $response . PHP_EOL. PHP_EOL. PHP_EOL;
|
| 1 |
www |
129 |
|
|
|
130 |
|
| 244 |
efrain |
131 |
|
| 1 |
www |
132 |
$lines = explode("\n", $response);
|
|
|
133 |
foreach($lines as $line)
|
|
|
134 |
{
|
|
|
135 |
$line = trim(strtolower($line));
|
|
|
136 |
if(strpos($line, 'width') !== false) {
|
|
|
137 |
$values = explode('=', $line);
|
|
|
138 |
$source_width = $values[1];
|
|
|
139 |
}
|
|
|
140 |
if(strpos($line, 'height') !== false) {
|
|
|
141 |
$values = explode('=', $line);
|
|
|
142 |
$source_height = $values[1];
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
| 245 |
efrain |
146 |
|
|
|
147 |
|
| 1 |
www |
148 |
|
|
|
149 |
|
| 245 |
efrain |
150 |
|
| 244 |
efrain |
151 |
if($source_width && $source_height) {
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
$orientation = $source_width > $source_height ? 'L' : 'P';
|
|
|
155 |
|
|
|
156 |
foreach($candidate_sizes as $size)
|
|
|
157 |
{
|
|
|
158 |
|
|
|
159 |
if($orientation == 'P' && $target_height < $size['height']) {
|
|
|
160 |
$target_width = $size['width'];
|
|
|
161 |
$target_height = $size['height'];
|
|
|
162 |
}
|
|
|
163 |
|
| 245 |
efrain |
164 |
if($orientation == 'L' && $target_width < $size['width']) {
|
| 244 |
efrain |
165 |
$target_width = $size['width'];
|
|
|
166 |
$target_height = $size['height'];
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
} else {
|
|
|
175 |
$target_width = $candidate_sizes[0]['width'];
|
|
|
176 |
$target_height = $candidate_sizes[0]['height'];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
|
| 245 |
efrain |
180 |
echo 'source_width = ' . $source_width . PHP_EOL;
|
|
|
181 |
echo 'source_height = ' . $source_height . PHP_EOL . PHP_EOL;
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
echo 'target_width = ' . $target_width . PHP_EOL;
|
|
|
185 |
echo 'target_height = ' . $target_height . PHP_EOL . PHP_EOL;
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
| 244 |
efrain |
189 |
if($source_width && $source_height) {
|
| 1 |
www |
190 |
$width_ratio = $target_width / $source_width;
|
|
|
191 |
$height_ratio = $target_height / $source_height;
|
|
|
192 |
$radio = $width_ratio > $height_ratio ? $height_ratio : $width_ratio;
|
|
|
193 |
|
|
|
194 |
$resized_width = round($source_width * $radio);
|
|
|
195 |
$resized_height = round($source_height * $radio);
|
|
|
196 |
} else {
|
|
|
197 |
$resized_width = $target_width;
|
|
|
198 |
$resized_height = $target_height;
|
|
|
199 |
}
|
|
|
200 |
|
| 245 |
efrain |
201 |
|
|
|
202 |
echo 'resized_width = ' . $resized_width . PHP_EOL;
|
|
|
203 |
echo 'resized_height = ' . $resized_height . PHP_EOL . PHP_EOL;
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
| 1 |
www |
209 |
$values = explode('.', $full_filename);
|
|
|
210 |
$full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
|
|
|
211 |
|
|
|
212 |
rename($full_filename, $full_tempname);
|
|
|
213 |
|
|
|
214 |
$sizeVideo = $resized_width . ':' . $resized_height;
|
| 244 |
efrain |
215 |
|
| 248 |
efrain |
216 |
$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo -c:a copy -c:s copy n-c:v libx265 -preset slower -crf 18 $full_filename";
|
| 246 |
efrain |
217 |
$output = null;
|
|
|
218 |
$retval = null;
|
|
|
219 |
exec($cmd, $output, $retval);
|
| 247 |
efrain |
220 |
$output->writeln("Retorno el estado : $retval y la salida :");
|
|
|
221 |
$output->writeln($output);
|
| 245 |
efrain |
222 |
|
|
|
223 |
$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -frames:v 1 -vf scale=$sizeVideo $full_filename $full_filename_poster";
|
| 246 |
efrain |
224 |
$output = null;
|
|
|
225 |
$retval = null;
|
|
|
226 |
exec($cmd, $output, $retval);
|
| 247 |
efrain |
227 |
$output->writeln("Retorno el estado : $retval y la salida :");
|
|
|
228 |
$output->writeln($output);
|
| 242 |
efrain |
229 |
|
| 1 |
www |
230 |
@unlink($full_tempname);
|
|
|
231 |
|
| 245 |
efrain |
232 |
|
|
|
233 |
|
| 1 |
www |
234 |
$videos_procesados++;
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
}
|
|
|
238 |
} else {
|
|
|
239 |
$videos_ignorados++;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
$output->writeln('Videos procesados:' . $videos_procesados);
|
|
|
248 |
$output->writeln('Videos ignorados:' . $videos_ignorados);
|
|
|
249 |
|
|
|
250 |
$output->writeln('Fin del proceso de la cola de Videos');
|
|
|
251 |
|
|
|
252 |
return 0;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
}
|