|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Streaming; |
|
13
|
|
|
|
|
14
|
|
|
use Streaming\Exception\InvalidArgumentException; |
|
15
|
|
|
use Streaming\MediaInfo\Streams\Stream; |
|
16
|
|
|
use Streaming\MediaInfo\Streams\StreamCollection; |
|
17
|
|
|
|
|
18
|
|
|
class AutoRepresentations |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Stream $video */ |
|
21
|
|
|
private $video; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Stream $general */ |
|
24
|
|
|
private $general; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* regular video's heights |
|
28
|
|
|
* |
|
29
|
|
|
* @var array side_values |
|
30
|
|
|
*/ |
|
31
|
|
|
private $side_values = [2160, 1080, 720, 480, 360, 240, 144]; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array $k_bitrate_values */ |
|
34
|
|
|
private $k_bitrate_values; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* AutoRepresentations constructor. |
|
38
|
|
|
* @param StreamCollection $streamCollection |
|
39
|
|
|
* @param null | array $side_values |
|
40
|
|
|
* @param array $k_bitrate_values |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(StreamCollection $streamCollection, array $side_values = null, array $k_bitrate_values = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->video = $streamCollection->videos()->first(); |
|
45
|
|
|
$this->general = $streamCollection->general(); |
|
46
|
|
|
$this->getSideValues($side_values); |
|
47
|
|
|
$this->getKiloBitrateValues($k_bitrate_values); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getDimensions(): array |
|
54
|
|
|
{ |
|
55
|
|
|
$width = $this->video->get('Width'); |
|
|
|
|
|
|
56
|
|
|
$height = $this->video->get('Height'); |
|
57
|
|
|
|
|
58
|
|
|
return [$width, $height, $width / $height]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
* @throws InvalidArgumentException |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getKiloBitRate(): int |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$this->video->has('BitRate')) { |
|
68
|
|
|
if (!$this->general->has('OverallBitRate')) { |
|
69
|
|
|
throw new InvalidArgumentException("Invalid stream"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return intval(($this->general->get('OverallBitRate') / 1024) * .9); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return (int)$this->video->get('BitRate') / 1024; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function get(): array |
|
82
|
|
|
{ |
|
83
|
|
|
$k_bitrate = $this->getKiloBitRate(); |
|
84
|
|
|
list($w, $h, $r) = $this->getDimensions(); |
|
85
|
|
|
|
|
86
|
|
|
$reps[] = $this->addRep($k_bitrate, $w, $h); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
foreach ($this->side_values as $key => $height) { |
|
89
|
|
|
$reps[] = $this->addRep($this->k_bitrate_values[$key], Helper::roundToEven($r * $height), $height); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return array_reverse($reps); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $width |
|
97
|
|
|
* @param $k_bitrate |
|
98
|
|
|
* @param $height |
|
99
|
|
|
* @return Representation |
|
100
|
|
|
* @throws InvalidArgumentException |
|
101
|
|
|
*/ |
|
102
|
|
|
private function addRep($k_bitrate, $width, $height): Representation |
|
103
|
|
|
{ |
|
104
|
|
|
return (new Representation())->setKiloBitrate($k_bitrate)->setResize($width, $height); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array|null $k_bitrate_values |
|
109
|
|
|
*/ |
|
110
|
|
|
private function getKiloBitrateValues(?array $k_bitrate_values): void |
|
111
|
|
|
{ |
|
112
|
|
|
$count_sides = count($this->side_values); |
|
113
|
|
|
|
|
114
|
|
|
if ($k_bitrate_values) { |
|
|
|
|
|
|
115
|
|
|
if ($count_sides !== count($k_bitrate_values)) { |
|
116
|
|
|
throw new InvalidArgumentException("The count of side value array must be the same as the count of kilo bitrate array"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->k_bitrate_values = $k_bitrate_values; |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$k_bitrate_value = $this->getKiloBitRate(); |
|
124
|
|
|
$divided_by = 1.5; |
|
125
|
|
|
|
|
126
|
|
|
while ($count_sides) { |
|
127
|
|
|
$this->k_bitrate_values[] = (($k_bitrate = intval($k_bitrate_value / $divided_by)) < 64) ? 64 : $k_bitrate; |
|
128
|
|
|
$divided_by += .5; |
|
129
|
|
|
$count_sides--; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array|null $side_values |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getSideValues(?array $side_values): void |
|
137
|
|
|
{ |
|
138
|
|
|
if ($side_values) { |
|
|
|
|
|
|
139
|
|
|
$this->side_values = $side_values; |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$h = $this->getDimensions()[1]; |
|
144
|
|
|
|
|
145
|
|
|
$this->side_values = array_values(array_filter($this->side_values, function ($height) use ($h) { |
|
146
|
|
|
return $height < $h; |
|
147
|
|
|
})); |
|
148
|
|
|
} |
|
149
|
|
|
} |