Skip to content

Commit 52ae20a

Browse files
committed
More Audio HLS fixes + tests
1 parent d3ddad1 commit 52ae20a

25 files changed

+230
-147
lines changed

src/Drivers/InteractsWithFilters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function addFilter(): self
8484
public function addWatermark(callable $withWatermarkFactory): self
8585
{
8686
$withWatermarkFactory(
87-
$watermarkFactory = new WatermarkFactory
87+
$watermarkFactory = new WatermarkFactory()
8888
);
8989

9090
return $this->addFilter($watermarkFactory->get());

src/Drivers/PHPFFMpeg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class PHPFFMpeg
6262
public function __construct(FFMpeg $ffmpeg)
6363
{
6464
$this->ffmpeg = $ffmpeg;
65-
$this->pendingComplexFilters = new Collection;
65+
$this->pendingComplexFilters = new Collection();
6666
}
6767

6868
/**

src/Exporters/EncryptsHLSSegments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private function replaceAbsolutePathsHLSEncryption(Collection $playlistMedia): s
293293
private function cleanupHLSEncryption(): self
294294
{
295295
if ($this->encryptionSecretsRoot) {
296-
(new Filesystem)->deleteDirectory($this->encryptionSecretsRoot);
296+
(new Filesystem())->deleteDirectory($this->encryptionSecretsRoot);
297297
}
298298

299299
return $this;

src/Exporters/HLSExporter.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace ProtoneMedia\LaravelFFMpeg\Exporters;
44

55
use Closure;
6+
use FFMpeg\Format\Audio\DefaultAudio;
7+
use FFMpeg\Format\AudioInterface;
68
use FFMpeg\Format\FormatInterface;
79
use FFMpeg\Format\Video\DefaultVideo;
10+
use FFMpeg\Format\Video\X264;
811
use FFMpeg\Format\VideoInterface;
912
use Illuminate\Support\Collection;
1013
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk;
@@ -15,8 +18,8 @@ class HLSExporter extends MediaExporter
1518
{
1619
use EncryptsHLSSegments;
1720

18-
const HLS_KEY_INFO_FILENAME = 'hls_encryption.keyinfo';
19-
const ENCRYPTION_LISTENER = "listen-encryption-key";
21+
public const HLS_KEY_INFO_FILENAME = 'hls_encryption.keyinfo';
22+
public const ENCRYPTION_LISTENER = "listen-encryption-key";
2023

2124
/**
2225
* @var integer
@@ -85,7 +88,7 @@ public function withPlaylistGenerator(PlaylistGenerator $playlistGenerator): sel
8588

8689
private function getPlaylistGenerator(): PlaylistGenerator
8790
{
88-
return $this->playlistGenerator ?: new HLSPlaylistGenerator;
91+
return $this->playlistGenerator ?: new HLSPlaylistGenerator();
8992
}
9093

9194
/**
@@ -122,11 +125,11 @@ private function getSegmentFilenameGenerator(): callable
122125
* Calls the generator with the path (without extension), format and key.
123126
*
124127
* @param string $baseName
125-
* @param \FFMpeg\Format\VideoInterface $format
128+
* @param \FFMpeg\Format\AudioInterface $format
126129
* @param integer $key
127130
* @return array
128131
*/
129-
private function getSegmentPatternAndFormatPlaylistPath(string $baseName, VideoInterface $format, int $key): array
132+
private function getSegmentPatternAndFormatPlaylistPath(string $baseName, AudioInterface $format, int $key): array
130133
{
131134
$segmentsPattern = null;
132135
$formatPlaylistPath = null;
@@ -150,13 +153,13 @@ function ($path) use (&$formatPlaylistPath) {
150153
/**
151154
* Merges the HLS parameters to the given format.
152155
*
153-
* @param \FFMpeg\Format\Video\DefaultVideo $format
156+
* @param \FFMpeg\Format\Video\DefaultAudio $format
154157
* @param string $segmentsPattern
155158
* @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk $disk
156159
* @param integer $key
157160
* @return array
158161
*/
159-
private function addHLSParametersToFormat(DefaultVideo $format, string $segmentsPattern, Disk $disk, int $key): array
162+
private function addHLSParametersToFormat(DefaultAudio $format, string $segmentsPattern, Disk $disk, int $key): array
160163
{
161164
$format->setAdditionalParameters(array_merge(
162165
$format->getAdditionalParameters() ?: [],
@@ -177,6 +180,7 @@ private function addHLSParametersToFormat(DefaultVideo $format, string $segments
177180
$this->getEncrypedHLSParameters()
178181
));
179182

183+
180184
return $hlsParameters;
181185
}
182186

@@ -251,7 +255,7 @@ private function cleanupSegmentPlaylistGuides(Media $media): self
251255
private function prepareSaving(string $path = null): Collection
252256
{
253257
if (!$this->pendingFormats) {
254-
throw new NoFormatException;
258+
throw new NoFormatException();
255259
}
256260

257261
$media = $this->getDisk()->makeMedia($path);
@@ -267,6 +271,7 @@ private function prepareSaving(string $path = null): Collection
267271
$key
268272
);
269273

274+
270275
$disk = $this->getDisk()->clone();
271276

272277
$this->addHLSParametersToFormat($format, $segmentsPattern, $disk, $key);
@@ -335,9 +340,46 @@ public function save(string $mainPlaylistPath = null): MediaOpener
335340
public function addFormat(FormatInterface $format, callable $filtersCallback = null): self
336341
{
337342
if (!$this->pendingFormats) {
338-
$this->pendingFormats = new Collection;
343+
$this->pendingFormats = new Collection();
339344
}
340345

346+
if (!$format instanceof DefaultVideo && $format instanceof DefaultAudio) {
347+
$originalFormat = clone $format;
348+
349+
$format = new class () extends DefaultVideo {
350+
private array $audioCodecs = [];
351+
352+
public function setAvailableAudioCodecs(array $audioCodecs)
353+
{
354+
$this->audioCodecs = $audioCodecs;
355+
}
356+
357+
public function getAvailableAudioCodecs(): array
358+
{
359+
return $this->audioCodecs;
360+
}
361+
362+
public function supportBFrames()
363+
{
364+
return false;
365+
}
366+
367+
public function getAvailableVideoCodecs()
368+
{
369+
return [];
370+
}
371+
};
372+
373+
$format->setAvailableAudioCodecs($originalFormat->getAvailableAudioCodecs());
374+
$format->setAudioCodec($originalFormat->getAudioCodec());
375+
$format->setAudioKiloBitrate($originalFormat->getAudioKiloBitrate());
376+
377+
if ($originalFormat->getAudioChannels()) {
378+
$format->setAudioChannels($originalFormat->getAudioChannels());
379+
}
380+
}
381+
382+
341383
$this->pendingFormats->push([$format, $filtersCallback]);
342384

343385
return $this;

src/Exporters/HLSPlaylistGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
class HLSPlaylistGenerator implements PlaylistGenerator
1313
{
14-
const PLAYLIST_START = '#EXTM3U';
15-
const PLAYLIST_END = '#EXT-X-ENDLIST';
14+
public const PLAYLIST_START = '#EXTM3U';
15+
public const PLAYLIST_END = '#EXT-X-ENDLIST';
1616

1717
/**
1818
* Return the line from the master playlist that references the given segment playlist.

src/Exporters/HLSVideoFilters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class HLSVideoFilters
1313
{
14-
const MAPPING_GLUE = "_hls_";
14+
public const MAPPING_GLUE = "_hls_";
1515

1616
/**
1717
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
@@ -106,7 +106,7 @@ public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT, $f
106106
*/
107107
public function addWatermark(callable $withWatermarkFactory): self
108108
{
109-
$withWatermarkFactory($watermarkFactory = new WatermarkFactory);
109+
$withWatermarkFactory($watermarkFactory = new WatermarkFactory());
110110

111111
return $this->addLegacyFilter($watermarkFactory->get());
112112
}

src/Exporters/MediaExporter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
*/
2121
class MediaExporter
2222
{
23-
use ForwardsCalls,
24-
HandlesAdvancedMedia,
25-
HandlesConcatenation,
26-
HandlesFrames,
27-
HandlesTimelapse,
28-
HasProgressListener;
23+
use ForwardsCalls;
24+
use HandlesAdvancedMedia;
25+
use HandlesConcatenation;
26+
use HandlesFrames;
27+
use HandlesTimelapse;
28+
use HasProgressListener;
2929

3030
/**
3131
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
@@ -59,7 +59,7 @@ public function __construct(PHPFFMpeg $driver)
5959
{
6060
$this->driver = $driver;
6161

62-
$this->maps = new Collection;
62+
$this->maps = new Collection();
6363
}
6464

6565
protected function getDisk(): Disk
@@ -107,7 +107,7 @@ public function withVisibility(string $visibility)
107107
public function addTileFilter(callable $withTileFactory): self
108108
{
109109
$withTileFactory(
110-
$tileFactory = new TileFactory
110+
$tileFactory = new TileFactory()
111111
);
112112

113113
$this->addFilter($filter = $tileFactory->get());
@@ -138,7 +138,7 @@ public function getCommand(string $path = null)
138138
$media = $this->prepareSaving($path);
139139

140140
return $this->driver->getFinalCommand(
141-
$this->format ?: new NullFormat,
141+
$this->format ?: new NullFormat(),
142142
optional($media)->getLocalPath() ?: '/dev/null'
143143
);
144144
}
@@ -229,7 +229,7 @@ public function save(string $path = null)
229229
}
230230
} else {
231231
$this->driver->save(
232-
$this->format ?: new NullFormat,
232+
$this->format ?: new NullFormat(),
233233
optional($outputMedia)->getLocalPath() ?: '/dev/null'
234234
);
235235
}
@@ -253,7 +253,7 @@ public function save(string $path = null)
253253

254254
public function getProcessOutput(): ProcessOutput
255255
{
256-
return tap(new StdListener, function (StdListener $listener) {
256+
return tap(new StdListener(), function (StdListener $listener) {
257257
$this->addListener($listener)->save();
258258
$listener->removeAllListeners();
259259
$this->removeListener($listener);

src/FFMpeg/StdListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class StdListener extends EventEmitter implements ListenerInterface
1111
{
12-
const TYPE_ALL = 'all';
12+
public const TYPE_ALL = 'all';
1313

1414
/**
1515
* Name of the emitted event.

src/Filesystem/MediaOnNetwork.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function toMedia(callable $withCurl = null): Media
7777
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
7878

7979
if (!empty($this->headers)) {
80-
$headers = Collection::make($this->headers)->map(function($value, $header) {
80+
$headers = Collection::make($this->headers)->map(function ($value, $header) {
8181
return "{$header}: {$value}";
8282
})->all();
8383

src/Filesystem/TemporaryDirectories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function create(): string
4949
*/
5050
public function deleteAll(): void
5151
{
52-
$filesystem = new Filesystem;
52+
$filesystem = new Filesystem();
5353

5454
foreach ($this->directories as $directory) {
5555
$filesystem->deleteDirectory($directory);

0 commit comments

Comments
 (0)