Passed
Push — master ( 220de3...37ea2e )
by Amin
13:48
created

DASHFilter::getBaseFilters()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 2
nop 2
dl 0
loc 30
rs 9.584
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
13
namespace Streaming\Filters;
14
15
16
use Streaming\DASH;
17
use Streaming\Format\X264;
18
use Streaming\Representation;
19
20
class DASHFilter extends Filter
21
{
22
    /**
23
     * @param $media
24
     */
25
    public function setFilter($media): void
26
    {
27
        $this->filter = $this->DASHFilter($media);
28
    }
29
30
    /**
31
     * @param DASH $dash
32
     * @return array
33
     */
34
    private function DASHFilter(DASH $dash): array
35
    {
36
        $filter = $this->getBaseFilters($dash, count($dash->getRepresentations()));
37
38
        foreach ($dash->getRepresentations() as $key => $representation) {
39
            $filter[] = "-map";
40
            $filter[] = "0";
41
            $filter[] = "-b:v:" . $key;
42
            $filter[] = $representation->getKiloBitrate() . "k";
43
            $filter = array_merge($filter, $this->getAudioBitrate($representation, $key));
44
45
            if (null !== $representation->getResize()) {
46
                $filter[] = "-s:v:" . $key;
47
                $filter[] = $representation->getResize();
48
            }
49
        }
50
51
        if ($dash->getAdaption()) {
52
            $filter[] = "-adaptation_sets";
53
            $filter[] = $dash->getAdaption();
54
        }
55
        $filter = array_merge($filter, $dash->getAdditionalParams());
56
        $filter = array_merge($filter, ["-strict", $dash->getStrict()]);
57
58
        return $filter;
59
    }
60
61
    /**
62
     * @param $dash
63
     * @param $count
64
     * @return array
65
     */
66
    private function getBaseFilters(DASH $dash, int $count): array
67
    {
68
        $path_parts = $dash->getPathInfo();
69
70
        $filter = [
71
            "-bf", "1",
72
            "-keyint_min", "120",
73
            "-g", "120",
74
            "-sc_threshold", "0",
75
            "-b_strategy", "0",
76
            "-use_timeline", "1",
77
            "-use_template", "1",
78
            "-init_seg_name", ($path_parts["filename"] . '_init_$RepresentationID$.$ext$'),
79
            "-media_seg_name", ($path_parts["filename"] . '_chunk_$RepresentationID$_$Number%05d$.$ext$'),
80
            "-seg_duration", $dash->getSegDuration(),
81
            "-f", "dash",
82
        ];
83
84
        if ($dash->getFormat() instanceof X264) {
85
            $filter[] = "-profile:v:0";
86
            $filter[] = "main";
87
88
            while ($count > 0) {
89
                $filter[] = "-profile:v:" . $count;
90
                $filter[] = "baseline";
91
                $count--;
92
            }
93
        }
94
95
        return $filter;
96
    }
97
98
    /**
99
     * @param Representation $rep
100
     * @param int $key
101
     * @return array
102
     */
103
    private function getAudioBitrate(Representation $rep, int $key): array
104
    {
105
        return $rep->getAudioKiloBitrate() ? ["-b:a:" . $key, $rep->getAudioKiloBitrate() . "k"] : [];
106
    }
107
}