Passed
Push — master ( 12216b...17b30e )
by Amin
02:35
created

HLSFilter::HLSFilter()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 60
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 8
eloc 46
c 2
b 2
f 0
nc 36
nop 1
dl 0
loc 60
rs 7.9337

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\FileManager;
15
use Streaming\Helper;
16
use Streaming\HLS;
17
use Streaming\Representation;
18
19
class HLSFilter extends Filter
20
{
21
    /**
22
     * @param $media
23
     * @return mixed|void
24
     * @throws \Streaming\Exception\Exception
25
     */
26
    public function setFilter($media): void
27
    {
28
        $this->filter = $this->HLSFilter($media);
29
    }
30
31
    /**
32
     * @param HLS $media
33
     * @return array
34
     * @throws \Streaming\Exception\Exception
35
     */
36
    private function HLSFilter(HLS $media)
37
    {
38
        $filter = [];
39
        $total_count = count($representations = $media->getRepresentations());
40
        $counter = 0;
41
        $path_parts = $media->getPathInfo();
42
        $dirname = str_replace("\\", "/", $path_parts["dirname"]);
43
        $filename = substr($path_parts["filename"], -50);
44
        $ts_sub_dir = Helper::appendSlash($media->getTsSubDirectory());
45
        $base_url = Helper::appendSlash($media->getHlsBaseUrl());
46
47
        if ($ts_sub_dir) {
48
            FileManager::makeDir($dirname . DIRECTORY_SEPARATOR . $ts_sub_dir);
49
            $base_url = $base_url . $media->getTsSubDirectory() . "/";
50
        }
51
52
        foreach ($representations as $representation) {
53
            if ($representation instanceof Representation) {
54
                $filter[] = "-s:v";
55
                $filter[] = $representation->getResize();
56
                $filter[] = "-crf";
57
                $filter[] = "20";
58
                $filter[] = "-sc_threshold";
59
                $filter[] = "0";
60
                $filter[] = "-g";
61
                $filter[] = "48";
62
                $filter[] = "-keyint_min";
63
                $filter[] = "48";
64
                $filter[] = "-hls_list_size";
65
                $filter[] = "0";
66
                $filter[] = "-hls_time";
67
                $filter[] = $media->getHlsTime();
68
                $filter[] = "-hls_allow_cache";
69
                $filter[] = $media->isHlsAllowCache() ? "1" : "0";
70
                $filter[] = "-b:v";
71
                $filter[] = $representation->getKiloBitrate() . "k";
72
                $filter[] = "-maxrate";
73
                $filter[] = intval($representation->getKiloBitrate() * 1.2) . "k";
74
                $filter[] = "-hls_segment_filename";
75
                $filter[] = $dirname . "/" . $ts_sub_dir . $filename . "_" . $representation->getHeight() . "p_%04d.ts";
76
77
                if ($base_url) {
78
                    $filter[] = "-hls_base_url";
79
                    $filter[] = $base_url;
80
                }
81
82
                if ($media->getHlsKeyInfoFile()) {
83
                    $filter[] = "-hls_key_info_file";
84
                    $filter[] = $media->getHlsKeyInfoFile();
85
                }
86
87
                $filter[] = "-strict";
88
                $filter[] = $media->getStrict();
89
90
                if (++$counter !== $total_count) {
91
                    $filter[] = $dirname . "/" . $filename . "_" . $representation->getHeight() . "p.m3u8";
92
                }
93
            }
94
        }
95
        return $filter;
96
    }
97
}