Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 244 | 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
 
242 efrain 82
            $full_filename_poster = substr($full_filename, 0, strlen($full_filename) - 3) . '.jpg';
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 :
99
                        $size = [];
100
 
101
                }
102
 
103
                if($size) {
104
                    $sizes = explode('x', $size);
105
                    $target_width = $sizes[0];
106
                    $target_height = $sizes[0];
107
                    $source_width =  0;
108
                    $source_height = 0;
109
 
110
 
111
 
112
                    $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width  $full_filename";
113
                    $response   = trim(shell_exec($cmd));
114
 
115
 
116
 
117
                    $lines = explode("\n", $response);
118
                    foreach($lines as $line)
119
                    {
120
                        $line = trim(strtolower($line));
121
                        if(strpos($line, 'width') !== false) {
122
                            $values = explode('=', $line);
123
                            $source_width = $values[1];
124
                        }
125
                        if(strpos($line, 'height') !== false) {
126
                            $values = explode('=', $line);
127
                            $source_height = $values[1];
128
                        }
129
                    }
130
 
131
 
132
 
133
                    if($source_width && $source_width) {
134
                        $width_ratio    = $target_width / $source_width;
135
                        $height_ratio   = $target_height / $source_height;
136
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
137
 
138
                        $resized_width = round($source_width * $radio);
139
                        $resized_height = round($source_height * $radio);
140
                    } else {
141
                        $resized_width = $target_width;
142
                        $resized_height = $target_height;
143
                    }
144
 
145
                    $values = explode('.', $full_filename);
146
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
147
 
148
                    rename($full_filename, $full_tempname);
149
 
150
                    $sizeVideo = $resized_width . ':' . $resized_height;
151
                    $cmd            = "/usr/bin/ffmpeg -i $full_tempname -vf scale=$sizeVideo $full_filename";
152
                    exec($cmd);
153
 
242 efrain 154
 
155
 
156
 
157
                    $cmd            = "/usr/bin/ffmpeg -i $full_tempname  -vframes 1  -vf scale=$sizeVideo $full_filename $full_filename_poster";
158
                    exec($cmd);
159
 
1 www 160
                    @unlink($full_tempname);
161
 
162
                    $videos_procesados++;
163
 
164
 
165
                }
166
            } else {
167
                $videos_ignorados++;
168
            }
169
 
170
 
171
        }
172
 
173
 
174
 
175
        $output->writeln('Videos procesados:'  . $videos_procesados);
176
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
177
 
178
        $output->writeln('Fin del proceso de la cola de  Videos');
179
 
180
        return 0;
181
    }
182
 
183
 
184
 
185
}