Completed
Push — master ( fc4224...debade )
by Sébastien
04:20
created

VideoInfo::getNbFrames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools;
6
7
use Soluble\MediaTools\Exception\JsonParseException;
8
use Soluble\MediaTools\Video\InfoInterface;
9
10
class VideoInfo implements InfoInterface
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
22 1
    public function __construct(string $file, array $metadata)
23
    {
24 1
        $this->metadata = $metadata;
25 1
        $this->file     = $file;
26 1
    }
27
28 1
    public static function createFromFFProbeJson(string $file, string $ffprobeJson): self
29
    {
30 1
        if (trim($ffprobeJson) === '') {
31
            throw new JsonParseException('Cannot parse empty json string');
32
        }
33 1
        $decoded = json_decode($ffprobeJson, true);
34 1
        if ($decoded === null) {
35
            throw new JsonParseException('Cannot parse json');
36
        }
37
38 1
        return new self($file, $decoded);
39
    }
40
41
    public function getFile(): string
42
    {
43
        return $this->file;
44
    }
45
46
    public function getMetadata(): array
47
    {
48
        return $this->metadata;
49
    }
50
51 1
    public function getDuration(): float
52
    {
53 1
        return (float) ($this->metadata['format']['duration'] ?? 0.0);
54
    }
55
56
    public function getDimensions(): array
57
    {
58
        $videoStream = $this->getVideoStreamInfo();
59
60
        return [
61
            'width'  => (int) ($videoStream['width'] ?? 0),
62
            'height' => (int) ($videoStream['height'] ?? 0),
63
        ];
64
    }
65
66
    public function getNbFrames(): int
67
    {
68
        $videoStream = $this->getVideoStreamInfo();
69
70
        return (int) ($videoStream['nb_frames'] ?? 0);
71
    }
72
73
    public function getBitrate(): int
74
    {
75
        $videoStream = $this->getVideoStreamInfo();
76
77
        return (int) ($videoStream['bit_rate'] ?? 0);
78
    }
79
80
    public function getAudioStreamInfo(): ?array
81
    {
82
        return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null;
83
    }
84
85
    public function getVideoStreamInfo(): ?array
86
    {
87
        return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null;
88
    }
89
90
    protected function getStreamsByType(): array
91
    {
92
        $streams = $this->metadata['streams'] ?? [];
93
        foreach ($streams as $stream) {
94
            $type = mb_strtolower($stream['codec_type']);
95
            switch ($type) {
96
                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...
97
                    $streams['video'] = $stream;
98
                    break;
99
                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...
100
                    $streams['audio'] = $stream;
101
                    break;
102
                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...
103
                    $streams['data'] = $stream;
104
                    break;
105
                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...
106
                    throw new \Exception(sprintf('Does not support codec_type "%s"', $type));
107
            }
108
        }
109
110
        return $streams;
111
    }
112
}
113