Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | | Comparar con el anterior | Ultima modificación | Ver Log |

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