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\Common\Exception\ProcessException; |
16
|
|
|
use Soluble\MediaTools\Preset\MP4\StreamableH264Preset; |
17
|
|
|
use Soluble\MediaTools\Video\VideoAnalyzerInterface; |
18
|
|
|
use Soluble\MediaTools\Video\VideoConverterInterface; |
19
|
|
|
use Soluble\MediaTools\Video\VideoInfoReaderInterface; |
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
|
|
|
|
27
|
|
|
class ConvertDirCommand extends Command |
28
|
|
|
{ |
29
|
|
|
/** @var VideoInfoReaderInterface */ |
30
|
|
|
protected $videoInfoReader; |
31
|
|
|
|
32
|
|
|
/** @var VideoAnalyzerInterface */ |
33
|
|
|
protected $videoAnalyzer; |
34
|
|
|
|
35
|
|
|
/** @var VideoConverterInterface */ |
36
|
|
|
protected $videoConverter; |
37
|
|
|
|
38
|
|
|
/** @var string[] */ |
39
|
|
|
protected $supportedVideoExtensions = [ |
40
|
|
|
'mov', |
41
|
|
|
'mp4', |
42
|
|
|
'm4v', |
43
|
|
|
'avi', |
44
|
|
|
'mkv', |
45
|
|
|
'flv', |
46
|
|
|
'webm', |
47
|
|
|
]; |
48
|
|
|
|
49
|
3 |
|
public function __construct(VideoInfoReaderInterface $videoInfoReader, VideoAnalyzerInterface $videoAnalyzer, VideoConverterInterface $videoConverter) |
50
|
|
|
{ |
51
|
3 |
|
$this->videoInfoReader = $videoInfoReader; |
52
|
3 |
|
$this->videoAnalyzer = $videoAnalyzer; |
53
|
3 |
|
$this->videoConverter = $videoConverter; |
54
|
3 |
|
parent::__construct(); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Configures the command. |
59
|
|
|
*/ |
60
|
3 |
|
protected function configure(): void |
61
|
|
|
{ |
62
|
|
|
$this |
63
|
3 |
|
->setName('convert:directory') |
64
|
3 |
|
->setDescription('Convert, transcode all media files in a directory') |
65
|
3 |
|
->setDefinition( |
66
|
3 |
|
new InputDefinition([ |
67
|
3 |
|
new InputOption('dir', 'd', InputOption::VALUE_REQUIRED), |
68
|
|
|
]) |
69
|
|
|
); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
73
|
|
|
{ |
74
|
3 |
|
if (!$input->hasOption('dir')) { |
75
|
|
|
throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>'); |
76
|
|
|
} |
77
|
3 |
|
$directory = $input->hasOption('dir') ? $input->getOption('dir') : null; |
78
|
3 |
|
if (!is_string($directory) || !is_dir($directory)) { |
79
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
80
|
2 |
|
'Directory %s does not exists', |
81
|
2 |
|
is_string($directory) ? $directory : json_encode($directory) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$output->writeln(sprintf('* Scanning %s for media files...', $directory)); |
86
|
|
|
|
87
|
|
|
// Get the videos in path |
88
|
1 |
|
$files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions); |
89
|
|
|
|
90
|
1 |
|
$output->writeln('* Reading metadata...'); |
91
|
|
|
|
92
|
1 |
|
$progressBar = new ProgressBar($output, count($files)); |
93
|
|
|
//$progressBar->start(); |
94
|
|
|
|
95
|
1 |
|
$preset = new StreamableH264Preset($this->videoInfoReader, $this->videoConverter, $this->videoAnalyzer); |
96
|
|
|
|
97
|
|
|
/** @var \SplFileInfo $file */ |
98
|
1 |
|
foreach ($files as $file) { |
99
|
|
|
try { |
100
|
1 |
|
$preset->convert($file->getPathname()); |
101
|
1 |
|
$output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file)); |
102
|
1 |
|
} catch (ProcessException $e) { |
103
|
1 |
|
$output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//$progressBar->advance(); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$progressBar->finish(); |
110
|
1 |
|
$output->writeln(''); |
111
|
|
|
|
112
|
1 |
|
return 0; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|