Passed
Push — master ( 8caa9b...dc3048 )
by Sébastien
06:46
created

VideoInfo::getFileSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Common\Exception\IOException;
8
use Soluble\MediaTools\Common\Exception\JsonParseException;
9
10
class VideoInfo implements VideoInfoInterface
11
{
12
    public const STREAM_TYPE_AUDIO = 'audio';
13
    public const STREAM_TYPE_VIDEO = 'video';
14
    public const STREAM_TYPE_DATA  = 'data';
15
16
    /** @var array */
17
    protected $metadata;
18
19
    /** @var string */
20
    protected $file;
21 1
22
    public function __construct(string $file, array $metadata)
23 1
    {
24 1
        $this->metadata = $metadata;
25 1
        $this->file     = $file;
26
    }
27 1
28
    public static function createFromFFProbeJson(string $file, string $ffprobeJson): self
29 1
    {
30
        if (trim($ffprobeJson) === '') {
31
            throw new JsonParseException('Cannot parse empty json string');
32 1
        }
33 1
        $decoded = json_decode($ffprobeJson, true);
34
        if ($decoded === null) {
35
            throw new JsonParseException('Cannot parse json');
36
        }
37 1
38
        return new self($file, $decoded);
39
    }
40 1
41
    public function getFile(): string
42 1
    {
43
        return $this->file;
44
    }
45 1
46
    /**
47 1
     * @throws IOException
48
     */
49
    public function getFileSize(): int
50 1
    {
51
        $size = @filesize($this->file);
52 1
        if ($size === false) {
53
            throw new IOException(sprintf(
54
                'Cannot get filesize of file %s',
55
                $this->file
56
            ));
57
        }
58
59
        return $size;
60 1
    }
61
62 1
    public function getFormatName(): string
63
    {
64
        return $this->metadata['format']['format_name'];
65 1
    }
66
67
    public function countStreams(): int
68 1
    {
69 1
        return $this->metadata['format']['nb_streams'];
70
    }
71
72
    public function getMetadata(): array
73 1
    {
74
        return $this->metadata;
75 1
    }
76
77 1
    public function getDuration(): float
78
    {
79
        return (float) ($this->metadata['format']['duration'] ?? 0.0);
80 1
    }
81
82 1
    public function getDimensions(): array
83
    {
84 1
        return [
85
            'width'  => $this->getWidth(),
86
            'height' => $this->getHeight(),
87 1
        ];
88
    }
89 1
90
    public function getWidth(): int
91 1
    {
92
        $videoStream = $this->getVideoStreamInfo();
93
94
        return (int) ($videoStream['width'] ?? 0);
95
    }
96
97
    public function getHeight(): int
98
    {
99
        $videoStream = $this->getVideoStreamInfo();
100
101
        return (int) ($videoStream['height'] ?? 0);
102
    }
103
104
    public function getNbFrames(): int
105
    {
106 1
        $videoStream = $this->getVideoStreamInfo();
107
108 1
        return (int) ($videoStream['nb_frames'] ?? 0);
109
    }
110
111 1
    public function getBitrate(): int
112
    {
113 1
        $videoStream = $this->getVideoStreamInfo();
114 1
115 1
        return (int) ($videoStream['bit_rate'] ?? 0);
116
    }
117 1
118 1
    public function getAudioStreamInfo(): ?array
119 1
    {
120 1
        return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null;
121 1
    }
122 1
123
    public function getVideoStreamInfo(): ?array
124
    {
125
        return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null;
126
    }
127 1
128
    protected function getStreamsByType(): array
129
    {
130
        $streams = $this->metadata['streams'] ?? [];
131 1
        foreach ($streams as $stream) {
132
            $type = mb_strtolower($stream['codec_type']);
133
            switch ($type) {
134
                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...
135
                    $streams['video'] = $stream;
136
                    break;
137
                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...
138
                    $streams['audio'] = $stream;
139
                    break;
140
                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...
141
                    $streams['data'] = $stream;
142
                    break;
143
                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...
144
                    throw new \Exception(sprintf('Does not support codec_type "%s"', $type));
145
            }
146
        }
147
148
        return $streams;
149
    }
150
}
151