Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 250 | Rev 252 | 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;
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
 
121
                    $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width  $full_filename";
122
                    $response   = trim(shell_exec($cmd));
123
 
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);
130
                            $source_width = $values[1];
131
                        }
132
                        if(strpos($line, 'height') !== false) {
133
                            $values = explode('=', $line);
134
                            $source_height = $values[1];
135
                        }
136
                    }
137
 
138
 
244 efrain 139
                    if($source_width && $source_height) {
140
                        $orientation = $source_width > $source_height ? 'L' : 'P';
141
 
142
                        foreach($candidate_sizes as $size)
143
                        {
144
 
145
                            if($orientation == 'P' && $target_height < $size['height']) {
146
                                $target_width   = $size['width'];
147
                                $target_height  = $size['height'];
148
                            }
149
 
245 efrain 150
                            if($orientation == 'L' && $target_width <  $size['width']) {
244 efrain 151
                                $target_width   = $size['width'];
152
                                $target_height  = $size['height'];
251 efrain 153
                            }
244 efrain 154
                        }
155
                    } else {
156
                        $target_width = $candidate_sizes[0]['width'];
157
                        $target_height = $candidate_sizes[0]['height'];
158
                    }
159
 
160
                    if($source_width && $source_height) {
1 www 161
                        $width_ratio    = $target_width / $source_width;
162
                        $height_ratio   = $target_height / $source_height;
163
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
164
 
165
                        $resized_width = round($source_width * $radio);
166
                        $resized_height = round($source_height * $radio);
167
                    } else {
168
                        $resized_width = $target_width;
169
                        $resized_height = $target_height;
170
                    }
251 efrain 171
 
1 www 172
 
173
                    $values = explode('.', $full_filename);
174
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
175
 
176
                    rename($full_filename, $full_tempname);
177
                    $sizeVideo = $resized_width . ':' . $resized_height;
244 efrain 178
 
251 efrain 179
                    $cmd = "/usr/bin/ffmpeg -y -i $full_tempname -vf scale=$sizeVideo -c:a copy -c:s copy -c:v libx265 -preset slower -crf 18  $full_filename";
180
                    $output->writeln("command  : $cmd" );
181
                    exec($cmd);
182
 
183
                    $cmd            = "/usr/bin/ffmpeg -y -i $full_tempname  -frames:v 1 -vf scale=$sizeVideo  $full_filename_poster";
184
                    $output->writeln("command  : $cmd" );
185
                    exec($cmd);
242 efrain 186
 
1 www 187
                    @unlink($full_tempname);
188
                    $videos_procesados++;
189
                }
190
            } else {
191
                $videos_ignorados++;
192
            }
193
 
194
 
195
        }
196
 
197
 
198
 
199
        $output->writeln('Videos procesados:'  . $videos_procesados);
200
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
201
 
202
        $output->writeln('Fin del proceso de la cola de  Videos');
203
 
204
        return 0;
205
    }
206
 
207
 
208
 
209
}