Proyectos de Subversion LeadersLinked - Services

Rev

Rev 302 | Rev 334 | 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;
302 www 17
use LeadersLinked\Library\Storage;
1 efrain 18
 
19
class ProcessQueueVideoConvertCommand extends Command
20
{
21
    /**
22
     *
23
     * @var \Laminas\Db\Adapter\AdapterInterface
24
     */
25
    private $adapter;
26
 
27
    /**
28
     *
29
     * @var \LeadersLinked\Cache\CacheInterface
30
     */
31
    private $cache;
32
 
33
 
34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
    /**
48
     *
49
     * @var \Laminas\Mvc\I18n\Translator
50
     */
51
    private $translator;
52
 
53
 
54
    /**
55
     *
56
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
57
     * @param \LeadersLinked\Cache\CacheInterface $cache
58
     * @param \Laminas\Log\LoggerInterface
59
     * @param array $config
60
     * @param \Laminas\Mvc\I18n\Translator $translator
61
     */
62
    public function __construct($adapter, $cache, $logger, $config, $translator)
63
    {
64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
69
 
70
        parent::__construct();
71
    }
72
 
73
 
74
    protected function execute(InputInterface $input, OutputInterface $output) : int
75
    {
76
 
77
 
302 www 78
        $checkExists = true;
1 efrain 79
        $videos_procesados = 0;
80
        $videos_ignorados = 0;
81
 
82
 
83
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
84
        $videos = $videoConvertMapper->fetchBatch();
85
 
333 www 86
        $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 87
 
88
        foreach($videos as $video)
89
        {
302 www 90
 
91
            if($video->type == VideoConvert::TYPE_FEED) {
92
                $targe_path = $storage->getPathFeed();
93
            }
94
            else if($video->type == VideoConvert::TYPE_MICRO_LEARNING_SLIDES) {
95
                $targe_path = $storage->getPathMicrolearningSlide();
96
            }
97
            else if($video->type == VideoConvert::TYPE_MEDIA) {
98
                $targe_path = $storage->getPathMedia();
99
            } else {
100
                $targe_path = '';
101
            }
102
 
103
            if(empty($targe_path)) {
104
                $videos_ignorados++;
105
                $videoConvertMapper->delete($video);
106
                continue;
107
            }
1 efrain 108
 
302 www 109
 
110
 
111
            $url = $storage->getGenericFile($targe_path, $video->uuid, $video->filename, $checkExists);
112
            if(empty($url)) {
113
                $videos_ignorados++;
114
                $videoConvertMapper->delete($video);
115
                continue;
116
            }
1 efrain 117
 
302 www 118
            $s = explode('.', $video->filename);
1 efrain 119
 
302 www 120
            $tmpname  =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $s[0] . '-tmp.' . $s[1];
121
            $filename =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $video->filename;
1 efrain 122
 
302 www 123
            $content = file_get_contents($url);
124
            if(empty($content)) {
125
                $videos_ignorados++;
126
                $videoConvertMapper->delete($video);
127
                continue;
128
            }
129
            file_put_contents($tmpname, $content);
1 efrain 130
 
302 www 131
            if(file_exists($tmpname)) {
1 efrain 132
 
302 www 133
                $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $full_filename";
134
                $response   = trim(shell_exec($cmd));
135
 
136
                $source_duration = 0;
137
 
138
 
139
                $lines = explode("\n", $response);
140
                foreach($lines as $line)
1 efrain 141
                {
302 www 142
                    $line = trim(strtolower($line));
143
                     if(strpos($line, 'duration') !== false) {
144
                         $values = explode('=', $line);
145
                         $source_duration = intval($values[1], 10);
146
                     }
1 efrain 147
                }
302 www 148
 
1 efrain 149
 
302 www 150
                if($source_duration == 0) {
151
                    $second_extract = '00:00:02';
152
                } else {
153
                    if($source_duration > 10) {
154
                        $second_extract = '00:00:10';
1 efrain 155
                    } else {
302 www 156
                        $second_extract = '00:00:02';
1 efrain 157
                    }
158
 
302 www 159
                }
160
 
161
                $poster = substr($video->filename, 0, strrpos($video->filename, '.')).  '.jpg';
162
                $poster =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $poster;
163
 
164
                $cmd = "/usr/bin/ffmpeg -y -i $tmpname  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y $poster";
165
                $output->writeln("command  : $cmd" );
166
                exec($cmd);
167
 
168
                $storage->putFile($targe_path, $video->uuid, $poster);
169
 
170
                $cmd = "/usr/bin/ffmpeg -y -i $tmpname  -preset medium -crf 24 -codec:v libx264 $filename";
171
                $output->writeln("command  : $cmd" );
172
                exec($cmd);
1 efrain 173
 
302 www 174
                $storage->putFile($targe_path, $video->uuid, $filename);
175
 
176
                $videos_procesados++;
177
                $videoConvertMapper->delete($video);
178
 
179
 
1 efrain 180
            } else {
181
                $videoConvertMapper->delete($video);
302 www 182
                $output->writeln("El video no existe  : " . $video->filename);
1 efrain 183
                $videos_ignorados++;
302 www 184
 
1 efrain 185
            }
186
        }
187
 
188
 
189
        $output->writeln('Videos procesados:'  . $videos_procesados);
190
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
191
 
192
        $output->writeln('Fin del proceso de la cola de  Videos');
193
 
194
        return 0;
195
    }
196
 
197
 
198
 
199
}