InteractsWithMediaStreams   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVideoStream() 0 4 1
A getAudioStream() 0 4 1
A getStreams() 0 9 2
A getDurationInMiliseconds() 0 12 3
A getDurationInSeconds() 0 3 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Drivers;
4
5
use FFMpeg\FFProbe\DataMapping\Stream;
6
use Illuminate\Support\Arr;
7
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
8
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection;
9
10
trait InteractsWithMediaStreams
11
{
12
    /**
13
     * Returns an array with all streams.
14
     *
15
     * @return array
16
     */
17
    public function getStreams(): array
18
    {
19
        if (!$this->isAdvancedMedia()) {
0 ignored issues
show
Bug introduced by
It seems like isAdvancedMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        if (!$this->/** @scrutinizer ignore-call */ isAdvancedMedia()) {
Loading history...
20
            return iterator_to_array($this->media->getStreams());
21
        }
22
23
        return $this->mediaCollection->map(function (Media $media) {
24
            return $this->fresh()->open(MediaCollection::make([$media]))->getStreams();
0 ignored issues
show
Bug introduced by
It seems like fresh() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            return $this->/** @scrutinizer ignore-call */ fresh()->open(MediaCollection::make([$media]))->getStreams();
Loading history...
25
        })->collapse()->all();
26
    }
27
28
    /**
29
     * Gets the duration of the media from the first stream or from the format.
30
     */
31
    public function getDurationInMiliseconds(): int
32
    {
33
        $stream = Arr::first($this->getStreams());
34
35
        if ($stream->has('duration')) {
36
            return $stream->get('duration') * 1000;
37
        }
38
39
        $format = $this->getFormat();
0 ignored issues
show
Bug introduced by
It seems like getFormat() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        /** @scrutinizer ignore-call */ 
40
        $format = $this->getFormat();
Loading history...
40
41
        if ($format->has('duration')) {
42
            return $format->get('duration') * 1000;
43
        }
44
    }
45
46
    public function getDurationInSeconds(): int
47
    {
48
        return round($this->getDurationInMiliseconds() / 1000);
49
    }
50
51
    /**
52
     * Gets the first audio streams of the media.
53
     */
54
    public function getAudioStream(): ?Stream
55
    {
56
        return Arr::first($this->getStreams(), function (Stream $stream) {
57
            return $stream->isAudio();
58
        });
59
    }
60
61
    /**
62
     * Gets the first video streams of the media.
63
     */
64
    public function getVideoStream(): ?Stream
65
    {
66
        return Arr::first($this->getStreams(), function (Stream $stream) {
67
            return $stream->isVideo();
68
        });
69
    }
70
}
71