Passed
Push — master ( f27e34...4d4fc5 )
by Amin
02:11
created

HLSFilter::getSegmentFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 4
rs 10
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\File;
15
use Streaming\HLS;
16
use Streaming\Representation;
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): array
34
    {
35
        $filter = [];
36
        $reps = $hls->getRepresentations();
37
        $path_parts = $hls->getPathInfo();
38
        $dirname = str_replace("\\", "/", $path_parts["dirname"]);
39
        list($seg_sub_dir, $base_url) = $this->getSubDirectory($hls, $dirname);
40
        $full_seg_filename = $dirname . "/" . $seg_sub_dir . $path_parts["filename"];
41
42
        foreach ($reps as $key => $rep) {
43
            if ($key) {
44
                $filter = array_merge($filter, $this->getFormats($hls));
45
            }
46
47
            $filter[] = "-s:v";
48
            $filter[] = $rep->getResize();
49
            $filter[] = "-crf";
50
            $filter[] = "20";
51
            $filter[] = "-sc_threshold";
52
            $filter[] = "0";
53
            $filter[] = "-g";
54
            $filter[] = "48";
55
            $filter[] = "-keyint_min";
56
            $filter[] = "48";
57
            $filter[] = "-hls_list_size";
58
            $filter[] = $hls->getHlsListSize();
59
            $filter[] = "-hls_time";
60
            $filter[] = $hls->getHlsTime();
61
            $filter[] = "-hls_allow_cache";
62
            $filter[] = (int)$hls->isHlsAllowCache();
63
            $filter[] = "-b:v";
64
            $filter[] = $rep->getKiloBitrate() . "k";
65
            $filter = array_merge($filter, $this->getAudioBitrate($rep));
66
            $filter[] = "-maxrate";
67
            $filter[] = intval($rep->getKiloBitrate() * 1.2) . "k";
68
            $filter[] = "-hls_segment_type";
69
            $filter[] = $hls->getHlsSegmentType();
70
            $filter[] = "-hls_fmp4_init_filename";
71
            $filter[] = $path_parts["filename"] . "_" . $hls->getHlsFmp4InitFilename();
72
            $filter[] = "-hls_segment_filename";
73
            $filter[] = $this->getSegmentFilename($full_seg_filename, $rep, $hls->getHlsSegmentType());
74
            $filter = array_merge($filter, $this->getBaseURL($base_url));
75
            $filter = array_merge($filter, $this->getKeyInfo($hls));
76
            $filter = array_merge($filter, $hls->getAdditionalParams());
77
            $filter[] = "-strict";
78
            $filter[] = $hls->getStrict();
79
80
            if (end($reps) !== $rep) {
81
                $filter[] = $dirname . "/" . $path_parts["filename"] . "_" . $rep->getHeight() . "p.m3u8";
82
            }
83
        }
84
85
        return $filter;
86
    }
87
88
    /**
89
     * @param HLS $hls
90
     * @param $dirname
91
     * @return array
92
     */
93
    private function getSubDirectory(HLS $hls, string $dirname): array
94
    {
95
        if ($hls->getTsSubDirectory()) {
96
            File::makeDir($dirname . "/" . $hls->getTsSubDirectory() . "/");
97
        }
98
99
        $base = $hls->getHlsBaseUrl() ? rtrim($hls->getHlsBaseUrl(), '/') . "/" : null;
100
        $ts = $hls->getTsSubDirectory() ? rtrim($hls->getTsSubDirectory(), '/') . "/" : null;
101
102
        return [$ts, $base . $ts];
103
    }
104
105
    /**
106
     * @param HLS $hls
107
     * @return array
108
     */
109
    private function getFormats(HLS $hls): array
110
    {
111
        $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

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

112
        $audio_format = $hls->getFormat()->/** @scrutinizer ignore-call */ getAudioCodec();
Loading history...
113
114
        return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format;
115
    }
116
117
    /**
118
     * @param $base_url
119
     * @return array
120
     */
121
    private function getBaseURL(string $base_url): array
122
    {
123
        return $base_url ? ["-hls_base_url", $base_url] : [];
124
    }
125
126
    /**
127
     * @param HLS $hls
128
     * @return array
129
     */
130
    private function getKeyInfo(HLS $hls): array
131
    {
132
        return $hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $hls->getHlsKeyInfoFile()] : [];
133
    }
134
135
    /**
136
     * @param Representation $rep
137
     * @return array
138
     */
139
    private function getAudioBitrate(Representation $rep): array
140
    {
141
        return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : [];
142
    }
143
144
    /**
145
     * @param string $full_seg_filename
146
     * @param Representation $rep
147
     * @param string $type
148
     * @return string
149
     */
150
    private function getSegmentFilename(string $full_seg_filename, Representation $rep, string $type): string
151
    {
152
        $ext = ($type === "fmp4") ? "m4s" : "ts";
153
        return $full_seg_filename . "_" . $rep->getHeight() . "p_%04d." . $ext;
154
    }
155
}