Passed
Push — master ( 833344...76588f )
by Amin
05:41
created

HLSSubtitle::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Streaming;
5
6
7
use Streaming\Exception\InvalidArgumentException;
8
9
class HLSSubtitle
10
{
11
    /** @const string */
12
    private const S_TAG = "#EXTM3U";
13
14
    /** @const string */
15
    private const E_TAG = "#EXT-X-ENDLIST";
16
17
    /** @const string */
18
    private const SUPPORTED_EXTS = ['vtt'];
19
20
    /** @var string */
21
    private $path;
22
23
    /** @var string */
24
    private $language_name;
25
26
    /** @var string */
27
    private $language_code;
28
29
    /** @var string */
30
    private $uri = null;
31
32
    /** @var string */
33
    private $m3u8_uri;
34
35
    /** @var bool */
36
    private $default = false;
37
38
    /** @var bool */
39
    private $auto_select = false;
40
41
    /** @var bool */
42
    private $force = false;
43
44
    /** @var int */
45
    private $hls_version = 3;
46
47
    /** @var int */
48
    private $media_sequence = 1;
49
50
    /** @var string */
51
    private $media_type = "VOD";
52
53
    /** @var string */
54
    private $group_id = "subs";
55
56
    /**
57
     * HLSSubtitle constructor.
58
     * @param string $path
59
     * @param string $language_name
60
     * @param string $language_code
61
     */
62
    public function __construct(string $path, string $language_name, string $language_code)
63
    {
64
        $this->path = $path;
65
        $this->language_name = $language_name;
66
        $this->language_code = $language_code;
67
68
        if(!in_array(pathinfo($path, PATHINFO_EXTENSION), static::SUPPORTED_EXTS)){
69
            throw new InvalidArgumentException(sprintf("Unsupported input! Only %s files are acceptable.", implode(",", static::SUPPORTED_EXTS)));
70
        }
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getPath(): string
77
    {
78
        return $this->path;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getLanguageName(): string
85
    {
86
        return $this->language_name;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getLanguageCode(): string
93
    {
94
        return $this->language_code;
95
    }
96
    /**
97
     * @return string|null
98
     */
99
    public function getUri(): string
100
    {
101
        return $this->uri ?? $this->getBaseName();
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    private function getFileName(): string
108
    {
109
        return pathinfo($this->path, PATHINFO_FILENAME);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    private function getBaseName(): string
116
    {
117
        return pathinfo($this->path, PATHINFO_BASENAME);
118
    }
119
120
    /**
121
     * @param string|null $uri
122
     */
123
    public function setUri(string $uri): void
124
    {
125
        $this->uri = $uri;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function isDefault(): bool
132
    {
133
        return $this->default;
134
    }
135
136
    /**
137
     * @param bool $default
138
     */
139
    public function default(bool $default = true): void
140
    {
141
        $this->default = $default;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function isAutoSelect(): bool
148
    {
149
        return $this->auto_select;
150
    }
151
152
    /**
153
     * @param bool $auto_select
154
     */
155
    public function autoSelect(bool $auto_select = true): void
156
    {
157
        $this->auto_select = $auto_select;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isForce(): bool
164
    {
165
        return $this->force;
166
    }
167
168
    /**
169
     * @param bool $force
170
     */
171
    public function force(bool $force = true): void
172
    {
173
        $this->force = $force;
174
    }
175
176
177
    /**
178
     * @param int $hls_version
179
     */
180
    public function setHlsVersion(int $hls_version): void
181
    {
182
        $this->hls_version = $hls_version;
183
    }
184
185
    /**
186
     * @param int $media_sequence
187
     */
188
    public function setMediaSequence(int $media_sequence): void
189
    {
190
        $this->media_sequence = $media_sequence;
191
    }
192
193
    /**
194
     * @param string $media_type
195
     */
196
    public function setMediaType(string $media_type): void
197
    {
198
        $this->media_type = $media_type;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getGroupId(): string
205
    {
206
        return $this->group_id;
207
    }
208
209
    /**
210
     * @param string $group_id
211
     */
212
    public function setGroupId(string $group_id): void
213
    {
214
        $this->group_id = $group_id;
215
    }
216
217
    /**
218
     * @return string
219
     */
220
    public function getM3u8Uri(): string
221
    {
222
        return $this->m3u8_uri;
223
    }
224
225
    /**
226
     * @param string $m3u8_uri
227
     */
228
    public function setM3u8Uri(string $m3u8_uri): void
229
    {
230
        $this->m3u8_uri = $m3u8_uri;
231
    }
232
233
    /**
234
     * @return string
235
     */
236
    public function __toString()
237
    {
238
        $s_ext = "#EXT-X-MEDIA:";
239
240
        $ext = [
241
            "TYPE"          => "SUBTITLES",
242
            "GROUP-ID"      => "\"" . $this->group_id . "\"",
243
            "NAME"          => "\"" . $this->language_name . "\"",
244
            "DEFAULT"       => Utiles::convertBooleanToYesNo($this->isDefault()),
245
            "AUTOSELECT"    => Utiles::convertBooleanToYesNo($this->isAutoSelect()),
246
            "FORCED"        => Utiles::convertBooleanToYesNo($this->isForce()),
247
            "LANGUAGE"      => "\"" . $this->language_code . "\"",
248
            "URI"           => "\"" . $this->getM3u8Uri(). "\"",
249
        ];
250
        Utiles::concatKeyValue($ext, "=");
251
252
        return $s_ext . implode(",", $ext);
253
    }
254
255
    public function generateM3U8File(string $path, float $duration, array $description = [], array $info = []): void
256
    {
257
        $ext_x = array_merge($description, [
258
            "#EXT-X-TARGETDURATION" => intval($duration),
259
            "#EXT-X-VERSION"        => $this->hls_version,
260
            "#EXT-X-MEDIA-SEQUENCE" => $this->media_sequence,
261
            "#EXT-X-PLAYLIST-TYPE"  => $this->media_type,
262
            "#EXTINF"               => implode(",", array_merge([number_format($duration, 1, '.', '')], $info))
263
        ]);
264
265
        Utiles::concatKeyValue($ext_x, ":");
266
        File::put($path, implode(PHP_EOL, array_merge([static::S_TAG], $ext_x, [$this->getUri(), static::E_TAG])));
267
268
        if(!$this->m3u8_uri){
269
            $this->setM3u8Uri(pathinfo($path, PATHINFO_BASENAME));
270
        }
271
272
        if(!$this->uri){
273
            File::copy($this->path, implode(DIRECTORY_SEPARATOR, [dirname($path), $this->getBaseName()]));
274
        }
275
    }
276
}