Passed
Push — master ( 4d4fc5...4bc7af )
by Amin
03:11
created

HLSFilter::initArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 16
rs 9.7998
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
namespace Streaming\Filters;
13
14
use Streaming\File;
15
use Streaming\Representation;
16
use Streaming\Utiles;
17
18
class HLSFilter extends Filter
19
{
20
    /**  @var \Streaming\HLS */
21
    private $hls;
22
23
    /** @var string */
24
    private $dirname;
25
26
    /** @var string */
27
    private $filename;
28
29
    /** @var string */
30
    private $seg_sub_dir;
31
32
    /** @var string */
33
    private $base_url;
34
35
    /** @var string */
36
    private $seg_filename;
37
38
    /**
39
     * @param
40
     * @return array
41
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
42
    private function getFormats(): array
43
    {
44
        $format = ['-c:v', $this->hls->getFormat()->getVideoCodec()];
0 ignored issues
show
Bug introduced by
The method getVideoCodec() does not exist on FFMpeg\Format\FormatInterface. It seems like you code against a sub-type of FFMpeg\Format\FormatInterface such as FFMpeg\Format\VideoInterface or Streaming\Format\Video or FFMpeg\Format\Video\DefaultVideo. ( Ignorable by Annotation )

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

44
        $format = ['-c:v', $this->hls->getFormat()->/** @scrutinizer ignore-call */ getVideoCodec()];
Loading history...
45
        $audio_format = $this->hls->getFormat()->getAudioCodec();
0 ignored issues
show
Bug introduced by
The method getAudioCodec() does not exist on FFMpeg\Format\FormatInterface. It seems like you code against a sub-type of FFMpeg\Format\FormatInterface such as FFMpeg\Format\AudioInterface. ( Ignorable by Annotation )

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

45
        $audio_format = $this->hls->getFormat()->/** @scrutinizer ignore-call */ getAudioCodec();
Loading history...
46
47
        return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format;
48
    }
49
50
    /**
51
     * @param Representation $rep
52
     * @param bool $not_last
53
     * @return array
54
     */
55
    private function playlistPath(Representation $rep, bool $not_last): array
56
    {
57
        return $not_last ? [$this->dirname . "/" . $this->filename . "_" . $rep->getHeight() . "p.m3u8"] : [];
58
    }
59
60
    /**
61
     * @param Representation $rep
62
     * @return array
63
     */
64
    private function getAudioBitrate(Representation $rep): array
65
    {
66
        return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : [];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    private function getBaseURL(): array
73
    {
74
        return $this->base_url ? ["-hls_base_url", $this->base_url] : [];
75
    }
76
77
    /**
78
     * @param
79
     * @return array
80
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
81
    private function getKeyInfo(): array
82
    {
83
        return $this->hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $this->hls->getHlsKeyInfoFile()] : [];
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    private function getInitFilename(): string
90
    {
91
        return $this->seg_sub_dir . $this->filename . "_" . $this->hls->getHlsFmp4InitFilename();
92
    }
93
94
    /**
95
     * @param Representation $rep
96
     * @return string
97
     */
98
    private function getSegmentFilename(Representation $rep): string
99
    {
100
        $ext = ($this->hls->getHlsFmp4InitFilename() === "fmp4") ? "m4s" : "ts";
101
        return $this->seg_filename . "_" . $rep->getHeight() . "p_%04d." . $ext;
102
    }
103
104
    /**
105
     * @param Representation $rep
106
     * @return array
107
     */
108
    private function initArgs(Representation $rep): array
109
    {
110
        return [
111
            "-s:v", $rep->getResize(),
112
            "-crf", "20",
113
            "-sc_threshold", "0",
114
            "-g", "48",
115
            "-keyint_min", "48",
116
            "-hls_list_size", $this->hls->getHlsListSize(),
117
            "-hls_time", $this->hls->getHlsTime(),
118
            "-hls_allow_cache", (int)$this->hls->isHlsAllowCache(),
119
            "-b:v", $rep->getKiloBitrate() . "k",
120
            "-maxrate", intval($rep->getKiloBitrate() * 1.2) . "k",
121
            "-hls_segment_type", $this->hls->getHlsSegmentType(),
122
            "-hls_fmp4_init_filename", $this->getInitFilename(),
123
            "-hls_segment_filename", $this->getSegmentFilename($rep)
124
        ];
125
    }
126
127
    /**
128
     * @param Representation $rep
129
     * @param bool $not_last
130
     */
131
    private function getArgs(Representation $rep, bool $not_last): void
132
    {
133
        $this->filter = array_merge(
134
            $this->filter,
135
            $this->initArgs($rep),
136
            $this->getAudioBitrate($rep),
137
            $this->getBaseURL(),
138
            $this->getKeyInfo(),
139
            $this->hls->getAdditionalParams(),
140
            ["-strict", $this->hls->getStrict()],
141
            $this->playlistPath($rep, $not_last)
142
        );
143
    }
144
145
    /**
146
     * set segments paths
147
     */
148
    private function segmentPaths()
149
    {
150
        if ($this->hls->getTsSubDirectory()) {
151
            File::makeDir($this->dirname . "/" . $this->hls->getTsSubDirectory() . "/");
152
        }
153
154
        $base = Utiles::appendSlash($this->hls->getHlsBaseUrl());
155
156
        $this->seg_sub_dir = Utiles::appendSlash($this->hls->getTsSubDirectory());
157
        $this->seg_filename = $this->dirname . "/" . $this->seg_sub_dir . $this->filename;
158
        $this->base_url = $base . $this->seg_sub_dir;
159
    }
160
161
    /**
162
     * set paths
163
     */
164
    private function setPaths(): void
165
    {
166
        $this->dirname = str_replace("\\", "/", $this->hls->getPathInfo(PATHINFO_DIRNAME));
167
        $this->filename = $this->hls->getPathInfo(PATHINFO_FILENAME);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->hls->getPathInfo(...ters\PATHINFO_FILENAME) can also be of type array. However, the property $filename is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
168
        $this->segmentPaths();
169
    }
170
171
    /**
172
     * @param $media
173
     * @return void
174
     */
175
    public function setFilter($media): void
176
    {
177
        $this->hls = $media;
178
        $this->setPaths();
179
180
        $reps = $this->hls->getRepresentations();
181
182
        foreach ($reps as $key => $rep) {
183
            if ($key) {
184
                $this->filter = array_merge($this->filter, $this->getFormats());
185
            }
186
187
            $this->getArgs($rep, end($reps) !== $rep);
188
        }
189
    }
190
}