Skip to content

Commit d3ddad1

Browse files
hls errors if you pass audio only (#461)
Co-authored-by: pascalbaljet <pascal@pascalbaljet.nl>
1 parent eac7fc2 commit d3ddad1

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/Exporters/HLSExporter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ public function useSegmentFilenameGenerator(Closure $callback): self
109109
private function getSegmentFilenameGenerator(): callable
110110
{
111111
return $this->segmentFilenameGenerator ?: function ($name, $format, $key, $segments, $playlist) {
112-
$segments("{$name}_{$key}_{$format->getKiloBitrate()}_%05d.ts");
113-
$playlist("{$name}_{$key}_{$format->getKiloBitrate()}.m3u8");
112+
$bitrate = $this->driver->getVideoStream()
113+
? $format->getKiloBitrate()
114+
: $format->getAudioKiloBitrate();
115+
116+
$segments("{$name}_{$key}_{$bitrate}_%05d.ts");
117+
$playlist("{$name}_{$key}_{$bitrate}.m3u8");
114118
};
115119
}
116120

src/Exporters/HLSPlaylistGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public function get(array $segmentPlaylists, PHPFFMpeg $driver): string
4949
$media = (new MediaOpener($segmentPlaylist->getDisk(), $driver))
5050
->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']);
5151

52-
if ($frameRate = StreamParser::new($media->getVideoStream())->getFrameRate()) {
53-
$streamInfoLine .= ",FRAME-RATE={$frameRate}";
52+
if ($media->getVideoStream()) {
53+
if ($frameRate = StreamParser::new($media->getVideoStream())->getFrameRate()) {
54+
$streamInfoLine .= ",FRAME-RATE={$frameRate}";
55+
}
5456
}
5557

5658
return [$streamInfoLine, $segmentPlaylist->getFilename()];

tests/HlsExportTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function it_throws_an_exception_when_no_format_has_been_added()
4040
}
4141

4242
/** @test */
43-
public function it_can_export_a_single_media_file_into_a_hls_export()
43+
public function it_can_export_a_single_video_file_into_a_hls_export()
4444
{
4545
$this->fakeLocalVideoFile();
4646

@@ -86,6 +86,41 @@ public function it_can_export_a_single_media_file_into_a_hls_export()
8686
]);
8787
}
8888

89+
/** @test */
90+
public function it_can_export_a_single_audio_file_into_a_hls_export()
91+
{
92+
$this->fakeLocalAudioFile();
93+
94+
$lowBitrate = $this->x264()->setAudioKiloBitrate(250);
95+
$midBitrate = $this->x264()->setAudioKiloBitrate(1000);
96+
$highBitrate = $this->x264()->setAudioKiloBitrate(4000);
97+
98+
(new MediaOpener)
99+
->open('guitar.m4a')
100+
->exportForHLS()
101+
->addFormat($lowBitrate)
102+
->addFormat($midBitrate)
103+
->addFormat($highBitrate)
104+
->toDisk('local')
105+
->save('adaptive.m3u8');
106+
107+
$this->assertTrue(Storage::disk('local')->has('adaptive.m3u8'));
108+
$this->assertTrue(Storage::disk('local')->has('adaptive_0_250.m3u8'));
109+
$this->assertTrue(Storage::disk('local')->has('adaptive_1_1000.m3u8'));
110+
$this->assertTrue(Storage::disk('local')->has('adaptive_2_4000.m3u8'));
111+
112+
$this->assertPlaylistPattern(Storage::disk('local')->get('adaptive.m3u8'), [
113+
'#EXTM3U',
114+
'#EXT-X-STREAM-INF:BANDWIDTH=275000,CODECS="mp4a.40.34"',
115+
'adaptive_0_250.m3u8',
116+
'#EXT-X-STREAM-INF:BANDWIDTH=1100000,CODECS="mp4a.40.34"',
117+
'adaptive_1_1000.m3u8',
118+
'#EXT-X-STREAM-INF:BANDWIDTH=4400000,CODECS="mp4a.40.34"',
119+
'adaptive_2_4000.m3u8',
120+
'#EXT-X-ENDLIST',
121+
]);
122+
}
123+
89124
/** @test */
90125
public function it_can_set_additional_parameters_on_the_format()
91126
{

0 commit comments

Comments
 (0)