Skip to content

Commit

Permalink
allow seconds to be a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 19, 2025
1 parent 80edf13 commit 26625e9
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Definitions/MediaConversionPoster.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MediaConversionPoster extends MediaConversionDefinition
{
/**
* @param null|string|(Closure(Media $media, ?MediaConversion $parent):string) $fileName
* @param TimeCode|float|(Closure(Media $media, ?MediaConversion $parent):TimeCode) $seconds
*/
public function __construct(
public string $name,
Expand All @@ -30,7 +31,7 @@ public function __construct(
public ?string $queue = null,
public array $conversions = [],
public null|Closure|string $fileName = null,
public TimeCode|float $seconds = 0.0,
public Closure|TimeCode|float $seconds = 0.0,
public ?int $width = null,
public ?int $height = null,
public Fit $fit = Fit::Contain,
Expand Down Expand Up @@ -69,6 +70,21 @@ public function getFileName(Media $media, ?MediaConversion $parent): string
return "{$source->name}.jpg";
}

public function getTimeCode(Media $media, ?MediaConversion $parent): TimeCode
{
$seconds = $this->seconds;

if (is_float($seconds)) {
return TimeCode::fromSeconds($seconds);
}

if ($seconds instanceof TimeCode) {
return $seconds;
}

return $seconds($media, $parent);
}

public function handle(
Media $media,
?MediaConversion $parent,
Expand All @@ -84,11 +100,7 @@ public function handle(

FFMpeg::fromFilesystem($filesystem)
->open($file)
->getFrameFromTimecode(
is_float($this->seconds)
? TimeCode::fromSeconds($this->seconds)
: $this->seconds
)
->getFrameFromTimecode($this->getTimeCode($media, $parent))
->export()
->save($fileName);

Expand Down

0 comments on commit 26625e9

Please sign in to comment.