1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Video; |
6
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Common\Exception\JsonParseException; |
8
|
|
|
|
9
|
|
|
class Info implements InfoInterface |
10
|
|
|
{ |
11
|
|
|
public const STREAM_TYPE_AUDIO = 'audio'; |
12
|
|
|
public const STREAM_TYPE_VIDEO = 'video'; |
13
|
|
|
public const STREAM_TYPE_DATA = 'data'; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $metadata; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $file; |
20
|
|
|
|
21
|
1 |
|
public function __construct(string $file, array $metadata) |
22
|
|
|
{ |
23
|
1 |
|
$this->metadata = $metadata; |
24
|
1 |
|
$this->file = $file; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public static function createFromFFProbeJson(string $file, string $ffprobeJson): self |
28
|
|
|
{ |
29
|
1 |
|
if (trim($ffprobeJson) === '') { |
30
|
|
|
throw new JsonParseException('Cannot parse empty json string'); |
31
|
|
|
} |
32
|
1 |
|
$decoded = json_decode($ffprobeJson, true); |
33
|
1 |
|
if ($decoded === null) { |
34
|
|
|
throw new JsonParseException('Cannot parse json'); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return new self($file, $decoded); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function getFile(): string |
41
|
|
|
{ |
42
|
1 |
|
return $this->file; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getMetadata(): array |
46
|
|
|
{ |
47
|
|
|
return $this->metadata; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getDuration(): float |
51
|
|
|
{ |
52
|
1 |
|
return (float) ($this->metadata['format']['duration'] ?? 0.0); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function getDimensions(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
1 |
|
'width' => $this->getWidth(), |
59
|
1 |
|
'height' => $this->getHeight(), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function getWidth(): int |
64
|
|
|
{ |
65
|
1 |
|
$videoStream = $this->getVideoStreamInfo(); |
66
|
|
|
|
67
|
1 |
|
return (int) ($videoStream['width'] ?? 0); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function getHeight(): int |
71
|
|
|
{ |
72
|
1 |
|
$videoStream = $this->getVideoStreamInfo(); |
73
|
|
|
|
74
|
1 |
|
return (int) ($videoStream['height'] ?? 0); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function getNbFrames(): int |
78
|
|
|
{ |
79
|
1 |
|
$videoStream = $this->getVideoStreamInfo(); |
80
|
|
|
|
81
|
1 |
|
return (int) ($videoStream['nb_frames'] ?? 0); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getBitrate(): int |
85
|
|
|
{ |
86
|
|
|
$videoStream = $this->getVideoStreamInfo(); |
87
|
|
|
|
88
|
|
|
return (int) ($videoStream['bit_rate'] ?? 0); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getAudioStreamInfo(): ?array |
92
|
|
|
{ |
93
|
|
|
return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getVideoStreamInfo(): ?array |
97
|
|
|
{ |
98
|
1 |
|
return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
protected function getStreamsByType(): array |
102
|
|
|
{ |
103
|
1 |
|
$streams = $this->metadata['streams'] ?? []; |
104
|
1 |
|
foreach ($streams as $stream) { |
105
|
1 |
|
$type = mb_strtolower($stream['codec_type']); |
106
|
|
|
switch ($type) { |
107
|
1 |
|
case self::STREAM_TYPE_VIDEO: |
|
|
|
|
108
|
1 |
|
$streams['video'] = $stream; |
109
|
1 |
|
break; |
110
|
1 |
|
case self::STREAM_TYPE_AUDIO: |
|
|
|
|
111
|
1 |
|
$streams['audio'] = $stream; |
112
|
1 |
|
break; |
113
|
|
|
case self::STREAM_TYPE_DATA: |
|
|
|
|
114
|
|
|
$streams['data'] = $stream; |
115
|
|
|
break; |
116
|
|
|
default: |
|
|
|
|
117
|
1 |
|
throw new \Exception(sprintf('Does not support codec_type "%s"', $type)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $streams; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.