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, transcode all media files in a directory') |
52
|
3 |
|
->setDefinition( |
53
|
3 |
|
new InputDefinition([ |
54
|
3 |
|
new InputOption('dir', 'd', InputOption::VALUE_REQUIRED, 'Directory to convert'), |
55
|
3 |
|
new InputOption('preset', 'p', InputOption::VALUE_REQUIRED, 'Conversion preset to use'), |
56
|
|
|
]) |
57
|
|
|
); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
61
|
|
|
{ |
62
|
|
|
// ######################## |
63
|
|
|
// Step 1: Check directory |
64
|
|
|
// ######################## |
65
|
|
|
|
66
|
3 |
|
$directory = $input->getOption('dir'); |
67
|
3 |
|
Assert::stringNotEmpty($directory); |
68
|
2 |
|
Assert::directory($directory); |
69
|
|
|
|
70
|
|
|
// ######################## |
71
|
|
|
// Step 2: Init preset |
72
|
|
|
// ######################## |
73
|
|
|
|
74
|
1 |
|
Assert::stringNotEmpty($input->getOption('preset')); |
75
|
1 |
|
$preset = $this->getPreset($input->getOption('preset')); |
76
|
|
|
|
77
|
|
|
// ######################## |
78
|
|
|
// Step 3: Scanning dir |
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, ['mp4']); |
85
|
|
|
|
86
|
1 |
|
$output->writeln('* Reading metadata...'); |
87
|
|
|
|
88
|
1 |
|
$progressBar = new ProgressBar($output, count($files)); |
89
|
|
|
//$progressBar->start(); |
90
|
|
|
|
91
|
1 |
|
$converter = $this->mediaTools->getConverter(); |
92
|
|
|
|
93
|
|
|
/** @var \SplFileInfo $file */ |
94
|
1 |
|
foreach ($files as $file) { |
95
|
|
|
try { |
96
|
|
|
$params = $preset->getParams($file->getPathname()); |
97
|
|
|
|
98
|
|
|
$outputFile = sprintf('%s%s', (string) $file, '.mov'); |
99
|
|
|
|
100
|
|
|
if (!file_exists($outputFile)) { |
101
|
|
|
$converter->convert((string) $file, $outputFile, $params, function ($stdOut, $stdErr) use ($output): void { |
102
|
|
|
$output->write($stdErr); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
$output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file)); |
106
|
|
|
} else { |
107
|
|
|
$output->writeln(sprintf('<fg=yellow>- Skipped:</> %s : Output file already exists.', $file)); |
108
|
|
|
} |
109
|
|
|
} catch (ProcessException $e) { |
110
|
|
|
$output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
//$progressBar->advance(); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$progressBar->finish(); |
117
|
1 |
|
$output->writeln(''); |
118
|
|
|
|
119
|
1 |
|
return 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
private function getPreset(string $presetName): PresetInterface |
123
|
|
|
{ |
124
|
1 |
|
return $this->presetLoader->getPreset($presetName); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|