Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 242 | Ir a la última revisión | | 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
    {
64
        $videos_procesados = 0;
65
        $videos_ignorados = 0;
66
 
67
 
68
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
69
        $videos = $videoConvertMapper->fetchBatch();
70
 
71
 
72
        foreach($videos as $video)
73
        {
74
            $videoConvertMapper->delete($video);
75
 
76
            $full_filename = $video->filename;
77
            $full_filename = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $full_filename);
78
 
79
            if(file_exists($video->filename)) {
80
 
81
                switch($video->type)
82
                {
83
                    case VideoConvert::TYPE_FEED :
84
                        $size = $this->config['leaderslinked.image_sizes.feed_video_size'];
85
 
86
                        break;
87
 
88
                    case VideoConvert::TYPE_MICRO_LEARNING :
89
                        $size = $this->config['leaderslinked.image_sizes.microlearning_video_size'];
90
                        break;
91
 
92
                    default :
93
                        $size = [];
94
 
95
                }
96
 
97
                if($size) {
98
                    $sizes = explode('x', $size);
99
                    $target_width = $sizes[0];
100
                    $target_height = $sizes[0];
101
                    $source_width =  0;
102
                    $source_height = 0;
103
 
104
 
105
 
106
                    $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width  $full_filename";
107
                    $response   = trim(shell_exec($cmd));
108
 
109
 
110
 
111
                    $lines = explode("\n", $response);
112
                    foreach($lines as $line)
113
                    {
114
                        $line = trim(strtolower($line));
115
                        if(strpos($line, 'width') !== false) {
116
                            $values = explode('=', $line);
117
                            $source_width = $values[1];
118
                        }
119
                        if(strpos($line, 'height') !== false) {
120
                            $values = explode('=', $line);
121
                            $source_height = $values[1];
122
                        }
123
                    }
124
 
125
 
126
 
127
                    if($source_width && $source_width) {
128
                        $width_ratio    = $target_width / $source_width;
129
                        $height_ratio   = $target_height / $source_height;
130
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
131
 
132
                        $resized_width = round($source_width * $radio);
133
                        $resized_height = round($source_height * $radio);
134
                    } else {
135
                        $resized_width = $target_width;
136
                        $resized_height = $target_height;
137
                    }
138
 
139
                    $values = explode('.', $full_filename);
140
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
141
 
142
                    rename($full_filename, $full_tempname);
143
 
144
                    $sizeVideo = $resized_width . ':' . $resized_height;
145
                    $cmd            = "/usr/bin/ffmpeg -i $full_tempname -vf scale=$sizeVideo $full_filename";
146
                    exec($cmd);
147
 
148
                    @unlink($full_tempname);
149
 
150
                    $videos_procesados++;
151
 
152
 
153
                }
154
            } else {
155
                $videos_ignorados++;
156
            }
157
 
158
 
159
        }
160
 
161
 
162
 
163
        $output->writeln('Videos procesados:'  . $videos_procesados);
164
        $output->writeln('Videos ignorados:'  . $videos_ignorados);
165
 
166
        $output->writeln('Fin del proceso de la cola de  Videos');
167
 
168
        return 0;
169
    }
170
 
171
 
172
 
173
}