-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Elegantly\Media\Definitions; | ||
|
||
use Closure; | ||
use Elegantly\Media\Enums\MediaType; | ||
use Elegantly\Media\Models\Media; | ||
use Elegantly\Media\Models\MediaConversion; | ||
use FFMpeg\Format\Audio\Mp3; | ||
use FFMpeg\Format\FormatInterface; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg; | ||
use Spatie\TemporaryDirectory\TemporaryDirectory as SpatieTemporaryDirectory; | ||
|
||
class MediaConversionAudio extends MediaConversionDefinition | ||
{ | ||
/** | ||
* @param MediaConversionDefinition[] $conversions | ||
* @param null|bool|Closure(Media $media, ?MediaConversion $parent): bool $when | ||
*/ | ||
public function __construct( | ||
Check failure on line 21 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
Check failure on line 21 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
|
||
public string $name, | ||
public null|bool|Closure $when = null, | ||
public bool $immediate = true, | ||
public bool $queued = true, | ||
public ?string $queue = null, | ||
public array $conversions = [], | ||
public ?string $fileName = null, | ||
public FormatInterface $format = new Mp3, | ||
) { | ||
|
||
parent::__construct( | ||
name: $name, | ||
handle: fn () => null, | ||
when: $when, | ||
immediate: $immediate, | ||
queued: $queued, | ||
queue: $queue, | ||
conversions: $conversions | ||
); | ||
} | ||
|
||
public function shouldExecute(Media $media, ?MediaConversion $parent): bool | ||
Check failure on line 43 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
Check failure on line 43 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
|
||
{ | ||
if ($this->when !== null) { | ||
return parent::shouldExecute($media, $parent); | ||
} | ||
|
||
$source = $parent ?? $media; | ||
|
||
return in_array($source->type, [MediaType::Video, MediaType::Audio]); | ||
} | ||
|
||
public function handle( | ||
Check failure on line 54 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
Check failure on line 54 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
Check failure on line 54 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
Check failure on line 54 in src/Definitions/MediaConversionAudio.php GitHub Actions / phpstan
|
||
Media $media, | ||
?MediaConversion $parent, | ||
string $file, | ||
Filesystem $filesystem, | ||
SpatieTemporaryDirectory $temporaryDirectory | ||
): ?MediaConversion { | ||
|
||
$fileName = $this->fileName ?? "{$media->name}.mp3"; | ||
|
||
$ffmpeg = FFMpeg::fromFilesystem($filesystem) | ||
->open($file) | ||
->export() | ||
->inFormat($this->format); | ||
|
||
$ffmpeg->save($fileName); | ||
|
||
return $media->addConversion( | ||
file: $filesystem->path($fileName), | ||
conversionName: $this->name, | ||
parent: $parent, | ||
); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Elegantly\Media\Models\Media; | ||
|
||
it('retreives the size in a human readable format', function () { | ||
|
||
$media = new Media([ | ||
'size' => 12345, | ||
]); | ||
|
||
expect($media->humanReadableSize())->tobe('12 KB'); | ||
|
||
}); | ||
|
||
it('retreives the duration in a human readable format', function () { | ||
|
||
$media = new Media([ | ||
'duration' => 123456.00, | ||
]); | ||
|
||
expect($media->duration)->toBeFloat(); | ||
|
||
expect($media->humanReadableDuration())->tobe('2 minutes 3 seconds'); | ||
|
||
}); |