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
|
|
|
use Streaming\Exception\RuntimeException; |
16
|
|
|
|
17
|
|
|
class HLSPlaylist |
18
|
|
|
{ |
19
|
|
|
/** @var HLS */ |
20
|
|
|
private $hls; |
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->getPathInfo(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
|
|
|
$ext_stream = '#EXT-X-STREAM-INF:'; |
47
|
|
|
$params = [ |
48
|
|
|
"BANDWIDTH=" . $rep->getKiloBitrate() * 1024, |
49
|
|
|
"RESOLUTION=" . $rep->getResize(), |
50
|
|
|
"NAME=\"" . $rep->getHeight() . "\"" |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
return $ext_stream . implode(",", array_merge($params, $rep->getHlsStreamInfo())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
private function getVersion(): string |
60
|
|
|
{ |
61
|
|
|
$version = $this->hls->getHlsSegmentType() === "fmp4" ? 7 : 3; |
62
|
|
|
return "#EXT-X-VERSION:" . $version; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $description |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function contents(array $description): string |
70
|
|
|
{ |
71
|
|
|
$content = array_merge(["#EXTM3U", $this->getVersion()], $description); |
72
|
|
|
|
73
|
|
|
foreach ($this->hls->getRepresentations() as $rep) { |
74
|
|
|
array_push($content, $this->streamInfo($rep), $this->segmentPath($rep)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return implode(PHP_EOL, $content); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $filename |
82
|
|
|
* @param array $description |
83
|
|
|
*/ |
84
|
|
|
public function save(string $filename, array $description): void |
85
|
|
|
{ |
86
|
|
|
if (false === @file_put_contents($filename, $this->contents($description))) { |
87
|
|
|
throw new RuntimeException("Unable to save the master playlist file"); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |