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