Video::convert()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace Spatie\MediaLibrary\ImageGenerators\FileTypes;
4
5
use FFMpeg\Coordinate\TimeCode;
6
use FFMpeg\FFMpeg;
7
use Illuminate\Support\Collection;
8
use Spatie\MediaLibrary\Conversion\Conversion;
9
use Spatie\MediaLibrary\ImageGenerators\BaseGenerator;
10
11
class Video extends BaseGenerator
12
{
13
    public function convert(string $file, Conversion $conversion = null): string
14
    {
15
        $imageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.jpg';
16
17
        $ffmpeg = FFMpeg::create([
18
            'ffmpeg.binaries' => config('medialibrary.ffmpeg_path'),
19
            'ffprobe.binaries' => config('medialibrary.ffprobe_path'),
20
        ]);
21
22
        $video = $ffmpeg->open($file);
23
        $duration = $ffmpeg->getFFProbe()->format($file)->get('duration');
24
25
        $seconds = $conversion ? $conversion->getExtractVideoFrameAtSecond() : 0;
26
        $seconds = $duration < $seconds ? 0 : $seconds;
27
28
        $frame = $video->frame(TimeCode::fromSeconds($seconds));
29
        $frame->save($imageFile);
30
31
        return $imageFile;
32
    }
33
34
    public function requirementsAreInstalled(): bool
35
    {
36
        return class_exists('\\FFMpeg\\FFMpeg');
37
    }
38
39
    public function supportedExtensions(): Collection
40
    {
41
        return collect(['webm', 'mov', 'mp4']);
42
    }
43
44
    public function supportedMimeTypes(): Collection
45
    {
46
        return collect(['video/webm', 'video/mpeg', 'video/mp4', 'video/quicktime']);
47
    }
48
}
49