Completed
Push — master ( 0eb1a8...12216b )
by Amin
03:20
created

HLSFilter::HLSFilter()   F

Complexity

Conditions 12
Paths 576

Size

Total Lines 60
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 12
eloc 46
c 2
b 2
f 0
nc 576
nop 1
dl 0
loc 60
rs 3.3888

How to fix   Long Method    Complexity   

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