Skip to content

Commit

Permalink
More Audio HLS fixes + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Jan 12, 2023
1 parent d3ddad1 commit 52ae20a
Show file tree
Hide file tree
Showing 25 changed files with 230 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/InteractsWithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function addFilter(): self
public function addWatermark(callable $withWatermarkFactory): self
{
$withWatermarkFactory(
$watermarkFactory = new WatermarkFactory
$watermarkFactory = new WatermarkFactory()
);

return $this->addFilter($watermarkFactory->get());
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/PHPFFMpeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PHPFFMpeg
public function __construct(FFMpeg $ffmpeg)
{
$this->ffmpeg = $ffmpeg;
$this->pendingComplexFilters = new Collection;
$this->pendingComplexFilters = new Collection();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exporters/EncryptsHLSSegments.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private function replaceAbsolutePathsHLSEncryption(Collection $playlistMedia): s
private function cleanupHLSEncryption(): self
{
if ($this->encryptionSecretsRoot) {
(new Filesystem)->deleteDirectory($this->encryptionSecretsRoot);
(new Filesystem())->deleteDirectory($this->encryptionSecretsRoot);
}

return $this;
Expand Down
60 changes: 51 additions & 9 deletions src/Exporters/HLSExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace ProtoneMedia\LaravelFFMpeg\Exporters;

use Closure;
use FFMpeg\Format\Audio\DefaultAudio;
use FFMpeg\Format\AudioInterface;
use FFMpeg\Format\FormatInterface;
use FFMpeg\Format\Video\DefaultVideo;
use FFMpeg\Format\Video\X264;
use FFMpeg\Format\VideoInterface;
use Illuminate\Support\Collection;
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk;
Expand All @@ -15,8 +18,8 @@ class HLSExporter extends MediaExporter
{
use EncryptsHLSSegments;

const HLS_KEY_INFO_FILENAME = 'hls_encryption.keyinfo';
const ENCRYPTION_LISTENER = "listen-encryption-key";
public const HLS_KEY_INFO_FILENAME = 'hls_encryption.keyinfo';
public const ENCRYPTION_LISTENER = "listen-encryption-key";

/**
* @var integer
Expand Down Expand Up @@ -85,7 +88,7 @@ public function withPlaylistGenerator(PlaylistGenerator $playlistGenerator): sel

private function getPlaylistGenerator(): PlaylistGenerator
{
return $this->playlistGenerator ?: new HLSPlaylistGenerator;
return $this->playlistGenerator ?: new HLSPlaylistGenerator();
}

/**
Expand Down Expand Up @@ -122,11 +125,11 @@ private function getSegmentFilenameGenerator(): callable
* Calls the generator with the path (without extension), format and key.
*
* @param string $baseName
* @param \FFMpeg\Format\VideoInterface $format
* @param \FFMpeg\Format\AudioInterface $format
* @param integer $key
* @return array
*/
private function getSegmentPatternAndFormatPlaylistPath(string $baseName, VideoInterface $format, int $key): array
private function getSegmentPatternAndFormatPlaylistPath(string $baseName, AudioInterface $format, int $key): array
{
$segmentsPattern = null;
$formatPlaylistPath = null;
Expand All @@ -150,13 +153,13 @@ function ($path) use (&$formatPlaylistPath) {
/**
* Merges the HLS parameters to the given format.
*
* @param \FFMpeg\Format\Video\DefaultVideo $format
* @param \FFMpeg\Format\Video\DefaultAudio $format
* @param string $segmentsPattern
* @param \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk $disk
* @param integer $key
* @return array
*/
private function addHLSParametersToFormat(DefaultVideo $format, string $segmentsPattern, Disk $disk, int $key): array
private function addHLSParametersToFormat(DefaultAudio $format, string $segmentsPattern, Disk $disk, int $key): array
{
$format->setAdditionalParameters(array_merge(
$format->getAdditionalParameters() ?: [],
Expand All @@ -177,6 +180,7 @@ private function addHLSParametersToFormat(DefaultVideo $format, string $segments
$this->getEncrypedHLSParameters()
));


return $hlsParameters;
}

Expand Down Expand Up @@ -251,7 +255,7 @@ private function cleanupSegmentPlaylistGuides(Media $media): self
private function prepareSaving(string $path = null): Collection
{
if (!$this->pendingFormats) {
throw new NoFormatException;
throw new NoFormatException();
}

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


$disk = $this->getDisk()->clone();

$this->addHLSParametersToFormat($format, $segmentsPattern, $disk, $key);
Expand Down Expand Up @@ -335,9 +340,46 @@ public function save(string $mainPlaylistPath = null): MediaOpener
public function addFormat(FormatInterface $format, callable $filtersCallback = null): self
{
if (!$this->pendingFormats) {
$this->pendingFormats = new Collection;
$this->pendingFormats = new Collection();
}

if (!$format instanceof DefaultVideo && $format instanceof DefaultAudio) {
$originalFormat = clone $format;

$format = new class () extends DefaultVideo {
private array $audioCodecs = [];

public function setAvailableAudioCodecs(array $audioCodecs)
{
$this->audioCodecs = $audioCodecs;
}

public function getAvailableAudioCodecs(): array
{
return $this->audioCodecs;
}

public function supportBFrames()
{
return false;
}

public function getAvailableVideoCodecs()
{
return [];
}
};

$format->setAvailableAudioCodecs($originalFormat->getAvailableAudioCodecs());
$format->setAudioCodec($originalFormat->getAudioCodec());
$format->setAudioKiloBitrate($originalFormat->getAudioKiloBitrate());

if ($originalFormat->getAudioChannels()) {
$format->setAudioChannels($originalFormat->getAudioChannels());
}
}


$this->pendingFormats->push([$format, $filtersCallback]);

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Exporters/HLSPlaylistGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

class HLSPlaylistGenerator implements PlaylistGenerator
{
const PLAYLIST_START = '#EXTM3U';
const PLAYLIST_END = '#EXT-X-ENDLIST';
public const PLAYLIST_START = '#EXTM3U';
public const PLAYLIST_END = '#EXT-X-ENDLIST';

/**
* Return the line from the master playlist that references the given segment playlist.
Expand Down
4 changes: 2 additions & 2 deletions src/Exporters/HLSVideoFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HLSVideoFilters
{
const MAPPING_GLUE = "_hls_";
public const MAPPING_GLUE = "_hls_";

/**
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
Expand Down Expand Up @@ -106,7 +106,7 @@ public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT, $f
*/
public function addWatermark(callable $withWatermarkFactory): self
{
$withWatermarkFactory($watermarkFactory = new WatermarkFactory);
$withWatermarkFactory($watermarkFactory = new WatermarkFactory());

return $this->addLegacyFilter($watermarkFactory->get());
}
Expand Down
22 changes: 11 additions & 11 deletions src/Exporters/MediaExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
*/
class MediaExporter
{
use ForwardsCalls,
HandlesAdvancedMedia,
HandlesConcatenation,
HandlesFrames,
HandlesTimelapse,
HasProgressListener;
use ForwardsCalls;
use HandlesAdvancedMedia;
use HandlesConcatenation;
use HandlesFrames;
use HandlesTimelapse;
use HasProgressListener;

/**
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
Expand Down Expand Up @@ -59,7 +59,7 @@ public function __construct(PHPFFMpeg $driver)
{
$this->driver = $driver;

$this->maps = new Collection;
$this->maps = new Collection();
}

protected function getDisk(): Disk
Expand Down Expand Up @@ -107,7 +107,7 @@ public function withVisibility(string $visibility)
public function addTileFilter(callable $withTileFactory): self
{
$withTileFactory(
$tileFactory = new TileFactory
$tileFactory = new TileFactory()
);

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

return $this->driver->getFinalCommand(
$this->format ?: new NullFormat,
$this->format ?: new NullFormat(),
optional($media)->getLocalPath() ?: '/dev/null'
);
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public function save(string $path = null)
}
} else {
$this->driver->save(
$this->format ?: new NullFormat,
$this->format ?: new NullFormat(),
optional($outputMedia)->getLocalPath() ?: '/dev/null'
);
}
Expand All @@ -253,7 +253,7 @@ public function save(string $path = null)

public function getProcessOutput(): ProcessOutput
{
return tap(new StdListener, function (StdListener $listener) {
return tap(new StdListener(), function (StdListener $listener) {
$this->addListener($listener)->save();
$listener->removeAllListeners();
$this->removeListener($listener);
Expand Down
2 changes: 1 addition & 1 deletion src/FFMpeg/StdListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class StdListener extends EventEmitter implements ListenerInterface
{
const TYPE_ALL = 'all';
public const TYPE_ALL = 'all';

/**
* Name of the emitted event.
Expand Down
2 changes: 1 addition & 1 deletion src/Filesystem/MediaOnNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function toMedia(callable $withCurl = null): Media
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

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

Expand Down
2 changes: 1 addition & 1 deletion src/Filesystem/TemporaryDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function create(): string
*/
public function deleteAll(): void
{
$filesystem = new Filesystem;
$filesystem = new Filesystem();

foreach ($this->directories as $directory) {
$filesystem->deleteDirectory($directory);
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/TileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TileFactory

public static function make(): TileFactory
{
return new static;
return new static();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Filters/WatermarkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class WatermarkFactory
use ForwardsCalls;

/** position constants */
const LEFT = 'left';
const RIGHT = 'right';
const CENTER = 'center';
const TOP = 'top';
const BOTTOM = 'bottom';
public const LEFT = 'left';
public const RIGHT = 'right';
public const CENTER = 'center';
public const TOP = 'top';
public const BOTTOM = 'bottom';

/**
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
Expand Down
4 changes: 2 additions & 2 deletions src/MediaOpener.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollect

$this->driver = ($driver ?: app(PHPFFMpeg::class))->fresh();

$this->collection = $mediaCollection ?: new MediaCollection;
$this->collection = $mediaCollection ?: new MediaCollection();
}

public function clone(): self
Expand Down Expand Up @@ -214,7 +214,7 @@ public function exportTile(callable $withTileFactory): MediaExporter
{
return $this->export()
->addTileFilter($withTileFactory)
->inFormat(new ImageFormat);
->inFormat(new ImageFormat());
}

public function exportFramesByAmount(int $amount, int $width = null, int $height = null, int $quality = null): MediaExporter
Expand Down
Loading

0 comments on commit 52ae20a

Please sign in to comment.