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\Cli\Media\FileExtensions; |
16
|
|
|
use Soluble\MediaTools\Cli\Service\MediaToolsServiceInterface; |
17
|
|
|
use Soluble\MediaTools\Common\Exception\ProcessException; |
18
|
|
|
use Soluble\MediaTools\Preset\PresetInterface; |
19
|
|
|
use Soluble\MediaTools\Preset\PresetLoader; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
22
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Webmozart\Assert\Assert; |
27
|
|
|
|
28
|
|
|
class ConvertDirCommand extends Command |
29
|
|
|
{ |
30
|
|
|
/** @var MediaToolsServiceInterface */ |
31
|
|
|
private $mediaTools; |
32
|
|
|
|
33
|
|
|
/** @var PresetLoader */ |
34
|
|
|
private $presetLoader; |
35
|
|
|
|
36
|
|
|
/** @var string[] */ |
37
|
|
|
private $supportedVideoExtensions; |
38
|
|
|
|
39
|
3 |
|
public function __construct(MediaToolsServiceInterface $mediaTools, PresetLoader $presetLoader) |
40
|
|
|
{ |
41
|
3 |
|
$this->mediaTools = $mediaTools; |
42
|
3 |
|
$this->presetLoader = $presetLoader; |
43
|
3 |
|
$this->supportedVideoExtensions = (new FileExtensions())->getMediaExtensions(); |
44
|
3 |
|
parent::__construct(); |
45
|
3 |
|
} |
46
|
|
|
|
47
|
3 |
|
protected function configure(): void |
48
|
|
|
{ |
49
|
|
|
$this |
50
|
3 |
|
->setName('convert:directory') |
51
|
3 |
|
->setDescription('Convert all media files in a directory using a preset') |
52
|
3 |
|
->setDefinition( |
53
|
3 |
|
new InputDefinition([ |
54
|
3 |
|
new InputOption('dir', ['d'], InputOption::VALUE_REQUIRED, 'Input directory to scan for medias'), |
55
|
3 |
|
new InputOption('preset', ['p'], InputOption::VALUE_REQUIRED, 'Conversion preset to use'), |
56
|
3 |
|
new InputOption('exts', ['e', 'extensions'], InputOption::VALUE_OPTIONAL, 'File extensions to process (ie. m4v,mp4,mov)'), |
57
|
3 |
|
new InputOption('output', ['o', 'out'], InputOption::VALUE_REQUIRED, 'Output directory'), |
58
|
|
|
]) |
59
|
|
|
); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
63
|
|
|
{ |
64
|
|
|
// ######################## |
65
|
|
|
// Step 1: Check directory |
66
|
|
|
// ######################## |
67
|
|
|
|
68
|
3 |
|
$directory = $input->getOption('dir'); |
69
|
3 |
|
Assert::stringNotEmpty($directory); |
70
|
2 |
|
Assert::directory($directory); |
71
|
|
|
|
72
|
|
|
// ######################## |
73
|
|
|
// Step 2: Init preset |
74
|
|
|
// ######################## |
75
|
|
|
|
76
|
1 |
|
Assert::stringNotEmpty($input->getOption('preset')); |
77
|
1 |
|
$preset = $this->getPreset($input->getOption('preset')); |
78
|
|
|
|
79
|
|
|
// ######################## |
80
|
|
|
// Step 3: Output dir |
81
|
|
|
// ######################## |
82
|
|
|
|
83
|
1 |
|
if ($input->getOption('output') !== null) { |
84
|
1 |
|
$outputDir = $input->getOption('output'); |
85
|
1 |
|
Assert::directory($outputDir); |
86
|
1 |
|
Assert::writable($outputDir); |
87
|
|
|
} else { |
88
|
|
|
$outputDir = $directory; |
89
|
|
|
} |
90
|
1 |
|
Assert::stringNotEmpty($outputDir); |
91
|
|
|
|
92
|
|
|
// ######################## |
93
|
|
|
// Step 4: Extensions |
94
|
|
|
// ######################## |
95
|
|
|
|
96
|
1 |
|
if ($input->getOption('exts') !== null) { |
97
|
1 |
|
$tmp = $input->getOption('exts'); |
98
|
1 |
|
Assert::stringNotEmpty($tmp); |
99
|
1 |
|
$exts = array_filter( |
100
|
1 |
|
array_map( |
101
|
1 |
|
'trim', |
102
|
1 |
|
explode(',', $tmp) |
|
|
|
|
103
|
|
|
) |
104
|
|
|
); |
105
|
1 |
|
Assert::minCount($exts, 1); |
106
|
|
|
} else { |
107
|
|
|
$exts = FileExtensions::BUILTIN_EXTENSIONS; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// ######################## |
111
|
|
|
// Step 5: Scanning dir |
112
|
|
|
// ######################## |
113
|
|
|
|
114
|
1 |
|
$output->writeln(sprintf('* Scanning %s for media files...', $directory)); |
|
|
|
|
115
|
|
|
|
116
|
|
|
// Get the videos in path |
117
|
1 |
|
$files = (new DirectoryScanner())->findFiles($directory, $exts); |
118
|
|
|
|
119
|
1 |
|
$output->writeln('* Reading metadata...'); |
120
|
|
|
|
121
|
1 |
|
$progressBar = new ProgressBar($output, count($files)); |
122
|
|
|
//$progressBar->start(); |
123
|
|
|
|
124
|
1 |
|
$converter = $this->mediaTools->getConverter(); |
125
|
|
|
|
126
|
|
|
/** @var \SplFileInfo $file */ |
127
|
1 |
|
foreach ($files as $file) { |
128
|
|
|
try { |
129
|
1 |
|
$params = $preset->getParams($file->getPathname()); |
130
|
|
|
|
131
|
1 |
|
$outputFile = sprintf( |
132
|
1 |
|
'%s/%s%s', |
133
|
|
|
$outputDir, |
134
|
1 |
|
$file->getBasename($file->getExtension()), |
135
|
1 |
|
$preset->getFileExtension() |
136
|
|
|
); |
137
|
|
|
|
138
|
1 |
|
if (!file_exists($outputFile)) { |
139
|
|
|
$converter->convert((string) $file, $outputFile, $params, function ($stdOut, $stdErr) use ($output): void { |
140
|
1 |
|
$output->write($stdErr); |
141
|
1 |
|
}); |
142
|
|
|
|
143
|
1 |
|
$output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file)); |
144
|
|
|
} else { |
145
|
1 |
|
$output->writeln(sprintf('<fg=yellow>- Skipped:</> %s : Output file already exists.', $file)); |
146
|
|
|
} |
147
|
1 |
|
} catch (ProcessException $e) { |
148
|
1 |
|
$output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
//$progressBar->advance(); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$progressBar->finish(); |
155
|
1 |
|
$output->writeln(''); |
156
|
|
|
|
157
|
1 |
|
return 0; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
private function getPreset(string $presetName): PresetInterface |
161
|
|
|
{ |
162
|
1 |
|
return $this->presetLoader->getPreset($presetName); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|