Passed
Push — master ( 4bc7af...ac0e9d )
by Amin
02:25
created

DASHFilter::streamFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 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
13
namespace Streaming\Filters;
14
15
16
use Streaming\StreamInterface;
17
use Streaming\Format\X264;
18
use Streaming\Representation;
19
20
class DASHFilter extends StreamFilter
21
{
22
    /** @var \Streaming\DASH */
23
    private $dash;
24
    /**
25
     * @param StreamInterface $stream
26
     */
27
    public function streamFilter(StreamInterface $stream): void
28
    {
29
        $this->dash = $stream;
0 ignored issues
show
Documentation Bug introduced by
$stream is of type Streaming\StreamInterface, but the property $dash was declared to be of type Streaming\DASH. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30
        $this->set();
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    private function set()
37
    {
38
        $this->filter = $this->getBaseFilters();
39
40
        foreach ($this->dash->getRepresentations() as $key => $representation) {
41
            $this->filter[] = "-map";
42
            $this->filter[] = "0";
43
            $this->filter[] = "-b:v:" . $key;
44
            $this->filter[] = $representation->getKiloBitrate() . "k";
45
            $this->filter = array_merge($this->filter, $this->getAudioBitrate($representation, $key));
46
47
            if (null !== $representation->getResize()) {
48
                $this->filter[] = "-s:v:" . $key;
49
                $this->filter[] = $representation->getResize();
50
            }
51
        }
52
53
        if ($this->dash->getAdaption()) {
54
            $this->filter[] = "-adaptation_sets";
55
            $this->filter[] = $this->dash->getAdaption();
56
        }
57
        $this->filter = array_merge($this->filter, $this->dash->getAdditionalParams());
58
        $this->filter = array_merge($this->filter, ["-strict", $this->dash->getStrict()]);
59
60
        return $this->filter;
61
    }
62
63
    /**
64
65
     * @return array
66
     */
67
    private function getBaseFilters(): array
68
    {
69
        $filename = $this->dash->getPathInfo(PATHINFO_FILENAME);
70
71
        $this->filter = [
72
            "-bf", "1",
73
            "-keyint_min", "120",
74
            "-g", "120",
75
            "-sc_threshold", "0",
76
            "-b_strategy", "0",
77
            "-use_timeline", "1",
78
            "-use_template", "1",
79
            "-init_seg_name", ($filename . '_init_$RepresentationID$.$ext$'),
80
            "-media_seg_name", ($filename . '_chunk_$RepresentationID$_$Number%05d$.$ext$'),
81
            "-seg_duration", $this->dash->getSegDuration(),
82
            "-hls_playlist", (int)$this->dash->isGenerateHlsPlaylist(),
83
            "-f", "dash",
84
        ];
85
86
        if ($this->dash->getFormat() instanceof X264) {
87
            $this->filter[] = "-profile:v:0";
88
            $this->filter[] = "main";
89
90
            $count = count($this->dash->getRepresentations());
91
92
            while ($count > 0) {
93
                $this->filter[] = "-profile:v:" . $count;
94
                $this->filter[] = "baseline";
95
                $count--;
96
            }
97
        }
98
99
        return $this->filter;
100
    }
101
102
    /**
103
     * @param Representation $rep
104
     * @param int $key
105
     * @return array
106
     */
107
    private function getAudioBitrate(Representation $rep, int $key): array
108
    {
109
        return $rep->getAudioKiloBitrate() ? ["-b:a:" . $key, $rep->getAudioKiloBitrate() . "k"] : [];
110
    }
111
}