Proyectos de Subversion LeadersLinked - Services

Rev

Rev 345 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
345 www 2
declare(strict_types = 1);
1 efrain 3
namespace LeadersLinked\Command;
4
 
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Log\LoggerInterface;
10
use LeadersLinked\Mapper\VideoConvertMapper;
11
use LeadersLinked\Model\VideoConvert;
12
use Laminas\Mvc\I18n\Translator;
13
use LeadersLinked\Cache\CacheInterface;
302 www 14
use LeadersLinked\Library\Storage;
1 efrain 15
 
16
class ProcessQueueVideoConvertCommand extends Command
17
{
345 www 18
 
1 efrain 19
    /**
20
     *
21
     * @var \Laminas\Db\Adapter\AdapterInterface
22
     */
23
    private $adapter;
345 www 24
 
1 efrain 25
    /**
26
     *
27
     * @var \LeadersLinked\Cache\CacheInterface
28
     */
29
    private $cache;
345 www 30
 
1 efrain 31
    /**
32
     *
33
     * @var \Laminas\Log\LoggerInterface
34
     */
35
    private $logger;
345 www 36
 
1 efrain 37
    /**
38
     *
39
     * @var array
40
     */
41
    private $config;
345 www 42
 
1 efrain 43
    /**
44
     *
45
     * @var \Laminas\Mvc\I18n\Translator
46
     */
47
    private $translator;
345 www 48
 
1 efrain 49
    /**
50
     *
51
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
52
     * @param \LeadersLinked\Cache\CacheInterface $cache
345 www 53
     * @param
54
     *            \Laminas\Log\LoggerInterface
1 efrain 55
     * @param array $config
56
     * @param \Laminas\Mvc\I18n\Translator $translator
57
     */
58
    public function __construct($adapter, $cache, $logger, $config, $translator)
59
    {
345 www 60
        $this->adapter = $adapter;
61
        $this->cache = $cache;
62
        $this->logger = $logger;
63
        $this->config = $config;
64
        $this->translator = $translator;
65
 
1 efrain 66
        parent::__construct();
67
    }
68
 
345 www 69
    protected function execute(InputInterface $input, OutputInterface $output): int
1 efrain 70
    {
302 www 71
        $checkExists = true;
1 efrain 72
        $videos_procesados = 0;
73
        $videos_ignorados = 0;
345 www 74
 
1 efrain 75
        $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
76
        $videos = $videoConvertMapper->fetchBatch();
345 www 77
 
333 www 78
        $storage = Storage::getInstance($this->config, $this->adapter);
345 www 79
 
80
        foreach ($videos as $video) {
81
 
82
            if ($video->type == VideoConvert::TYPE_FEED) {
302 www 83
                $targe_path = $storage->getPathFeed();
345 www 84
            } else if ($video->type == VideoConvert::TYPE_MICRO_LEARNING_SLIDES) {
302 www 85
                $targe_path = $storage->getPathMicrolearningSlide();
345 www 86
            } else if ($video->type == VideoConvert::TYPE_MEDIA) {
302 www 87
                $targe_path = $storage->getPathMedia();
345 www 88
            } else if ($video->type == VideoConvert::TYPE_HABIT_CONTENT) {
89
                $targe_path = $storage->getPathHabitContent();
302 www 90
            } else {
91
                $targe_path = '';
92
            }
345 www 93
 
94
            if (empty($targe_path)) {
95
                $videos_ignorados ++;
302 www 96
                $videoConvertMapper->delete($video);
97
                continue;
98
            }
1 efrain 99
 
334 www 100
            $directory = $storage->getDirectoryFromDisk($targe_path, $video->uuid);
345 www 101
 
102
            $currentfile = $directory . DIRECTORY_SEPARATOR . $video->filename;
103
            $posterfile = $directory . DIRECTORY_SEPARATOR . substr($video->filename, 0, strrpos($video->filename, '.')) . '.jpg';
104
 
105
            if (! file_exists($currentfile)) {
106
                $videos_ignorados ++;
302 www 107
                $videoConvertMapper->delete($video);
108
                continue;
109
            }
345 www 110
 
302 www 111
            $s = explode('.', $video->filename);
345 www 112
 
113
            $tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $s[0] . '-tmp.' . $s[1];
114
            $newfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $video->filename;
115
 
116
            if (! @copy($currentfile, $tmpfile)) {
117
                $videos_ignorados ++;
302 www 118
                $videoConvertMapper->delete($video);
119
                continue;
120
            }
345 www 121
 
122
            if (file_exists($tmpfile)) {
123
 
124
                if (! file_exists($posterfile)) {
383 www 125
                    \LeadersLinked\Library\Storage::extractPosterFromVideo($tmpfile, $posterfile);
1 efrain 126
                }
345 www 127
 
128
                $cmd = "/usr/bin/ffmpeg -y -i $tmpfile  -preset medium -crf 24 -codec:v libx264 $newfile";
129
                $output->writeln("command  : $cmd");
302 www 130
                exec($cmd);
1 efrain 131
 
334 www 132
                @copy($newfile, $currentfile);
345 www 133
 
334 www 134
                @unlink($tmpfile);
135
                @unlink($newfile);
345 www 136
 
137
                $videos_procesados ++;
302 www 138
                $videoConvertMapper->delete($video);
1 efrain 139
            } else {
140
                $videoConvertMapper->delete($video);
302 www 141
                $output->writeln("El video no existe  : " . $video->filename);
345 www 142
                $videos_ignorados ++;
1 efrain 143
            }
144
        }
334 www 145
 
345 www 146
        $output->writeln('Videos procesados:' . $videos_procesados);
147
        $output->writeln('Videos ignorados:' . $videos_ignorados);
148
 
1 efrain 149
        $output->writeln('Fin del proceso de la cola de  Videos');
345 www 150
 
1 efrain 151
        return 0;
152
    }
153
}