Passed
Push — master ( 67f2c8...68e9c9 )
by Amin
01:53
created

HLSFilter::HLSFilter()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nc 10
nop 1
dl 0
loc 49
rs 8.7217
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
20
namespace AYazdanpanah\FFMpegStreaming\Filters;
21
22
23
use AYazdanpanah\FFMpegStreaming\HLS;
24
use AYazdanpanah\FFMpegStreaming\Representation;
25
26
class HLSFilter extends Filter
27
{
28
29
    public function setFilter($media)
30
    {
31
        $this->filter = $this->HLSFilter($media);
32
    }
33
34
    /**
35
     * @param HLS $media
36
     * @return array
37
     */
38
    private function HLSFilter(HLS $media)
39
    {
40
41
        $filter = [];
42
        $total_count = count($representations = $media->getRepresentations());
43
        $counter = 0;
44
        $path_parts = $media->getPathInfo();
45
        $dirname = str_replace("\\", "/", $path_parts["dirname"]);
46
        $filename = substr($path_parts["filename"], -50);
47
48
        foreach ($representations as $key => $representation) {
49
            if ($representation instanceof Representation) {
50
                $filter[] = "-s:v";
51
                $filter[] = $representation->getResize();
52
                $filter[] = "-crf";
53
                $filter[] = "20";
54
                $filter[] = "-sc_threshold";
55
                $filter[] = "0";
56
                $filter[] = "-g";
57
                $filter[] = "48";
58
                $filter[] = "-keyint_min";
59
                $filter[] = "48";
60
                $filter[] = "-hls_list_size";
61
                $filter[] = "0";
62
                $filter[] = "-hls_time";
63
                $filter[] = $media->getHlsTime();
64
                $filter[] = "-hls_allow_cache";
65
                $filter[] = $media->isHlsAllowCache() ? "1" : "0";
66
                $filter[] = "-b:v";
67
                $filter[] = $representation->getKiloBitrate() . "k";
68
                $filter[] = "-maxrate";
69
                $filter[] = intval($representation->getKiloBitrate() * 1.2) . "k";
70
                $filter[] = "-hls_segment_filename";
71
                $filter[] = $dirname . "/" . $filename . "_" . $representation->getHeight() . "p_%04d.ts";
72
73
                if(($hls_key_info_file = $media->getHlsKeyInfoFile()) !== ""){
74
                    $filter[] = "-hls_key_info_file";
75
                    $filter[] = $hls_key_info_file;
76
                }
77
78
                if (++$counter !== $total_count) {
79
                    $filter[] = $dirname . "/" . $filename . "_" . $representation->getHeight() . "p.m3u8";
80
                }
81
            }
82
        }
83
84
85
86
        return $filter;
87
    }
88
}