Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3477 | Rev 6849 | Ir a la última revisión | | 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;
6749 efrain 11
use LeadersLinked\Cache\CacheInterface;
1 www 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
     *
6749 efrain 27
     * @var CacheInterface
1 www 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
6749 efrain 47
     * @param CacheInterface $cache
1 www 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
        {
6749 efrain 77
 
1 www 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
 
6749 efrain 85
            if(file_exists($full_filename)) {
1 www 86
 
87
                switch($video->type)
88
                {
89
                    case VideoConvert::TYPE_FEED :
90
                        $size = $this->config['leaderslinked.image_sizes.feed_video_size'];
3477 efrain 91
                        $imageSize = $this->config['leaderslinked.image_sizes.feed_image_size'];
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
 
3473 efrain 126
                    $source_duration = 0;
127
                    $source_width = 0;
128
                    $source_height = 0;
129
 
130
 
1 www 131
                    $lines = explode("\n", $response);
132
                    foreach($lines as $line)
133
                    {
134
                        $line = trim(strtolower($line));
135
                        if(strpos($line, 'width') !== false) {
136
                            $values = explode('=', $line);
3471 efrain 137
                            $source_width = intval($values[1], 10);
1 www 138
                        }
139
                        if(strpos($line, 'height') !== false) {
140
                            $values = explode('=', $line);
3471 efrain 141
                            $source_height = intval($values[1], 10);
1 www 142
                        }
3471 efrain 143
                        if(strpos($line, 'duration') !== false) {
144
                            $values = explode('=', $line);
145
                            $source_duration = intval($values[1], 10);
146
                        }
1 www 147
                    }
148
 
149
 
244 efrain 150
                    if($source_width && $source_height) {
151
                        $orientation = $source_width > $source_height ? 'L' : 'P';
152
 
153
                        foreach($candidate_sizes as $size)
154
                        {
155
 
156
                            if($orientation == 'P' && $target_height < $size['height']) {
157
                                $target_width   = $size['width'];
158
                                $target_height  = $size['height'];
159
                            }
160
 
245 efrain 161
                            if($orientation == 'L' && $target_width <  $size['width']) {
244 efrain 162
                                $target_width   = $size['width'];
163
                                $target_height  = $size['height'];
251 efrain 164
                            }
244 efrain 165
                        }
166
                    } else {
167
                        $target_width = $candidate_sizes[0]['width'];
168
                        $target_height = $candidate_sizes[0]['height'];
169
                    }
170
 
171
                    if($source_width && $source_height) {
1 www 172
                        $width_ratio    = $target_width / $source_width;
173
                        $height_ratio   = $target_height / $source_height;
174
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
175
 
176
                        $resized_width = round($source_width * $radio);
177
                        $resized_height = round($source_height * $radio);
178
                    } else {
179
                        $resized_width = $target_width;
180
                        $resized_height = $target_height;
181
                    }
251 efrain 182
 
1 www 183
 
184
                    $values = explode('.', $full_filename);
185
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
186
 
187
                    rename($full_filename, $full_tempname);
188
                    $sizeVideo = $resized_width . ':' . $resized_height;
244 efrain 189
 
258 efrain 190
                    //$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 191
                    //$cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo  -preset veryslow -crf 10 -c:v libx264  $full_filename";
192
                    //$cmd = "/usr/local/Cellar/ffmpeg/5.0.1/bin/ffmpeg -y -i $source  -preset medium -crf 24 -codec:v libx264 $target";
258 efrain 193
 
2194 efrain 194
 
3473 efrain 195
                    if($source_duration == 0) {
196
                       $second_extract = '00:00:02';
197
                    } else {
198
                        if($source_duration > 10) {
199
                            $second_extract = '00:00:10';
200
                        } else {
201
                            $second_extract = '00:00:02';
202
                        }
203
 
204
                    }
205
 
2194 efrain 206
                    $cmd = "/usr/bin/ffmpeg -y -i $full_tempname  -preset medium -crf 24 -codec:v libx264 $full_filename";
207
 
251 efrain 208
                    $output->writeln("command  : $cmd" );
209
                    exec($cmd);
210
 
253 efrain 211
                    if($imageSize) {
3477 efrain 212
                        //$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";
213
 
214
                        $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 215
                        $output->writeln("command  : $cmd" );
216
                        exec($cmd);
217
                    }
218
 
257 efrain 219
                    @unlink($full_tempname);
1 www 220
                    $videos_procesados++;
6749 efrain 221
                    $videoConvertMapper->delete($video);
1 www 222
                }
223
            } else {
6749 efrain 224
                $videoConvertMapper->delete($video);
225
                $output->writeln("El video no existe  : " . $full_filename );
1 www 226
                $videos_ignorados++;
227
            }
228
 
229
 
230
        }
231
 
232
 
233
 
234
        $output->writeln('Videos procesados:'  . $videos_procesados);
235
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
236
 
237
        $output->writeln('Fin del proceso de la cola de  Videos');
238
 
239
        return 0;
240
    }
241
 
242
 
243
 
244
}