Passed
Push — master ( 3ee3fe...b14545 )
by Amin
04:48
created

AutoRepresentations::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[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 Streaming;
13
14
use Streaming\Exception\Exception;
15
use FFMpeg\FFProbe\DataMapping\Stream;
16
17
class AutoRepresentations
18
{
19
    /** @var Stream $stream */
20
    private $stream;
21
22
    /** @Const regular video's heights */
23
    private const heights = [2160, 1080, 720, 480, 240, 144];
24
25
    /**
26
     * AutoRepresentations constructor.
27
     * @param Stream $stream
28
     */
29
    public function __construct(Stream $stream)
30
    {
31
        $this->stream = $stream;
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    private function getDimensions(): array
38
    {
39
        $width = $this->stream->get('width');
40
        $height = $this->stream->get('height');
41
42
        return [$width, $height, $width / $height];
43
    }
44
45
    /**
46
     * @return mixed
47
     * @throws Exception
48
     */
49
    private function getKiloBitRate(): int
50
    {
51
        if (!$this->stream->has('bit_rate')) {
52
            throw new Exception("Invalid stream");
53
        }
54
        return (int)$this->stream->get('bit_rate') / 1024;
55
    }
56
57
    /**
58
     * @return array
59
     * @throws Exception
60
     */
61
    public function get(): array
62
    {
63
        $kilobitrate = $this->getKiloBitRate();
64
        list($width, $height, $ratio) = $this->getDimensions();
65
66
        $representations[] = $this->addRepresentation($kilobitrate, $width, $height);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$representations was never initialized. Although not strictly required by PHP, it is generally a good practice to add $representations = array(); before regardless.
Loading history...
67
68
        $heights = array_filter(static::heights, function ($value) use ($height) {
69
            return $value < $height;
70
        });
71
72
        if (!empty($heights)) {
73
            $kilobitrates = $this->getKiloBitRates($kilobitrate, count($heights));
74
75
            foreach (array_values($heights) as $key => $height) {
76
                $representations[] = $this->addRepresentation($kilobitrates[$key], Helper::roundToEven($ratio * $height), $height);
77
            }
78
        }
79
80
        return array_reverse($representations);
81
    }
82
83
    /**
84
     * @param $width
85
     * @param $kilobitrate
86
     * @param $height
87
     * @return Representation
88
     * @throws Exception
89
     */
90
    private function addRepresentation($kilobitrate, $width, $height): Representation
91
    {
92
        return (new Representation())->setKiloBitrate($kilobitrate)->setResize($width, $height);
93
    }
94
95
    /**
96
     * @param $kilobitrate
97
     * @param $count
98
     * @return array
99
     */
100
    private function getKiloBitRates($kilobitrate, $count): array
101
    {
102
        $divided_by = 1.3;
103
104
        while ($count) {
105
            $kilobitrates[] = (($kbitrate = intval($kilobitrate / $divided_by)) < 64) ? 64 : $kbitrate;
106
            $divided_by += .3;
107
            $count--;
108
        }
109
110
        return $kilobitrates;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $kilobitrates does not seem to be defined for all execution paths leading up to this point.
Loading history...
111
    }
112
}