Passed
Push — master ( 5b54fd...e408cf )
by Amin
02:09
created

Filter::liveFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>.
5
 *
6
 * Licensed under the MIT License;
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      https://opensource.org/licenses/MIT
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace AYazdanpanah\FFMpegStreaming;
20
21
use AYazdanpanah\FFMpegStreaming\Format\X264;
22
use FFMpeg\Filters\FilterInterface;
23
24
class Filter implements FilterInterface
25
{
26
    private $priority = 2;
27
28
    private $filter = [];
29
30
    /**
31
     * Filter constructor.
32
     * @param Export $media
33
     */
34
    public function __construct(Export $media)
35
    {
36
        $this->setFilter($media);
37
    }
38
39
40
    /**
41
     * Applies the filter on the the Audio media given an format.
42
     *
43
     * @return array An array of arguments
44
     */
45
    public function apply(): array
46
    {
47
        return $this->getFilter();
48
    }
49
50
    /**
51
     * Returns the priority of the filter.
52
     *
53
     * @return integer
54
     */
55
    public function getPriority(): int
56
    {
57
        return $this->priority;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getFilter(): array
64
    {
65
        return $this->filter;
66
    }
67
68
    /**
69
     * @param $media
70
     */
71
    public function setFilter(Export $media): void
72
    {
73
        if ($media instanceof DASH) {
74
            $this->filter = $this->DASHFilter($media);
75
        } elseif ($media instanceof HLS) {
76
            $this->filter = $this->HLSFilter($media);
77
        } elseif ($media instanceof Live) {
78
            $this->filter = $this->liveFilter($media);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->filter is correct as $this->liveFilter($media) targeting AYazdanpanah\FFMpegStreaming\Filter::liveFilter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
79
        }
80
    }
81
82
    /**
83
     * @param DASH $media
84
     * @return array
85
     */
86
    private function DASHFilter(DASH $media)
87
    {
88
        $filter = $this->getDASHFilter();
89
90
        if($media->format instanceof X264){
91
            $filter[] = "-profile:v:0";
92
            $filter[] =  "main";
93
        }
94
95
        foreach ($media->getRepresentations() as $key => $representation) {
96
            if ($representation instanceof Representation) {
97
                $filter[] = "-map";
98
                $filter[] = "0";
99
                $filter[] = "-b:v:" . $key;
100
                $filter[] = $representation->getKiloBitrate() . "k";
101
                if (null !== $representation->getResize()) {
102
                    $filter[] = "-s:v:" . $key;
103
                    $filter[] = $representation->getResize();
104
                }
105
                if ($key > 0 && $media->format instanceof X264) {
106
                    $filter[] = "-profile:v:" . $key;
107
                    $filter[] = "baseline";
108
                }
109
            }
110
        }
111
112
        if ($media->getAdaption()) {
113
            $filter[] = "-adaptation_sets";
114
            $filter[] = $media->getAdaption();
115
        }
116
117
        return $filter;
118
    }
119
120
    /**
121
     * @param HLS $media
122
     * @return array
123
     */
124
    private function HLSFilter(HLS $media)
125
    {
126
        $filter = [
127
            "-f",
128
            "hls"
129
        ];
130
131
        foreach ($media->getRepresentations() as $key => $representation) {
132
            if ($representation instanceof Representation) {
133
                $filter[] = "-map";
134
                $filter[] = "0:v";
135
                $filter[] = "-b:v:" . $key;
136
                $filter[] = $representation->getKiloBitrate() . "k";
137
                if (null !== $representation->getResize()) {
138
                    $filter[] = "-s:v:" . $key;
139
                    $filter[] = $representation->getResize();
140
                }
141
            }
142
        }
143
144
        if ($media->getStreamMap()) {
145
            $filter[] = "-var_stream_map";
146
            $filter[] = $media->getStreamMap();
147
        }
148
149
        return $filter;
150
    }
151
152
    /**
153
     * @param Live $media
154
     */
155
    private function liveFilter(Live $media)
0 ignored issues
show
Unused Code introduced by
The parameter $media is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
    private function liveFilter(/** @scrutinizer ignore-unused */ Live $media)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        die("Live (Soon)");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
158
    }
159
160
    private function getDASHFilter()
161
    {
162
        return [
163
            "-bf",
164
            "1",
165
            "-keyint_min",
166
            "120",
167
            "-g",
168
            "120",
169
            "-sc_threshold",
170
            "0",
171
            "-b_strategy",
172
            "0",
173
            "-use_timeline",
174
            "1",
175
            "-use_template",
176
            "1",
177
            "-f",
178
            "dash"
179
        ];
180
    }
181
}