Proyectos de Subversion LeadersLinked - Services

Rev

Rev 159 | Rev 333 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 159 Rev 302
Línea 12... Línea 12...
12
use Laminas\Log\LoggerInterface;
12
use Laminas\Log\LoggerInterface;
13
use LeadersLinked\Mapper\VideoConvertMapper;
13
use LeadersLinked\Mapper\VideoConvertMapper;
14
use LeadersLinked\Model\VideoConvert;
14
use LeadersLinked\Model\VideoConvert;
15
use Laminas\Mvc\I18n\Translator;
15
use Laminas\Mvc\I18n\Translator;
16
use LeadersLinked\Cache\CacheInterface;
16
use LeadersLinked\Cache\CacheInterface;
-
 
17
use LeadersLinked\Library\Storage;
Línea 17... Línea 18...
17
 
18
 
18
class ProcessQueueVideoConvertCommand extends Command
19
class ProcessQueueVideoConvertCommand extends Command
19
{
20
{
20
    /**
21
    /**
Línea 72... Línea 73...
72
    
73
    
73
    protected function execute(InputInterface $input, OutputInterface $output) : int
74
    protected function execute(InputInterface $input, OutputInterface $output) : int
Línea 74... Línea -...
74
    {
-
 
-
 
75
    {
75
 
76
 
76
        
77
        
Línea 77... Línea 78...
77
        
78
        $checkExists = true;
78
        $videos_procesados = 0;
79
        $videos_procesados = 0;
Línea -... Línea 80...
-
 
80
        $videos_ignorados = 0;
Línea 79... Línea 81...
79
        $videos_ignorados = 0;
81
        
80
        
82
        
-
 
83
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
-
 
84
        $videos = $videoConvertMapper->fetchBatch();
-
 
85
        
-
 
86
        $storage = Storage::getInstance($this->config);
-
 
87
        
-
 
88
        foreach($videos as $video)
-
 
89
        {
-
 
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 = '';
Línea -... Línea 101...
-
 
101
            }
-
 
102
            
-
 
103
            if(empty($targe_path)) {
-
 
104
                $videos_ignorados++;
-
 
105
                $videoConvertMapper->delete($video);
-
 
106
                continue;
-
 
107
            }
-
 
108
 
Línea 81... Línea 109...
81
        
109
           
82
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
-
 
Línea -... Línea 110...
-
 
110
           
83
        $videos = $videoConvertMapper->fetchBatch();
111
            $url = $storage->getGenericFile($targe_path, $video->uuid, $video->filename, $checkExists);
Línea -... Línea 112...
-
 
112
            if(empty($url)) {
-
 
113
                $videos_ignorados++;
-
 
114
                $videoConvertMapper->delete($video);
-
 
115
                continue;
-
 
116
            }
-
 
117
            
-
 
118
            $s = explode('.', $video->filename);
Línea 84... Línea 119...
84
        
119
            
-
 
120
            $tmpname  =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $s[0] . '-tmp.' . $s[1];
-
 
121
            $filename =  sys_get_temp_dir()  . DIRECTORY_SEPARATOR . $video->filename;
-
 
122
            
-
 
123
            $content = file_get_contents($url);
-
 
124
            if(empty($content)) {
-
 
125
                $videos_ignorados++;
Línea -... Línea 126...
-
 
126
                $videoConvertMapper->delete($video);
85
        
127
                continue;
86
        foreach($videos as $video)
128
            }
87
        {
129
            file_put_contents($tmpname, $content);
88
 
130
            
89
            
-
 
90
            $full_filename = $video->filename;
-
 
91
            $full_filename = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $full_filename);
-
 
92
            
131
            if(file_exists($tmpname)) {
93
            $full_filename_poster = substr($full_filename, 0, strlen($full_filename) - 3) . 'jpg';
-
 
94
            
132
                
95
            
-
 
96
            if(file_exists($full_filename)) {
-
 
97
                
-
 
98
                switch($video->type) 
-
 
99
                {
-
 
100
                    case VideoConvert::TYPE_FEED : 
133
                $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $full_filename";
101
                        $size = $this->config['leaderslinked.image_sizes.feed_video_size'];
-
 
102
                        $imageSize = $this->config['leaderslinked.image_sizes.feed_image_size'];
-
 
103
                        
-
 
104
                        
134
                $response   = trim(shell_exec($cmd));
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'];
-
 
Línea 180... Línea 135...
180
                    }
135
                
181
                    
136
                $source_duration = 0;
182
                    if($source_width && $source_height) {
-
 
183
                        $width_ratio    = $target_width / $source_width;
-
 
184
                        $height_ratio   = $target_height / $source_height;
137
 
185
                        $radio = $width_ratio > $height_ratio ?  $height_ratio : $width_ratio;
138
                
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
                    }
139
                $lines = explode("\n", $response);
193
 
140
                foreach($lines as $line)
194
                    
141
                {
195
                    $values = explode('.', $full_filename);
-
 
196
                    $full_tempname = $values[0] .'-' . uniqid() . '.' . $values[1];
-
 
197
                    
-
 
198
                    rename($full_filename, $full_tempname);
142
                    $line = trim(strtolower($line));
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
                    }
143
                     if(strpos($line, 'duration') !== false) {
Línea 216... Línea -...
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" );
144
                         $values = explode('=', $line);
-
 
145
                         $source_duration = intval($values[1], 10);
-
 
146
                     }
-
 
147
                }
-
 
148
 
-
 
149
                
-
 
150
                if($source_duration == 0) {
-
 
151
                    $second_extract = '00:00:02';
-
 
152
                } else {
-
 
153
                    if($source_duration > 10) {
-
 
154
                        $second_extract = '00:00:10';
-
 
155
                    } else {
-
 
156
                        $second_extract = '00:00:02';
-
 
157
                    }
-
 
158
                    
-
 
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";
220
                    exec($cmd);
165
                $output->writeln("command  : $cmd" );
221
 
166
                exec($cmd);
222
                    if($imageSize) {
167
                
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";
168
                $storage->putFile($targe_path, $video->uuid, $poster); 
-
 
169
                                     
224
                        
170
                $cmd = "/usr/bin/ffmpeg -y -i $tmpname  -preset medium -crf 24 -codec:v libx264 $filename";  
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" );
171
                $output->writeln("command  : $cmd" );
228
                        exec($cmd);
-
 
Línea 229... Línea 172...
229
                    }
172
                exec($cmd);
230
                    
173