Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Rev 302 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
12
use Laminas\Log\LoggerInterface;
13
use LeadersLinked\Mapper\VideoConvertMapper;
14
use LeadersLinked\Model\VideoConvert;
15
use Laminas\Mvc\I18n\Translator;
16
use LeadersLinked\Cache\CacheInterface;
17
 
18
class ProcessQueueVideoConvertCommand extends Command
19
{
20
    /**
21
     *
22
     * @var \Laminas\Db\Adapter\AdapterInterface
23
     */
24
    private $adapter;
25
 
26
    /**
27
     *
28
     * @var \LeadersLinked\Cache\CacheInterface
29
     */
30
    private $cache;
31
 
32
 
33
    /**
34
     *
35
     * @var \Laminas\Log\LoggerInterface
36
     */
37
    private $logger;
38
 
39
    /**
40
     *
41
     * @var array
42
     */
43
    private $config;
44
 
45
 
46
    /**
47
     *
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
58
     * @param array $config
59
     * @param \Laminas\Mvc\I18n\Translator $translator
60
     */
61
    public function __construct($adapter, $cache, $logger, $config, $translator)
62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
        $this->translator   = $translator;
68
 
69
        parent::__construct();
70
    }
71
 
72
 
73
    protected function execute(InputInterface $input, OutputInterface $output) : int
74
    {
75
 
76
 
77
 
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
        {
88
 
89
 
90
            $full_filename = $video->filename;
91
            $full_filename = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $full_filename);
92
 
93
            $full_filename_poster = substr($full_filename, 0, strlen($full_filename) - 3) . 'jpg';
94
 
95
 
96
            if(file_exists($full_filename)) {
97
 
98
                switch($video->type)
99
                {
100
                    case VideoConvert::TYPE_FEED :
101
                        $size = $this->config['leaderslinked.image_sizes.feed_video_size'];
102
                        $imageSize = $this->config['leaderslinked.image_sizes.feed_image_size'];
103
 
104
 
105
                        break;
106
 
107
                    case VideoConvert::TYPE_MICRO_LEARNING :
108
                        $size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
109
                        $imageSize = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
110
 
111
                        break;
112
 
113
                    default :
114
                        $size = '';
115
                        $imageSize = '';
116
 
117
                }
118
 
119
                if($size) {
120
                    $target_width = 0 ;
121
                    $target_height = 0;
122
 
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
                    }
133
 
134
                    $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $full_filename";
135
                    $response   = trim(shell_exec($cmd));
136
 
137
                    $source_duration = 0;
138
                    $source_width = 0;
139
                    $source_height = 0;
140
 
141
 
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);
148
                            $source_width = intval($values[1], 10);
149
                        }
150
                        if(strpos($line, 'height') !== false) {
151
                            $values = explode('=', $line);
152
                            $source_height = intval($values[1], 10);
153
                        }
154
                        if(strpos($line, 'duration') !== false) {
155
                            $values = explode('=', $line);
156
                            $source_duration = intval($values[1], 10);
157
                        }
158
                    }
159
 
160
 
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
 
172
                            if($orientation == 'L' && $target_width <  $size['width']) {
173
                                $target_width   = $size['width'];
174
                                $target_height  = $size['height'];
175
                            }
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) {
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
                    }
193
 
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;
200
 
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";
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";
204
 
205
 
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
 
217
                    $cmd = "/usr/bin/ffmpeg -y -i $full_tempname  -preset medium -crf 24 -codec:v libx264 $full_filename";
218
 
219
                    $output->writeln("command  : $cmd" );
220
                    exec($cmd);
221
 
222
                    if($imageSize) {
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
 
159 efrain 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";
226
                       $cmd = "/usr/bin/ffmpeg -y -i $full_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y $full_filename_poster";
227
                       $output->writeln("command  : $cmd" );
1 efrain 228
                        exec($cmd);
229
                    }
230
 
231
                    @unlink($full_tempname);
232
                    $videos_procesados++;
233
                    $videoConvertMapper->delete($video);
234
                }
235
            } else {
236
                $videoConvertMapper->delete($video);
237
                $output->writeln("El video no existe  : " . $full_filename );
238
                $videos_ignorados++;
239
            }
240
 
241
 
242
        }
243
 
244
 
245
 
246
        $output->writeln('Videos procesados:'  . $videos_procesados);
247
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
248
 
249
        $output->writeln('Fin del proceso de la cola de  Videos');
250
 
251
        return 0;
252
    }
253
 
254
 
255
 
256
}