|
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
|
|
|
* Extracts the framerate from the given media and formats it in a |
|
19
|
|
|
* suitable format. |
|
20
|
|
|
* |
|
21
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\MediaOpener $media |
|
22
|
|
|
* @return mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
private function getFrameRate(MediaOpener $media) |
|
25
|
|
|
{ |
|
26
|
|
|
$mediaStream = $media->getVideoStream(); |
|
27
|
|
|
|
|
28
|
|
|
$frameRate = trim(Str::before(optional($mediaStream)->get('avg_frame_rate'), "/1")); |
|
29
|
|
|
|
|
30
|
|
|
if (!$frameRate || Str::endsWith($frameRate, '/0')) { |
|
31
|
|
|
return null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $frameRate ? number_format($frameRate, 3, '.', '') : null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return the line from the master playlist that references the given segment playlist. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Media $playlistMedia |
|
41
|
|
|
* @param string $key |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
private function getStreamInfoLine(Media $segmentPlaylistMedia, string $key): string |
|
45
|
|
|
{ |
|
46
|
|
|
$segmentPlaylist = $segmentPlaylistMedia->getDisk()->get( |
|
47
|
|
|
$segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename($key) |
|
48
|
|
|
$segmentPlaylistMedia->getDirectory() . HLSExporter::generateTemporarySegmentPlaylistFilename($key) |
|
|
|
|
|
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$lines = DynamicHLSPlaylist::parseLines($segmentPlaylist)->filter(); |
|
52
|
|
|
|
|
53
|
|
|
return $lines->get($lines->search($segmentPlaylistMedia->getFilename()) - 1); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Loops through all segment playlists and generates a main playlist. It finds |
|
58
|
|
|
* the relative paths to the segment playlists and adds the framerate when |
|
59
|
|
|
* to each playlist. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $segmentPlaylists |
|
62
|
|
|
* @param \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg $driver |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get(array $segmentPlaylists, PHPFFMpeg $driver): string |
|
66
|
|
|
{ |
|
67
|
|
|
return Collection::make($segmentPlaylists)->map(function (Media $segmentPlaylist, $key) use ($driver) { |
|
68
|
|
|
$streamInfoLine = $this->getStreamInfoLine($segmentPlaylist, $key); |
|
69
|
|
|
|
|
70
|
|
|
$media = (new MediaOpener($segmentPlaylist->getDisk(), $driver)) |
|
71
|
|
|
->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']); |
|
72
|
|
|
|
|
73
|
|
|
if ($frameRate = $this->getFrameRate($media)) { |
|
74
|
|
|
$streamInfoLine .= ",FRAME-RATE={$frameRate}"; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return [$streamInfoLine, $segmentPlaylist->getFilename()]; |
|
78
|
|
|
})->collapse() |
|
79
|
|
|
->prepend(static::PLAYLIST_START) |
|
80
|
|
|
->push(static::PLAYLIST_END) |
|
81
|
|
|
->implode(PHP_EOL); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|