1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Cli\Media; |
6
|
|
|
|
7
|
|
|
use ScriptFUSION\Byte\ByteFormatter; |
8
|
|
|
use Soluble\MediaTools\Cli\Exception\MissingFFProbeBinaryException; |
9
|
|
|
use Soluble\MediaTools\Video\Exception as VideoException; |
10
|
|
|
use Soluble\MediaTools\Video\SeekTime; |
11
|
|
|
use Soluble\MediaTools\Video\VideoInfoReaderInterface; |
12
|
|
|
use SplFileInfo; |
13
|
|
|
|
14
|
|
|
class MediaScanner |
15
|
|
|
{ |
16
|
|
|
/** @var VideoInfoReaderInterface */ |
17
|
|
|
private $reader; |
18
|
|
|
|
19
|
2 |
|
public function __construct(VideoInfoReaderInterface $videoInfoReader) |
20
|
|
|
{ |
21
|
2 |
|
$this->reader = $videoInfoReader; |
22
|
2 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param SplFileInfo[] $files |
26
|
|
|
*/ |
27
|
2 |
|
public function getMedias(array $files, ?callable $callback): array |
28
|
|
|
{ |
29
|
2 |
|
$bitRateFormatter = new ByteFormatter(); |
30
|
2 |
|
$sizeFormatter = new ByteFormatter(); |
31
|
|
|
|
32
|
2 |
|
$rows = []; |
33
|
2 |
|
$warnings = []; |
34
|
2 |
|
$totalSize = 0; |
35
|
|
|
|
36
|
|
|
/** @var SplFileInfo $file */ |
37
|
2 |
|
foreach ($files as $file) { |
38
|
2 |
|
$videoFile = $file->getPathname(); |
39
|
|
|
try { |
40
|
2 |
|
$info = $this->reader->getInfo($videoFile); |
41
|
1 |
|
$vStream = $info->getVideoStreams()->getFirst(); |
42
|
1 |
|
$aStream = $info->getAudioStreams()->getFirst(); |
43
|
1 |
|
$pixFmt = $vStream->getPixFmt(); |
44
|
1 |
|
$bitRate = $vStream->getBitRate(); |
45
|
1 |
|
$fileSize = $file->getSize(); |
46
|
1 |
|
$frames = $vStream->getNbFrames(); |
47
|
|
|
|
48
|
1 |
|
$fps = (string) ($vStream->getFps(0) ?? ''); |
49
|
|
|
|
50
|
|
|
$row = [ |
51
|
1 |
|
'file' => $file, |
52
|
1 |
|
'duration' => preg_replace('/\.([0-9])+$/', '', SeekTime::convertSecondsToHMSs(round($info->getDuration(), 1))), |
53
|
1 |
|
'frames' => $frames, |
54
|
1 |
|
'total_time' => $info->getDuration(), |
55
|
1 |
|
'codec' => sprintf('%s/%s', $vStream->getCodecName(), $aStream->getCodecName()), |
56
|
1 |
|
'resolution' => sprintf( |
57
|
1 |
|
'%sx%s', |
58
|
1 |
|
$vStream->getWidth(), |
59
|
1 |
|
$vStream->getHeight() |
60
|
|
|
), |
61
|
1 |
|
'fps' => $fps, |
62
|
1 |
|
'bitrate' => ($bitRate > 0 ? $bitRateFormatter->format((int) $bitRate) . '/s' : ''), |
63
|
1 |
|
'size' => $sizeFormatter->format($fileSize), |
64
|
1 |
|
'pixFmt' => $pixFmt, |
65
|
|
|
]; |
66
|
1 |
|
$rows[] = $row; |
67
|
1 |
|
$totalSize += $fileSize; |
68
|
2 |
|
} catch (VideoException\MissingFFProbeBinaryException $e) { |
69
|
1 |
|
throw new MissingFFProbeBinaryException('Unable to run ffprobe binary, check your config and ensure it\'s properly installed'); |
70
|
1 |
|
} catch (VideoException\InfoReaderExceptionInterface $e) { |
71
|
1 |
|
$warnings[] = [$videoFile]; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
if ($callback === null) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$callback(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return [ |
82
|
1 |
|
'rows' => $rows, |
83
|
1 |
|
'totalSize' => $totalSize, |
84
|
1 |
|
'warnings' => $warnings, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|