Passed
Push — master ( 4991cf...c37170 )
by Amin
02:21
created

HLSFilter::getBaseURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Utilities;
17
18
class HLSFilter extends Filter
19
{
20
    /**
21
     * @param $media
22
     * @return mixed|void
23
     */
24
    public function setFilter($media): void
25
    {
26
        $this->filter = $this->HLSFilter($media);
27
    }
28
29
    /**
30
     * @param HLS $hls
31
     * @return array
32
     */
33
    private function HLSFilter(HLS $hls)
34
    {
35
        $filter = [];
36
        $representations = $hls->getRepresentations();
37
        $path_parts = $hls->getPathInfo();
38
        $dirname = str_replace("\\", "/", $path_parts["dirname"]);
39
        list($ts_sub_dir, $base_url) = $this->getSubDirectory($hls, $dirname);
40
41
        foreach ($representations as $key => $representation) {
42
            if ($key) {
43
                $filter = array_merge($filter, $this->getFormats($hls));
44
            }
45
46
            $filter[] = "-s:v";
47
            $filter[] = $representation->getResize();
48
            $filter[] = "-crf";
49
            $filter[] = "20";
50
            $filter[] = "-sc_threshold";
51
            $filter[] = "0";
52
            $filter[] = "-g";
53
            $filter[] = "48";
54
            $filter[] = "-keyint_min";
55
            $filter[] = "48";
56
            $filter[] = "-hls_list_size";
57
            $filter[] = "0";
58
            $filter[] = "-hls_time";
59
            $filter[] = $hls->getHlsTime();
60
            $filter[] = "-hls_allow_cache";
61
            $filter[] = (int)$hls->isHlsAllowCache();
62
            $filter[] = "-b:v";
63
            $filter[] = $representation->getKiloBitrate() . "k";
64
            $filter[] = "-maxrate";
65
            $filter[] = intval($representation->getKiloBitrate() * 1.2) . "k";
66
            $filter[] = "-hls_segment_filename";
67
            $filter[] = $dirname . "/" . $ts_sub_dir . $path_parts["filename"] . "_" . $representation->getHeight() . "p_%04d.ts";
68
            $filter = array_merge($filter, $this->getBaseURL($base_url));
69
            $filter = array_merge($filter, $this->getKeyInfo($hls));
70
            $filter[] = "-strict";
71
            $filter[] = $hls->getStrict();
72
73
            if (end($representations) !== $representation) {
74
                $filter[] = $dirname . "/" . $path_parts["filename"] . "_" . $representation->getHeight() . "p.m3u8";
75
            }
76
        }
77
78
        return $filter;
79
    }
80
81
    /**
82
     * @param HLS $hls
83
     * @param $dirname
84
     * @return array
85
     */
86
    private function getSubDirectory(HLS $hls, $dirname)
87
    {
88
        $ts_sub_dir = Utilities::appendSlash($hls->getTsSubDirectory());
89
        $base_url = Utilities::appendSlash($hls->getHlsBaseUrl());
90
91
        if ($ts_sub_dir) {
92
            FileManager::makeDir($dirname . DIRECTORY_SEPARATOR . $ts_sub_dir);
93
            $base_url = $base_url . $hls->getTsSubDirectory() . "/";
94
        }
95
96
        return [$ts_sub_dir, $base_url];
97
    }
98
99
    private function getFormats(HLS $hls)
100
    {
101
        $format = ['-c:v', $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

101
        $format = ['-c:v', $hls->getFormat()->/** @scrutinizer ignore-call */ getVideoCodec()];
Loading history...
102
103
        $audio_format = $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

103
        $audio_format = $hls->getFormat()->/** @scrutinizer ignore-call */ getAudioCodec();
Loading history...
104
        if ($audio_format) {
105
            $format = array_merge($format, ['-c:a', $audio_format]);
106
        }
107
108
        return $format;
109
    }
110
111
    private function getBaseURL($base_url)
112
    {
113
        $filter = [];
114
115
        if ($base_url) {
116
            $filter[] = "-hls_base_url";
117
            $filter[] = $base_url;
118
        }
119
120
        return $filter;
121
    }
122
123
    private function getKeyInfo(HLS $hls)
124
    {
125
        $filter = [];
126
127
        if ($hls->getHlsKeyInfoFile()) {
128
            $filter[] = "-hls_key_info_file";
129
            $filter[] = $hls->getHlsKeyInfoFile();
130
        }
131
132
        return $filter;
133
    }
134
}