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
|
|
|
|
15
|
|
|
class HLSPlaylist |
16
|
|
|
{ |
17
|
|
|
/** @var HLS */ |
18
|
|
|
private $hls; |
19
|
|
|
|
20
|
|
|
private const DEFAULT_AUDIO_BITRATE = 131072; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* HLSPlaylist constructor. |
24
|
|
|
* @param HLS $hls |
25
|
|
|
*/ |
26
|
|
|
public function __construct(HLS $hls) |
27
|
|
|
{ |
28
|
|
|
$this->hls = $hls; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Representation $rep |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
private function segmentPath(Representation $rep): string |
36
|
|
|
{ |
37
|
|
|
return $this->hls->pathInfo(PATHINFO_FILENAME) . "_" . $rep->getHeight() . "p.m3u8"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Representation $rep |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
private function streamInfo(Representation $rep): string |
45
|
|
|
{ |
46
|
|
|
$tag = '#EXT-X-STREAM-INF:'; |
47
|
|
|
$params = array_merge( |
48
|
|
|
[ |
49
|
|
|
"BANDWIDTH" => $rep->getKiloBitrate() * 1024 + $this->getAudioBitrate($rep), |
50
|
|
|
"RESOLUTION" => $rep->size2string(), |
51
|
|
|
"NAME" => "\"" . $rep->getHeight() . "\"" |
52
|
|
|
], |
53
|
|
|
$rep->getHlsStreamInfo() |
54
|
|
|
); |
55
|
|
|
Utiles::concatKeyValue($params, "="); |
56
|
|
|
|
57
|
|
|
return $tag . implode(",", $params); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
private function getVersion(): string |
64
|
|
|
{ |
65
|
|
|
$version = $this->hls->getHlsSegmentType() === "fmp4" ? 7 : 3; |
66
|
|
|
return "#EXT-X-VERSION:" . $version; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $description |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private function contents(array $description): string |
74
|
|
|
{ |
75
|
|
|
$content = array_merge(["#EXTM3U", $this->getVersion()], $description); |
76
|
|
|
|
77
|
|
|
foreach ($this->hls->getRepresentations() as $rep) { |
78
|
|
|
array_push($content, $this->streamInfo($rep), $this->segmentPath($rep)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return implode(PHP_EOL, $content); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $filename |
86
|
|
|
* @param array $description |
87
|
|
|
*/ |
88
|
|
|
public function save(string $filename, array $description): void |
89
|
|
|
{ |
90
|
|
|
File::put($filename, $this->contents(($description))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Representation $rep |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
private function getAudioBitrate(Representation $rep): int |
98
|
|
|
{ |
99
|
|
|
return $rep->getAudioKiloBitrate() ? $rep->getAudioKiloBitrate() * 1024 : $this->getOriginalAudioBitrate(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
private function getOriginalAudioBitrate(): int |
106
|
|
|
{ |
107
|
|
|
return $this->hls |
|
|
|
|
108
|
|
|
->getMedia() |
109
|
|
|
->getStreams() |
|
|
|
|
110
|
|
|
->audios() |
|
|
|
|
111
|
|
|
->first() |
|
|
|
|
112
|
|
|
->get('bit_rate', static::DEFAULT_AUDIO_BITRATE); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |