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->getAdditionalFilters($dash->getFormat(), 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
|
|
|
|
56
|
|
|
return $filter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $format |
61
|
|
|
* @param $count |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
private function getAdditionalFilters($format, $count): array |
65
|
|
|
{ |
66
|
|
|
$filter = [ |
67
|
|
|
"-bf", "1", "-keyint_min", "120", "-g", "120", |
68
|
|
|
"-sc_threshold", "0", "-b_strategy", "0", "-strict", "-2", |
69
|
|
|
"-use_timeline", "1", "-use_template", "1", "-f", "dash" |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if ($format instanceof X264) { |
73
|
|
|
$filter[] = "-profile:v:0"; |
74
|
|
|
$filter[] = "main"; |
75
|
|
|
|
76
|
|
|
while ($count > 0) { |
77
|
|
|
$filter[] = "-profile:v:" . $count; |
78
|
|
|
$filter[] = "baseline"; |
79
|
|
|
$count--; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $filter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Representation $rep |
88
|
|
|
* @param int $key |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private function getAudioBitrate(Representation $rep, int $key): array |
92
|
|
|
{ |
93
|
|
|
return $rep->getAudioKiloBitrate() ? ["-b:a:" . $key, $rep->getAudioKiloBitrate() . "k"] : []; |
94
|
|
|
} |
95
|
|
|
} |