1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Video\Info; |
6
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Video\Exception\InvalidStreamMetadataException; |
8
|
|
|
use Soluble\MediaTools\Video\Exception\StreamNotFoundException; |
9
|
|
|
|
10
|
|
|
class VideoStreamCollection implements StreamCollectionInterface |
11
|
|
|
{ |
12
|
|
|
/** @var array<int, array> */ |
13
|
|
|
private $streamsMetadata; |
14
|
|
|
|
15
|
|
|
/** @var array<int, VideoStream> */ |
16
|
|
|
private $streams; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array<int, array> $videoStreamsMetadata |
20
|
|
|
* @throws InvalidStreamMetadataException |
21
|
|
|
*/ |
22
|
2 |
|
public function __construct(array $videoStreamsMetadata) |
23
|
|
|
{ |
24
|
2 |
|
$this->streamsMetadata = $videoStreamsMetadata; |
25
|
2 |
|
$this->loadStreams(); |
26
|
2 |
|
} |
27
|
|
|
|
28
|
1 |
|
public function getFirst(): VideoStream |
29
|
|
|
{ |
30
|
1 |
|
if ($this->count() === 0) { |
31
|
|
|
throw new StreamNotFoundException('Unable to get video first stream, none exists'); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
return new VideoStream($this->streamsMetadata[0]); |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function count(): int |
38
|
|
|
{ |
39
|
2 |
|
return count($this->streamsMetadata); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function getIterator(): \ArrayIterator |
43
|
|
|
{ |
44
|
1 |
|
return new \ArrayIterator($this->streams); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws InvalidStreamMetadataException |
49
|
|
|
*/ |
50
|
2 |
|
private function loadStreams(): void |
51
|
|
|
{ |
52
|
2 |
|
$this->streams = []; |
53
|
2 |
|
foreach ($this->streamsMetadata as $idx => $metadata) { |
54
|
2 |
|
if (!is_array($metadata)) { |
55
|
|
|
throw new InvalidStreamMetadataException(sprintf( |
56
|
|
|
'Invalid or unsupported metadata stream received %s', |
57
|
|
|
(string) json_encode($metadata) |
58
|
|
|
)); |
59
|
|
|
} |
60
|
2 |
|
$this->streams[] = new VideoStream($metadata); |
61
|
|
|
} |
62
|
2 |
|
} |
63
|
|
|
} |
64
|
|
|
|