Skip to content

Commit

Permalink
fix and add audio conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 18, 2024
1 parent 119102d commit d2c7af2
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Concerns/InteractWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public function humanReadableDuration(
return null;
}

return CarbonInterval::milliseconds($this->duration)->forHumans($syntax, $short, $parts, $options);
return CarbonInterval::milliseconds($this->duration)
->cascade()
->forHumans($syntax, $short, $parts, $options);
}
}
78 changes: 78 additions & 0 deletions src/Definitions/MediaConversionAudio.php
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

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::__construct() has parameter $when with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia

Check failure on line 21 in src/Definitions/MediaConversionAudio.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::__construct() has parameter $when with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia
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

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::shouldExecute() has parameter $parent with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia

Check failure on line 43 in src/Definitions/MediaConversionAudio.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::shouldExecute() has parameter $parent with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia
{
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

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::handle() has parameter $parent with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia

Check failure on line 54 in src/Definitions/MediaConversionAudio.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::handle() return type with generic class Elegantly\Media\Models\MediaConversion does not specify its types: TMedia

Check failure on line 54 in src/Definitions/MediaConversionAudio.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::handle() has parameter $parent with generic class Elegantly\Media\Models\MediaConversion but does not specify its types: TMedia

Check failure on line 54 in src/Definitions/MediaConversionAudio.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Elegantly\Media\Definitions\MediaConversionAudio::handle() return type with generic class Elegantly\Media\Models\MediaConversion does not specify its types: TMedia
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,
);

}
}
6 changes: 4 additions & 2 deletions src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ public static function type(string $path): MediaType
public static function duration(string $path): ?float
{
if (static::type($path) === MediaType::Video) {
$disk = Storage::build([
$filesystem = Storage::build([
'driver' => 'local',
'root' => SupportFile::dirname($path),
]);

return FFMpeg::fromDisk($disk)->open(SupportFile::basename($path))->getDurationInMiliseconds();
return FFMpeg::fromFilesystem($filesystem)
->open(SupportFile::basename($path))
->getDurationInMiliseconds();
}

return null;
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Media extends Model
protected $casts = [
'type' => MediaType::class,
'metadata' => AsArrayObject::class,
'duration' => 'float',
'aspect_ratio' => 'float',
];

public static function booted()
Expand Down
11 changes: 9 additions & 2 deletions src/Models/MediaConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Illuminate\Support\Str;

/**
* @template TMedia of Media
*
* @property int $id
* @property string $uuid
* @property string $conversion_name
Expand Down Expand Up @@ -71,6 +73,8 @@ class MediaConversion extends Model
'type' => MediaType::class,
'metadata' => AsArrayObject::class,
'state_set_at' => 'datetime',
'duration' => 'float',
'aspect_ratio' => 'float',
];

public static function booted()
Expand All @@ -81,11 +85,14 @@ public static function booted()
}

/**
* @return BelongsTo<Media, MediaConversion>
* @return BelongsTo<TMedia, MediaConversion>
*/
public function media(): BelongsTo
{
return $this->belongsTo(Media::class);
/** @var class-string<Media> */
$class = config('media.model', Media::class);

return $this->belongsTo($class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
expect($media->mime_type)->toBe('image/svg+xml');
expect($media->width)->toBe(279);
expect($media->height)->toBe(279);
expect($media->aspect_ratio)->toBe(1);
expect($media->aspect_ratio)->toBe(1.0);
expect($media->duration)->toBe(null);
expect($media->size)->toBe(1853);

Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/IntercatWithMediaTest.php
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');

});

0 comments on commit d2c7af2

Please sign in to comment.