1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg\Exporters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
8
|
|
|
use Pbmedia\LaravelFFMpeg\Filesystem\Media; |
9
|
|
|
use Pbmedia\LaravelFFMpeg\MediaOpener; |
10
|
|
|
|
11
|
|
|
class HLSPlaylistGenerator implements PlaylistGenerator |
12
|
|
|
{ |
13
|
|
|
const PLAYLIST_START = '#EXTM3U'; |
14
|
|
|
const PLAYLIST_END = '#EXT-X-ENDLIST'; |
15
|
|
|
|
16
|
|
|
private function getPathOfFirstSegment(Media $playlistMedia): string |
17
|
|
|
{ |
18
|
|
|
$playlistContent = file_get_contents($playlistMedia->getLocalPath()); |
19
|
|
|
|
20
|
|
|
return Collection::make(explode(PHP_EOL, $playlistContent))->first(function ($line) { |
21
|
|
|
return !Str::startsWith($line, '#') && Str::endsWith($line, '.ts'); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function getBandwidth(MediaOpener $media) |
26
|
|
|
{ |
27
|
|
|
return $media->getFormat()->get('bit_rate'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function getResolution(MediaOpener $media) |
31
|
|
|
{ |
32
|
|
|
$mediaStream = $media->getStreams()[0]; |
|
|
|
|
33
|
|
|
|
34
|
|
|
return "{$mediaStream->get('width')}x{$mediaStream->get('height')}"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getFrameRate(MediaOpener $media) |
38
|
|
|
{ |
39
|
|
|
$mediaStream = $media->getStreams()[0]; |
|
|
|
|
40
|
|
|
$frameRate = trim(Str::before($mediaStream->get('avg_frame_rate'), "/1")); |
41
|
|
|
|
42
|
|
|
return $frameRate ? number_format($frameRate, 3, '.', '') : null; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function get(array $playlistMedia, PHPFFMpeg $driver): string |
46
|
|
|
{ |
47
|
|
|
return Collection::make($playlistMedia)->map(function (Media $playlistMedia) use ($driver) { |
48
|
|
|
$media = (new MediaOpener($playlistMedia->getDisk(), $driver->fresh()))->open( |
49
|
|
|
$this->getPathOfFirstSegment($playlistMedia) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$streamInfo = [ |
53
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH={$this->getBandwidth($media)}", |
54
|
|
|
"RESOLUTION={$this->getResolution($media)}", |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
if ($frameRate = $this->getFrameRate($media)) { |
58
|
|
|
$streamInfo[] = "FRAME-RATE={$frameRate}"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [implode(',', $streamInfo), $playlistMedia->getPath()]; |
62
|
|
|
})->collapse() |
63
|
|
|
->prepend(static::PLAYLIST_START) |
64
|
|
|
->push(static::PLAYLIST_END) |
65
|
|
|
->implode(PHP_EOL); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.