Passed
Push — master ( c37170...fb5355 )
by Amin
02:31
created

HLSFilter::getAudioBitrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

108
        $audio_format = $hls->getFormat()->/** @scrutinizer ignore-call */ getAudioCodec();
Loading history...
109
110
        return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format;
111
    }
112
113
    /**
114
     * @param $base_url
115
     * @return array
116
     */
117
    private function getBaseURL($base_url): array
118
    {
119
        return $base_url ? ["-hls_base_url", $base_url] : [];
120
    }
121
122
    /**
123
     * @param HLS $hls
124
     * @return array
125
     */
126
    private function getKeyInfo(HLS $hls): array
127
    {
128
        return $hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $hls->getHlsKeyInfoFile()] : [];
129
    }
130
131
    /**
132
     * @param Representation $representation
133
     * @return array
134
     */
135
    private function getAudioBitrate(Representation $representation): array
136
    {
137
        return $representation->getAudioKiloBitrate() ? ["-b:a", $representation->getAudioKiloBitrate() . "k"] : [];
138
    }
139
}