1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Exporters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
8
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media; |
9
|
|
|
use ProtoneMedia\LaravelFFMpeg\Http\DynamicHLSPlaylist; |
10
|
|
|
use ProtoneMedia\LaravelFFMpeg\MediaOpener; |
11
|
|
|
|
12
|
|
|
class HLSPlaylistGenerator implements PlaylistGenerator |
13
|
|
|
{ |
14
|
|
|
const PLAYLIST_START = '#EXTM3U'; |
15
|
|
|
const PLAYLIST_END = '#EXT-X-ENDLIST'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $uuid; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Setter for the UUID. |
24
|
|
|
* |
25
|
|
|
* @param string $uuid |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
|
|
public function setUuid(string $uuid): self |
29
|
|
|
{ |
30
|
|
|
$this->uuid = $uuid; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Extracts the framerate from the given media and formats it in a |
37
|
|
|
* suitable format. |
38
|
|
|
* |
39
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\MediaOpener $media |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
private function getFrameRate(MediaOpener $media) |
43
|
|
|
{ |
44
|
|
|
$mediaStream = $media->getVideoStream(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1")); |
47
|
|
|
|
48
|
|
|
if (!$frameRate || Str::endsWith($frameRate, '/0')) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (Str::contains($frameRate, '/')) { |
53
|
|
|
[$numerator, $denominator] = explode('/', $frameRate); |
54
|
|
|
|
55
|
|
|
$frameRate = $numerator / $denominator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $frameRate ? number_format($frameRate, 3, '.', '') : null; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return the line from the master playlist that references the given segment playlist. |
63
|
|
|
* |
64
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Media $playlistMedia |
65
|
|
|
* @param string $key |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
private function getStreamInfoLine(Media $segmentPlaylistMedia, string $key): string |
69
|
|
|
{ |
70
|
|
|
$segmentPlaylist = $segmentPlaylistMedia->getDisk()->get( |
|
|
|
|
71
|
|
|
$segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename($key, $this->uuid) |
|
|
|
|
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$lines = DynamicHLSPlaylist::parseLines($segmentPlaylist)->filter(); |
75
|
|
|
|
76
|
|
|
return $lines->get($lines->search($segmentPlaylistMedia->getFilename()) - 1); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Loops through all segment playlists and generates a main playlist. It finds |
81
|
|
|
* the relative paths to the segment playlists and adds the framerate when |
82
|
|
|
* to each playlist. |
83
|
|
|
* |
84
|
|
|
* @param array $segmentPlaylists |
85
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg $driver |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function get(array $segmentPlaylists, PHPFFMpeg $driver): string |
89
|
|
|
{ |
90
|
|
|
return Collection::make($segmentPlaylists)->map(function (Media $segmentPlaylist, $key) use ($driver) { |
91
|
|
|
$streamInfoLine = $this->getStreamInfoLine($segmentPlaylist, $key); |
92
|
|
|
|
93
|
|
|
$media = (new MediaOpener($segmentPlaylist->getDisk(), $driver)) |
94
|
|
|
->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']); |
95
|
|
|
|
96
|
|
|
if ($frameRate = $this->getFrameRate($media)) { |
97
|
|
|
$streamInfoLine .= ",FRAME-RATE={$frameRate}"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return [$streamInfoLine, $segmentPlaylist->getFilename()]; |
101
|
|
|
})->collapse() |
102
|
|
|
->prepend(static::PLAYLIST_START) |
103
|
|
|
->push(static::PLAYLIST_END) |
104
|
|
|
->implode(PHP_EOL); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.