Skip to content

Commit

Permalink
hls errors if you pass audio only (#461)
Browse files Browse the repository at this point in the history
Co-authored-by: pascalbaljet <[email protected]>
  • Loading branch information
aronquiray and pascalbaljet authored Jan 12, 2023
1 parent eac7fc2 commit d3ddad1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Exporters/HLSExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ public function useSegmentFilenameGenerator(Closure $callback): self
private function getSegmentFilenameGenerator(): callable
{
return $this->segmentFilenameGenerator ?: function ($name, $format, $key, $segments, $playlist) {
$segments("{$name}_{$key}_{$format->getKiloBitrate()}_%05d.ts");
$playlist("{$name}_{$key}_{$format->getKiloBitrate()}.m3u8");
$bitrate = $this->driver->getVideoStream()
? $format->getKiloBitrate()
: $format->getAudioKiloBitrate();

$segments("{$name}_{$key}_{$bitrate}_%05d.ts");
$playlist("{$name}_{$key}_{$bitrate}.m3u8");
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/Exporters/HLSPlaylistGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public function get(array $segmentPlaylists, PHPFFMpeg $driver): string
$media = (new MediaOpener($segmentPlaylist->getDisk(), $driver))
->openWithInputOptions($segmentPlaylist->getPath(), ['-allowed_extensions', 'ALL']);

if ($frameRate = StreamParser::new($media->getVideoStream())->getFrameRate()) {
$streamInfoLine .= ",FRAME-RATE={$frameRate}";
if ($media->getVideoStream()) {
if ($frameRate = StreamParser::new($media->getVideoStream())->getFrameRate()) {
$streamInfoLine .= ",FRAME-RATE={$frameRate}";
}
}

return [$streamInfoLine, $segmentPlaylist->getFilename()];
Expand Down
37 changes: 36 additions & 1 deletion tests/HlsExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function it_throws_an_exception_when_no_format_has_been_added()
}

/** @test */
public function it_can_export_a_single_media_file_into_a_hls_export()
public function it_can_export_a_single_video_file_into_a_hls_export()
{
$this->fakeLocalVideoFile();

Expand Down Expand Up @@ -86,6 +86,41 @@ public function it_can_export_a_single_media_file_into_a_hls_export()
]);
}

/** @test */
public function it_can_export_a_single_audio_file_into_a_hls_export()
{
$this->fakeLocalAudioFile();

$lowBitrate = $this->x264()->setAudioKiloBitrate(250);
$midBitrate = $this->x264()->setAudioKiloBitrate(1000);
$highBitrate = $this->x264()->setAudioKiloBitrate(4000);

(new MediaOpener)
->open('guitar.m4a')
->exportForHLS()
->addFormat($lowBitrate)
->addFormat($midBitrate)
->addFormat($highBitrate)
->toDisk('local')
->save('adaptive.m3u8');

$this->assertTrue(Storage::disk('local')->has('adaptive.m3u8'));
$this->assertTrue(Storage::disk('local')->has('adaptive_0_250.m3u8'));
$this->assertTrue(Storage::disk('local')->has('adaptive_1_1000.m3u8'));
$this->assertTrue(Storage::disk('local')->has('adaptive_2_4000.m3u8'));

$this->assertPlaylistPattern(Storage::disk('local')->get('adaptive.m3u8'), [
'#EXTM3U',
'#EXT-X-STREAM-INF:BANDWIDTH=275000,CODECS="mp4a.40.34"',
'adaptive_0_250.m3u8',
'#EXT-X-STREAM-INF:BANDWIDTH=1100000,CODECS="mp4a.40.34"',
'adaptive_1_1000.m3u8',
'#EXT-X-STREAM-INF:BANDWIDTH=4400000,CODECS="mp4a.40.34"',
'adaptive_2_4000.m3u8',
'#EXT-X-ENDLIST',
]);
}

/** @test */
public function it_can_set_additional_parameters_on_the_format()
{
Expand Down

0 comments on commit d3ddad1

Please sign in to comment.