FfprobeReader::read()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 78
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 506

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 0
cts 67
cp 0
rs 2.2923
c 0
b 0
f 0
cc 22
eloc 49
nc 1966079
nop 1
crap 506

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Meta Reader package.
5
 *
6
 * (c) Stephan Wentz <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Temp\MetaReader;
13
14
use FFMpeg\FFProbe;
15
use FFMpeg\FFProbe\DataMapping\Stream;
16
use Temp\MetaReader\Value\MetaValue;
17
use Temp\MetaReader\Value\ValueBag;
18
19
/**
20
 * Ffprobe reader.
21
 *
22
 * @author Stephan Wentz <[email protected]>
23
 */
24
class FfprobeReader implements ReaderInterface
25
{
26
    /**
27
     * @var FFProbe
28
     */
29
    private $ffprobe;
30
31
    /**
32
     * @param FFProbe $ffprobe
33
     */
34
    public function __construct(FFProbe $ffprobe)
35
    {
36
        $this->ffprobe = $ffprobe;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function available()
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supports($filename)
51
    {
52
        try {
53
            $streams = $this->ffprobe->streams($filename);
54
        } catch (\Exception $e) {
55
            return false;
56
        }
57
58
        foreach ($streams as $stream) {
59
            if (!$stream->has('codec_type') || !in_array($stream->get('codec_type'), ['audio', 'video'])) {
60
                return false;
61
            }
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function read($filename)
71
    {
72
        $meta = new ValueBag();
73
74
        try {
75
            $format = $this->ffprobe->format($filename);
76
77
            if ($format->has('format_name')) {
78
                $meta->set('media.format_name', new MetaValue($format->get('format_name')));
79
            }
80
            if ($format->has('format_long_name')) {
81
                $meta->set('media.format_long_name', new MetaValue($format->get('format_long_name')));
82
            }
83
            if ($format->has('duration')) {
84
                $meta->set('media.duration', new MetaValue($format->get('duration')));
85
            }
86
            if ($format->has('bit_rate')) {
87
                $meta->set('media.bit_rate', new MetaValue($format->get('bit_rate')));
88
            }
89
            if ($format->has('width')) {
90
                $meta->set('media.width', new MetaValue($format->get('width')));
91
            }
92
            if ($format->has('height')) {
93
                $meta->set('media.height', new MetaValue($format->get('height')));
94
            }
95
            if ($format->has('nb_streams')) {
96
                $meta->set('media.number_of_streams', new MetaValue($format->get('nb_streams')));
97
            }
98
99
            $streams = $this->ffprobe->streams($filename);
100
101
            foreach ($streams as $stream) {
102
                $index = $stream->get('index');
103
                $prefix = 'stream_'.$index;
104
105
                $type = 'media';
106
                if ($stream->isVideo()) {
107
                    $type = 'video';
108
                } elseif ($stream->isAudio()) {
109
                    $type = 'audio';
110
                }
111
112
                if ($stream->has('codec_type')) {
113
                    $meta->set("$type.$prefix.codec_type", new MetaValue($stream->get('codec_type')));
114
                }
115
                if ($stream->has('codec_name')) {
116
                    $meta->set("$type.$prefix.codec_name", new MetaValue($stream->get('codec_name')));
117
                }
118
                if ($stream->has('codec_long_name')) {
119
                    $meta->set("$type.$prefix.codec_long_name", new MetaValue($stream->get('codec_long_name')));
120
                }
121
                if ($stream->has('codec_time_base')) {
122
                    $meta->set("$type.$prefix.codec_time_base", new MetaValue($stream->get('codec_time_base')));
123
                }
124
                if ($stream->has('codec_tag_string')) {
125
                    $meta->set("$type.$prefix.codec_tag", new MetaValue($stream->get('codec_tag_string')));
126
                }
127
                if ($stream->has('bit_rate')) {
128
                    $meta->set("$type.$prefix.bit_rate", new MetaValue($stream->get('bit_rate')));
129
                }
130
                if ($stream->has('display_aspect_ration')) {
131
                    $meta->set("$type.$prefix.aspect_ratio", new MetaValue($stream->get('display_aspect_ratio')));
132
                }
133
                if ($stream->has('avg_frame_rate')) {
134
                    $meta->set("$type.$prefix.frame_rate", new MetaValue($stream->get('avg_frame_rate')));
135
                }
136
                if ($stream->has('bits_per_sample')) {
137
                    $meta->set("$type.$prefix.bits_per_sample", new MetaValue($stream->get('bits_per_sample')));
138
                }
139
                if ($stream->has('channels')) {
140
                    $meta->set("$type.$prefix.channels", new MetaValue($stream->get('channels')));
141
                }
142
            }
143
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by Stephan Wentz
Consider adding a comment why this CATCH block is empty.
Loading history...
144
        }
145
146
        return $meta;
147
    }
148
}
149