Skip to content

Commit

Permalink
more robust type
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 26, 2024
1 parent e1221d5 commit f191d6b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
44 changes: 40 additions & 4 deletions src/Enums/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Finller\Media\Enums;

use Finller\Media\Helpers\File;
use ProtoneMedia\LaravelFFMpeg\FFMpeg\FFProbe;

enum MediaType: string
{
case Video = 'video';
Expand All @@ -12,10 +15,6 @@ enum MediaType: string

public static function tryFromMimeType(string $mimeType)
{
if (str_starts_with($mimeType, 'video/')) {
return self::Video;
}

if (str_starts_with($mimeType, 'image/')) {
return self::Image;
}
Expand All @@ -24,10 +23,47 @@ public static function tryFromMimeType(string $mimeType)
return self::Audio;
}

if (str_starts_with($mimeType, 'video/')) {
return self::Video;
}

if (in_array($mimeType, ['application/pdf', 'application/acrobat', 'application/nappdf', 'application/x-pdf', 'image/pdf'])) {
return self::Pdf;
}

return self::Other;
}

/**
* Some codec like 3GPP files can contain either audios or videos
* To determine the true type, we need to check which stream is defined
*/
public static function tryFromStreams(string $path)
{
$type = self::tryFromMimeType(File::mimeType($path));

if (
$type === self::Video ||
$type === self::Audio
) {
$ffprobe = FFProbe::create([
'ffmpeg.binaries' => config('laravel-ffmpeg.ffmpeg.binaries'),
'ffprobe.binaries' => config('laravel-ffmpeg.ffprobe.binaries'),
]);

$streams = $ffprobe->streams($path);

if ($streams->videos()->first()) {
return self::Video;
}

if ($streams->audios()->first()) {
return self::Audio;
}

return self::Other;
}

return $type;
}
}
2 changes: 1 addition & 1 deletion src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function extension(string|HttpFile|UploadedFile $file): ?string

public static function type(string $path): MediaType
{
return MediaType::tryFromMimeType(SupportFile::mimeType($path));
return MediaType::tryFromStreams($path);
}

public static function duration(string $path): ?float
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Video implements HasDimension
{
public static function dimension(string $path): ?Dimension
{
$file = FFProbe::create([
$ffprobe = FFProbe::create([
'ffmpeg.binaries' => config('laravel-ffmpeg.ffmpeg.binaries'),
'ffprobe.binaries' => config('laravel-ffmpeg.ffprobe.binaries'),
]);

$stream = $file
$stream = $ffprobe
->streams($path)
->videos()
->first();
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function storeConversionFromHttpFile(
$extension = File::extension($file);
$file_name = "{$name}.{$extension}";
$mime_type = File::mimeType($file);
$type = MediaType::tryFromMimeType($mime_type);
$type = File::type($file->getPathname());
$dimension = File::dimension($file->getPathname(), type: $type);

$existingConversion = $this->getGeneratedConversion($conversion);
Expand Down

0 comments on commit f191d6b

Please sign in to comment.