|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pbmedia\LaravelFFMpeg; |
| 4 | + |
| 5 | +use FFMpeg\Format\VideoInterface; |
| 6 | +use Pbmedia\LaravelFFMpeg\SegmentedExporter; |
| 7 | + |
| 8 | +class HLSPlaylistExporter extends MediaExporter |
| 9 | +{ |
| 10 | + protected $formats = []; |
| 11 | + |
| 12 | + protected $segmentLength = 10; |
| 13 | + |
| 14 | + protected $segmentedExporters; |
| 15 | + |
| 16 | + protected $saveMethod = 'savePlaylist'; |
| 17 | + |
| 18 | + public function addFormat(VideoInterface $format): MediaExporter |
| 19 | + { |
| 20 | + $this->formats[] = $format; |
| 21 | + |
| 22 | + return $this; |
| 23 | + } |
| 24 | + |
| 25 | + public function getFormatsSorted(): array |
| 26 | + { |
| 27 | + usort($this->formats, function ($formatA, $formatB) { |
| 28 | + return $formatA->getKiloBitrate() <=> $formatB->getKiloBitrate(); |
| 29 | + }); |
| 30 | + |
| 31 | + return $this->formats; |
| 32 | + } |
| 33 | + |
| 34 | + public function setPlaylistPath(string $playlistPath): MediaExporter |
| 35 | + { |
| 36 | + $this->playlistPath = $playlistPath; |
| 37 | + |
| 38 | + return $this; |
| 39 | + } |
| 40 | + |
| 41 | + public function setSegmentLength(int $segmentLength): MediaExporter |
| 42 | + { |
| 43 | + $this->segmentLength = $segmentLength; |
| 44 | + |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + protected function getSegmentedExporterFromFormat(VideoInterface $format): SegmentedExporter |
| 49 | + { |
| 50 | + $media = clone $this->media; |
| 51 | + |
| 52 | + return (new SegmentedExporter($media)) |
| 53 | + ->inFormat($format) |
| 54 | + ->setPlaylistPath($this->playlistPath) |
| 55 | + ->setSegmentLength($this->segmentLength); |
| 56 | + } |
| 57 | + |
| 58 | + public function getSegmentedExporters(): array |
| 59 | + { |
| 60 | + if ($this->segmentedExporters) { |
| 61 | + return $this->segmentedExporters; |
| 62 | + } |
| 63 | + |
| 64 | + return $this->segmentedExporters = array_map(function ($format) { |
| 65 | + return $this->getSegmentedExporterFromFormat($format); |
| 66 | + }, $this->getFormatsSorted()); |
| 67 | + } |
| 68 | + |
| 69 | + protected function exportStreams() |
| 70 | + { |
| 71 | + foreach ($this->getSegmentedExporters() as $segmentedExporter) { |
| 72 | + $segmentedExporter->saveStream($this->playlistPath); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + protected function getMasterPlaylistContents(): string |
| 77 | + { |
| 78 | + $lines = ['#EXTM3U']; |
| 79 | + |
| 80 | + foreach ($this->getSegmentedExporters() as $segmentedExporter) { |
| 81 | + $bitrate = $segmentedExporter->getFormat()->getKiloBitrate() * 1000; |
| 82 | + |
| 83 | + $lines[] = '#EXT-X-STREAM-INF:BANDWIDTH=' . $bitrate; |
| 84 | + $lines[] = $segmentedExporter->getPlaylistFilename(); |
| 85 | + } |
| 86 | + |
| 87 | + return implode(PHP_EOL, $lines); |
| 88 | + } |
| 89 | + |
| 90 | + public function savePlaylist(string $playlistPath): MediaExporter |
| 91 | + { |
| 92 | + $this->setPlaylistPath($playlistPath); |
| 93 | + $this->exportStreams(); |
| 94 | + |
| 95 | + file_put_contents( |
| 96 | + $playlistPath, |
| 97 | + $this->getMasterPlaylistContents() |
| 98 | + ); |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | +} |
0 commit comments