Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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