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 ScriptFUSION\Byte\ByteFormatter; |
15
|
|
|
use Soluble\MediaTools\Cli\Exception\InvalidArgumentException; |
16
|
|
|
use Soluble\MediaTools\Cli\FileSystem\DirectoryScanner; |
17
|
|
|
use Soluble\MediaTools\Cli\Media\FileExtensions; |
18
|
|
|
use Soluble\MediaTools\Cli\Media\MediaScanner; |
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\Helper\Table; |
23
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
24
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
25
|
|
|
use Symfony\Component\Console\Helper\TableStyle; |
26
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Input\InputOption; |
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
30
|
|
|
|
31
|
|
|
class ScanCommand extends Command |
32
|
|
|
{ |
33
|
|
|
/** @var VideoInfoReaderInterface */ |
34
|
|
|
private $reader; |
35
|
|
|
|
36
|
|
|
/** @var string[] */ |
37
|
|
|
private $supportedVideoExtensions; |
38
|
|
|
|
39
|
4 |
|
public function __construct(VideoInfoReaderInterface $videoInfoReader) |
40
|
|
|
{ |
41
|
4 |
|
$this->reader = $videoInfoReader; |
42
|
4 |
|
$this->supportedVideoExtensions = (new FileExtensions())->getMediaExtensions(); |
43
|
4 |
|
parent::__construct(); |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Configures the command. |
48
|
|
|
*/ |
49
|
4 |
|
protected function configure(): void |
50
|
|
|
{ |
51
|
|
|
$this |
52
|
4 |
|
->setName('scan:videos') |
53
|
4 |
|
->setDescription('Scan for video') |
54
|
4 |
|
->setDefinition( |
55
|
4 |
|
new InputDefinition([ |
56
|
4 |
|
new InputOption('dir', 'd', InputOption::VALUE_REQUIRED, 'Directory to scan'), |
57
|
4 |
|
new InputOption('recursive', 'r', InputOption::VALUE_NONE, 'Recursive mode'), |
58
|
|
|
]) |
59
|
|
|
); |
60
|
4 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws InvalidArgumentException |
64
|
|
|
*/ |
65
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
66
|
|
|
{ |
67
|
4 |
|
if (!$input->hasOption('dir')) { |
68
|
|
|
throw new InvalidArgumentException('Missing dir argument, use <command> <dir>'); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
$videoPath = $input->getOption('dir'); |
72
|
4 |
|
if (!is_string($videoPath) || !is_dir($videoPath)) { |
73
|
2 |
|
throw new InvalidArgumentException(sprintf( |
74
|
2 |
|
'Video dir %s does not exists', |
75
|
2 |
|
is_string($videoPath) ? $videoPath : json_encode($videoPath) |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$recursive = $input->getOption('recursive') === true; |
80
|
|
|
|
81
|
2 |
|
$output->writeln(sprintf('* Scanning %s for files...', $videoPath)); |
82
|
|
|
|
83
|
|
|
// Get the videos in path |
84
|
2 |
|
$videos = (new DirectoryScanner())->findFiles($videoPath, $this->supportedVideoExtensions, $recursive); |
85
|
|
|
|
86
|
2 |
|
$output->writeln('* Reading metadata...'); |
87
|
|
|
|
88
|
2 |
|
$progressBar = new ProgressBar($output, count($videos)); |
89
|
2 |
|
$progressBar->start(); |
90
|
|
|
|
91
|
|
|
$medias = (new MediaScanner($this->reader))->getMedias($videos, function () use ($progressBar) { |
92
|
1 |
|
$progressBar->advance(); |
93
|
2 |
|
}); |
94
|
|
|
|
95
|
1 |
|
$progressBar->finish(); |
96
|
|
|
|
97
|
1 |
|
$output->writeln(''); |
98
|
1 |
|
$output->writeln('* Available media files:'); |
99
|
|
|
|
100
|
1 |
|
$this->renderResultsTable($output, $medias['rows'], $medias['totalSize']); |
101
|
|
|
|
102
|
|
|
// display warnings |
103
|
|
|
|
104
|
1 |
|
if (count($medias['warnings']) > 0) { |
105
|
1 |
|
$output->writeln('* The following files were not detected as valid medias:'); |
106
|
1 |
|
$table = new Table($output); |
107
|
1 |
|
$table->setHeaders([ |
108
|
1 |
|
'Unsupported files', |
109
|
|
|
]); |
110
|
1 |
|
$table->setStyle('box-double'); |
111
|
1 |
|
$table->setRows($medias['warnings']); |
112
|
1 |
|
$table->render(); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$output->writeln(''); |
116
|
|
|
|
117
|
1 |
|
return 0; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
private function renderResultsTable(OutputInterface $output, array $rows, int $totalSize, array $columns = []): void |
|
|
|
|
121
|
|
|
{ |
122
|
1 |
|
$sizeFormatter = new ByteFormatter(); |
123
|
|
|
|
124
|
1 |
|
$table = new Table($output); |
125
|
|
|
|
126
|
1 |
|
$rightAlignstyle = new TableStyle(); |
127
|
1 |
|
$rightAlignstyle->setPadType(STR_PAD_LEFT); |
128
|
|
|
|
129
|
|
|
//$table->setStyle($tableStyle); |
130
|
1 |
|
$table->setStyle('box'); |
131
|
1 |
|
$table->setHeaders([ |
132
|
1 |
|
'file', |
133
|
|
|
'duration', |
134
|
|
|
'codec', |
135
|
|
|
'size', |
136
|
|
|
'fps', |
137
|
|
|
'bitrate', |
138
|
|
|
'filesize', |
139
|
|
|
'pix_fmt', |
140
|
|
|
]); |
141
|
|
|
|
142
|
1 |
|
foreach ($colIndexes = [1, 2, 3, 4, 5, 6] as $idx) { |
|
|
|
|
143
|
1 |
|
$table->setColumnStyle($idx, $rightAlignstyle); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
$previousPath = null; |
147
|
1 |
|
$first = true; |
148
|
|
|
|
149
|
1 |
|
foreach ($rows as $idx => $row) { |
150
|
|
|
/** @var \SplFileInfo $file */ |
151
|
1 |
|
$file = $row['video']; |
152
|
1 |
|
$fileName = $file->getBasename(); |
153
|
1 |
|
$fileName = mb_strlen($fileName) > 30 ? mb_substr($file->getBasename(), 0, 30) . '[...].' . $file->getExtension() : $fileName; |
154
|
1 |
|
$row['video'] = $fileName; |
155
|
1 |
|
if ($previousPath !== $file->getPath()) { |
156
|
1 |
|
if (!$first) { |
157
|
|
|
$table->addRow(new TableSeparator(['colspan' => count($row)])); |
158
|
|
|
} |
159
|
1 |
|
$table->addRow([new TableCell(sprintf('<fg=yellow>%s</>', $file->getPath()), ['colspan' => count($row)])]); |
160
|
1 |
|
$table->addRow(new TableSeparator(['colspan' => count($row)])); |
161
|
1 |
|
$previousPath = $file->getPath(); |
162
|
|
|
} |
163
|
1 |
|
$table->addRow($row); |
164
|
1 |
|
$first = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
$table->addRow(new TableSeparator()); |
168
|
1 |
|
$table->addRow(['<fg=cyan>Total</>', '', '', '', '', sprintf('<fg=cyan>%s</>', $sizeFormatter->format($totalSize))]); |
169
|
|
|
|
170
|
1 |
|
$table->render(); |
171
|
1 |
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.