Passed
Push — master ( 89fbe5...c257f0 )
by Sébastien
02:18
created

VideoInfo::getStreamsByType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 0
dl 0
loc 21
ccs 12
cts 15
cp 0.8
crap 5.2
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Common\Exception\JsonParseException;
8
9
class VideoInfo implements VideoInfoInterface
10
{
11
    public const STREAM_TYPE_AUDIO = 'audio';
12
    public const STREAM_TYPE_VIDEO = 'video';
13
    public const STREAM_TYPE_DATA  = 'data';
14
15
    /** @var array */
16
    protected $metadata;
17
18
    /** @var string */
19
    protected $file;
20
21 1
    public function __construct(string $file, array $metadata)
22
    {
23 1
        $this->metadata = $metadata;
24 1
        $this->file     = $file;
25 1
    }
26
27 1
    public static function createFromFFProbeJson(string $file, string $ffprobeJson): self
28
    {
29 1
        if (trim($ffprobeJson) === '') {
30
            throw new JsonParseException('Cannot parse empty json string');
31
        }
32 1
        $decoded = json_decode($ffprobeJson, true);
33 1
        if ($decoded === null) {
34
            throw new JsonParseException('Cannot parse json');
35
        }
36
37 1
        return new self($file, $decoded);
38
    }
39
40 1
    public function getFile(): string
41
    {
42 1
        return $this->file;
43
    }
44
45
    public function getMetadata(): array
46
    {
47
        return $this->metadata;
48
    }
49
50 1
    public function getDuration(): float
51
    {
52 1
        return (float) ($this->metadata['format']['duration'] ?? 0.0);
53
    }
54
55 1
    public function getDimensions(): array
56
    {
57
        return [
58 1
            'width'  => $this->getWidth(),
59 1
            'height' => $this->getHeight(),
60
        ];
61
    }
62
63 1
    public function getWidth(): int
64
    {
65 1
        $videoStream = $this->getVideoStreamInfo();
66
67 1
        return (int) ($videoStream['width'] ?? 0);
68
    }
69
70 1
    public function getHeight(): int
71
    {
72 1
        $videoStream = $this->getVideoStreamInfo();
73
74 1
        return (int) ($videoStream['height'] ?? 0);
75
    }
76
77 1
    public function getNbFrames(): int
78
    {
79 1
        $videoStream = $this->getVideoStreamInfo();
80
81 1
        return (int) ($videoStream['nb_frames'] ?? 0);
82
    }
83
84
    public function getBitrate(): int
85
    {
86
        $videoStream = $this->getVideoStreamInfo();
87
88
        return (int) ($videoStream['bit_rate'] ?? 0);
89
    }
90
91
    public function getAudioStreamInfo(): ?array
92
    {
93
        return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null;
94
    }
95
96 1
    public function getVideoStreamInfo(): ?array
97
    {
98 1
        return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null;
99
    }
100
101 1
    protected function getStreamsByType(): array
102
    {
103 1
        $streams = $this->metadata['streams'] ?? [];
104 1
        foreach ($streams as $stream) {
105 1
            $type = mb_strtolower($stream['codec_type']);
106
            switch ($type) {
107 1
                case self::STREAM_TYPE_VIDEO:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108 1
                    $streams['video'] = $stream;
109 1
                    break;
110 1
                case self::STREAM_TYPE_AUDIO:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
111 1
                    $streams['audio'] = $stream;
112 1
                    break;
113
                case self::STREAM_TYPE_DATA:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
                    $streams['data'] = $stream;
115
                    break;
116
                default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
117 1
                    throw new \Exception(sprintf('Does not support codec_type "%s"', $type));
118
            }
119
        }
120
121 1
        return $streams;
122
    }
123
}
124