|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @see https://github.com/soluble-io/soluble-mediatools-cli for the canonical repository |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude) |
|
9
|
|
|
* @license https://github.com/soluble-io/soluble-mediatools-cli/blob/master/LICENSE.md MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Soluble\MediaTools\Cli\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Soluble\MediaTools\Cli\FileSystem\DirectoryScanner; |
|
15
|
|
|
use Soluble\MediaTools\Video\VideoAnalyzerInterface; |
|
16
|
|
|
use Soluble\MediaTools\Video\VideoConverterInterface; |
|
17
|
|
|
use Soluble\MediaTools\Video\VideoInfoReaderInterface; |
|
18
|
|
|
use Symfony\Component\Console\Command\Command; |
|
19
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
|
|
25
|
|
|
class ConvertDirCommand extends Command |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var VideoInfoReaderInterface */ |
|
28
|
|
|
protected $videoInfoReader; |
|
29
|
|
|
|
|
30
|
|
|
/** @var VideoAnalyzerInterface */ |
|
31
|
|
|
protected $videoAnalyzer; |
|
32
|
|
|
|
|
33
|
|
|
/** @var VideoConverterInterface */ |
|
34
|
|
|
protected $videoConverter; |
|
35
|
|
|
|
|
36
|
|
|
/** @var string[] */ |
|
37
|
|
|
protected $supportedVideoExtensions = [ |
|
38
|
|
|
'mov', |
|
39
|
|
|
'mp4', |
|
40
|
|
|
'mkv', |
|
41
|
|
|
'flv', |
|
42
|
|
|
'webm', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
3 |
|
public function __construct(VideoInfoReaderInterface $videoInfoReader, VideoAnalyzerInterface $videoAnalyzer, VideoConverterInterface $videoConverter) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->videoInfoReader = $videoInfoReader; |
|
48
|
3 |
|
$this->videoAnalyzer = $videoAnalyzer; |
|
49
|
3 |
|
$this->videoConverter = $videoConverter; |
|
50
|
3 |
|
parent::__construct(); |
|
51
|
3 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Configures the command. |
|
55
|
|
|
*/ |
|
56
|
3 |
|
protected function configure(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this |
|
59
|
3 |
|
->setName('convert:directory') |
|
60
|
3 |
|
->setDescription('Convert, transcode all media files in a directory') |
|
61
|
3 |
|
->setDefinition( |
|
62
|
3 |
|
new InputDefinition([ |
|
63
|
3 |
|
new InputOption('dir', 'd', InputOption::VALUE_REQUIRED), |
|
64
|
|
|
]) |
|
65
|
|
|
); |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
69
|
|
|
{ |
|
70
|
3 |
|
if (!$input->hasOption('dir')) { |
|
71
|
|
|
throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>'); |
|
72
|
|
|
} |
|
73
|
3 |
|
$directory = $input->hasOption('dir') ? $input->getOption('dir') : null; |
|
74
|
3 |
|
if (!is_string($directory) || !is_dir($directory)) { |
|
75
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
|
76
|
2 |
|
'Directory %s does not exists', |
|
77
|
2 |
|
is_string($directory) ? $directory : json_encode($directory) |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$output->writeln(sprintf('* Scanning %s for media files...', $directory)); |
|
82
|
|
|
|
|
83
|
|
|
// Get the videos in path |
|
84
|
1 |
|
$files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions); |
|
85
|
|
|
|
|
86
|
1 |
|
$output->writeln('* Reading metadata...'); |
|
87
|
|
|
|
|
88
|
1 |
|
$progressBar = new ProgressBar($output, count($files)); |
|
89
|
1 |
|
$progressBar->start(); |
|
90
|
|
|
|
|
91
|
|
|
/* @var \SplFileInfo $video */ |
|
92
|
1 |
|
foreach ($files as $file) { |
|
93
|
1 |
|
$progressBar->advance(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$progressBar->finish(); |
|
97
|
1 |
|
$output->writeln(''); |
|
98
|
|
|
|
|
99
|
1 |
|
return 0; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|