Skip to content

fix: 🎨 getFormat not found #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ramsey/collection": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.21",
"league/flysystem-memory": "^3.10",
"mockery/mockery": "^1.4.4",
"nesbot/carbon": "^2.66|^3.0",
Expand Down
20 changes: 5 additions & 15 deletions src/Drivers/InteractsWithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ trait InteractsWithFilters

/**
* Returns an array with the filters applied to the underlying media object.
*
* @return array
*/
public function getFilters(): array
{
Expand All @@ -33,8 +31,6 @@ public function getFilters(): array
/**
* Helper method to provide multiple ways to add a filter to the underlying
* media object.
*
* @return self
*/
public function addFilter(): self
{
Expand Down Expand Up @@ -77,14 +73,11 @@ public function addFilter(): self
/**
* Calls the callable with a WatermarkFactory instance and
* adds the freshly generated WatermarkFilter.
*
* @param callable $withWatermarkFactory
* @return self
*/
public function addWatermark(callable $withWatermarkFactory): self
{
$withWatermarkFactory(
$watermarkFactory = new WatermarkFactory()
$watermarkFactory = new WatermarkFactory
);

return $this->addFilter($watermarkFactory->get());
Expand All @@ -93,11 +86,10 @@ public function addWatermark(callable $withWatermarkFactory): self
/**
* Shortcut for adding a Resize filter.
*
* @param int $width
* @param int $height
* @param string $mode
* @param boolean $forceStandards
* @return self
* @param int $width
* @param int $height
* @param string $mode
* @param bool $forceStandards
*/
public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT, $forceStandards = true): self
{
Expand Down Expand Up @@ -126,8 +118,6 @@ public function addFilterAsComplexFilter($in, $out, ...$arguments): self

/**
* Getter for the pending complex filters.
*
* @return \Illuminate\Support\Collection
*/
public function getPendingComplexFilters(): Collection
{
Expand Down
65 changes: 61 additions & 4 deletions src/Drivers/InteractsWithMediaStreams.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ trait InteractsWithMediaStreams
{
/**
* Returns an array with all streams.
*
* @return array
*/
public function getStreams(): array
{
if (!$this->isAdvancedMedia()) {
if (! $this->isAdvancedMedia()) {
return iterator_to_array($this->media->getStreams());
}

Expand All @@ -39,7 +37,17 @@ public function getDurationInMiliseconds(): int
$format = $this->getFormat();

if ($format->has('duration')) {
return intval(round($format->get('duration') * 1000));
$duration = $format->get('duration');

if (! blank($duration)) {
return $format->get('duration') * 1000;
}
}

$duration = $this->extractDurationFromStream($this->getVideoStream() ?? $this->getAudioStream());

if ($duration !== null) {
return $duration;
}

throw new UnknownDurationException('Could not determine the duration of the media.');
Expand Down Expand Up @@ -69,4 +77,53 @@ public function getVideoStream(): ?Stream
return $stream->isVideo();
});
}

/**
* Extract video duration when it's not a standard property.
*/
public function extractDurationFromStream(Stream $stream): ?int
{
$duration = $this->findDuration($stream->all());

if ($duration === null) {
return null;
}

return $this->formatDuration($duration) * 1000;
}

/**
* Recursively search for the duration key.
*/
public function findDuration(array $array): ?string
{
foreach ($array as $key => $value) {
if (is_array($value)) {
if (! is_null($duration = $this->findDuration($value))) {
return $duration;
}
}

if (strtolower($key) === 'duration') {
return (string) $value;
}
}

return null;
}

/**
* Convert duration string to seconds.
*/
public function formatDuration(string $duration): float
{
$parts = array_map('floatval', explode(':', $duration));
$count = count($parts);

return match ($count) {
2 => $parts[0] * 60 + $parts[1],
3 => $parts[0] * 3600 + $parts[1] * 60 + $parts[2],
default => 0.0,
};
}
}
27 changes: 5 additions & 22 deletions src/Drivers/PHPFFMpeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PHPFFMpeg
private $mediaCollection;

/**
* @var boolean
* @var bool
*/
private $forceAdvanced = false;

Expand All @@ -61,8 +61,8 @@ class PHPFFMpeg

public function __construct(FFMpeg $ffmpeg)
{
$this->ffmpeg = $ffmpeg;
$this->pendingComplexFilters = new Collection();
$this->ffmpeg = $ffmpeg;
$this->pendingComplexFilters = new Collection;
}

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ public function open(MediaCollection $mediaCollection): self

$this->mediaCollection = $mediaCollection;

if ($mediaCollection->count() === 1 && !$this->forceAdvanced) {
if ($mediaCollection->count() === 1 && ! $this->forceAdvanced) {
$media = Arr::first($mediaCollection->collection());

$this->ffmpeg->setFFProbe(
Expand Down Expand Up @@ -147,7 +147,7 @@ public function open(MediaCollection $mediaCollection): self

public function frame(TimeCode $timecode)
{
if (!$this->isVideo()) {
if (! $this->isVideo()) {
throw new Exception('Opened media is not a video file.');
}

Expand Down Expand Up @@ -178,8 +178,6 @@ public function openAdvanced(MediaCollection $mediaCollection): self

/**
* Returns the FFMpegDriver of the underlying library.
*
* @return \FFMpeg\Driver\FFMpegDriver
*/
private function getFFMpegDriver(): FFMpegDriver
{
Expand All @@ -188,9 +186,6 @@ private function getFFMpegDriver(): FFMpegDriver

/**
* Add a Listener to the underlying library.
*
* @param \Alchemy\BinaryDriver\Listeners\ListenerInterface $listener
* @return self
*/
public function addListener(ListenerInterface $listener): self
{
Expand All @@ -201,9 +196,6 @@ public function addListener(ListenerInterface $listener): self

/**
* Remove the Listener from the underlying library.
*
* @param \Alchemy\BinaryDriver\Listeners\ListenerInterface $listener
* @return self
*/
public function removeListener(ListenerInterface $listener): self
{
Expand All @@ -214,9 +206,6 @@ public function removeListener(ListenerInterface $listener): self

/**
* Adds a callable to the callbacks array.
*
* @param callable $callback
* @return self
*/
public function beforeSaving(callable $callback): self
{
Expand All @@ -227,8 +216,6 @@ public function beforeSaving(callable $callback): self

/**
* Set the callbacks on the Media.
*
* @return self
*/
public function applyBeforeSavingCallbacks(): self
{
Expand All @@ -243,10 +230,6 @@ public function applyBeforeSavingCallbacks(): self

/**
* Add an event handler to the underlying library.
*
* @param string $event
* @param callable $callback
* @return self
*/
public function onEvent(string $event, callable $callback): self
{
Expand Down
4 changes: 1 addition & 3 deletions src/Drivers/UnknownDurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

class UnknownDurationException extends Exception
{
}
class UnknownDurationException extends Exception {}
2 changes: 1 addition & 1 deletion src/Exporters/EncodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function decorate(RuntimeException $runtimeException): EncodingExc
$runtimeException->getPrevious()
), function (self $exception) {
if (config('laravel-ffmpeg.set_command_and_error_output_on_exception', true)) {
$exception->message = $exception->getAlchemyException()?->getMessage() ?: "";
$exception->message = $exception->getAlchemyException()?->getMessage() ?: '';
}
});
}
Expand Down
Loading
Loading