-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessVideosCommand.php
108 lines (88 loc) · 3.65 KB
/
ProcessVideosCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Coshi\Bundle\TranscodeBundle\Command;
use AppBundle\Entity\Book;
use Coshi\Bundle\TranscodeBundle\Transcoder\Transcoder;
use Coshi\Bundle\TranscodeBundle\Transcoder\TranscodeableProviderInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
class ProcessVideosCommand extends ContainerAwareCommand
{
private $videos;
private $manager;
protected function configure()
{
$this
->setName('media:video:process')
->setDescription('Upload videos to amazon s3 to be processed by elastic transcoder')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->manager = $this->getContainer()->get('doctrine')->getManager();
$this->queryVideos();
if ($this->hasNoVideos()) {
$output->writeln('No videos to process');
} else {
$output->writeln(sprintf('%d videos to process', count($this->videos)));
}
foreach ($this->videos as $video) {
$output->writeln(sprintf('Processing file: %s', realpath($video->getFilePath())));
$amazonVideo = $this->getContainer()->get('coshi.amazon_transcoder.transcoder');
$amazonVideo->setVideo($video);
$output->writeln(' Uploading...');
$s3Object = $amazonVideo->upload();
if (is_null($s3Object)) {
$output->writeln(' This video has been uploaded already!');
} else {
$output->writeln(' Upload Finished!');
}
$output->writeln(' Converting...');
$job = $amazonVideo->convert();
if (is_null($job)) {
$output->writeln(' This video has been queued for conversion already!');
} else {
$output->writeln(sprintf(' Transcoder job created! id: %s', $job['Job']['Id']));
}
$book = $this->manager->getRepository('AppBundle:Book')->findOneBy(array('hash' => $video->getCode()));
if ($book instanceof Book) {
$book->setTranscodeStatus(Book::TRANSCODE_STATUS_PROCESSING);
}
}
$this->manager->flush();
}
private function queryVideos()
{
$this->videos = $this->getMediaProvider()->findAllUnprocessedVideos();
}
public function hasNoVideos()
{
return empty($this->videos);
}
private function getConfig($key)
{
return $this->getContainer()->getParameter('coshi_transcode')[$key];
}
/**
* @return TranscodeableProviderInterface
* @throws InvalidConfigurationException
*/
private function getMediaProvider()
{
$providerType = $this->getConfig('media_provider')['type'];
$providerName = $this->getConfig('media_provider')['name'];
$provider = null;
if (TranscodeableProviderInterface::RETRIEVER_TYPE_REPOSITORY === $providerType) {
$provider = $this->manager->getRepository($providerName);
}
if (TranscodeableProviderInterface::RETRIEVER_TYPE_SERVICE === $providerType) {
$provider = $this->getContainer()->get($providerName);
}
if (!$provider instanceof TranscodeableProviderInterface) {
throw new InvalidConfigurationException(sprintf('Provider class should exist and implement "%s" interface',
'Coshi\\Bundle\\TranscodeBundle\\Transcoder\\TranscodeableProviderInterface'));
}
return $provider;
}
}