Skip to content

Commit eef29bb

Browse files
authored
fix: duration should be parsed as float (#3869)
* Fix duration should be float Updated video frame extraction logic and supported formats. * Fix frame extraction logic in Video.php Updated the logic to extract video frames by clamping the seconds to the video duration and using the correct variable for frame extraction. * Remove unsupported video formats and mime types Removed unsupported video formats from the list. * Remove 'mp4v-es' from video format lists Removed 'mp4v-es' from supported video formats. * Add support for 'video/mpeg' format
1 parent 04abdaa commit eef29bb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Conversions/ImageGenerators/Video.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use FFMpeg\FFMpeg;
77
use FFMpeg\Media\Video as FFMpegVideo;
88
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Number;
910
use Spatie\MediaLibrary\Conversions\Conversion;
1011

1112
class Video extends ImageGenerator
1213
{
1314
public function convert(string $file, ?Conversion $conversion = null): ?string
1415
{
16+
// Create an FFMpeg instance
1517
$ffmpeg = FFMpeg::create([
1618
'ffmpeg.binaries' => config('media-library.ffmpeg_path'),
1719
'ffprobe.binaries' => config('media-library.ffprobe_path'),
@@ -25,13 +27,19 @@ public function convert(string $file, ?Conversion $conversion = null): ?string
2527
return null;
2628
}
2729

28-
$duration = $ffmpeg->getFFProbe()->format($file)->get('duration');
30+
// Get the duration of the video in seconds
31+
$duration = (float) $ffmpeg->getFFProbe()->format($file)->get('duration');
2932

33+
// Determine at which second to extract the frame
3034
$seconds = $conversion ? $conversion->getExtractVideoFrameAtSecond() : 0;
31-
$seconds = $duration <= $seconds ? 0 : $seconds;
3235

36+
// Clamp the seconds to be within the video duration
37+
$seconds = Number::clamp($seconds, 0, $duration);
38+
39+
// Define the output image file path
3340
$imageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.jpg';
3441

42+
// Extract the frame at the specified time and save it as an image
3543
$frame = $video->frame(TimeCode::fromSeconds($seconds));
3644
$frame->save($imageFile);
3745

0 commit comments

Comments
 (0)