Skip to content

Commit

Permalink
prevent audio conversion from video when no stream
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 4, 2024
1 parent 371564c commit 875d89a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Definitions/MediaConversionAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use FFMpeg\Format\Audio\Mp3;
use FFMpeg\Format\FormatInterface;
use Illuminate\Contracts\Filesystem\Filesystem;
use ProtoneMedia\LaravelFFMpeg\FFMpeg\FFProbe;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use Spatie\TemporaryDirectory\TemporaryDirectory as SpatieTemporaryDirectory;

Expand Down Expand Up @@ -51,6 +52,18 @@ public function shouldExecute(Media $media, ?MediaConversion $parent): bool
return in_array($source->type, [MediaType::Video, MediaType::Audio]);
}

public function hasFileAudioStream(string $path): bool
{
$ffprobe = FFProbe::create([
'ffmpeg.binaries' => config('laravel-ffmpeg.ffmpeg.binaries'),
'ffprobe.binaries' => config('laravel-ffmpeg.ffprobe.binaries'),
]);

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

return (bool) $streams->audios()->count();
}

public function handle(
Media $media,
?MediaConversion $parent,
Expand All @@ -62,6 +75,13 @@ public function handle(
return null;
}

/**
* Videos do not always have an audio stream
*/
if (! $this->hasFileAudioStream($file)) {
return null;
}

$fileName = $this->fileName ?? "{$media->name}.mp3";

$ffmpeg = FFMpeg::fromFilesystem($filesystem)
Expand Down

0 comments on commit 875d89a

Please sign in to comment.