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\Representation; |
17
|
|
|
|
18
|
|
|
class HLSFilter extends Filter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param $media |
22
|
|
|
* @return mixed|void |
23
|
|
|
* @throws \Streaming\Exception\Exception |
24
|
|
|
*/ |
25
|
|
|
public function setFilter($media): void |
26
|
|
|
{ |
27
|
|
|
$this->filter = $this->HLSFilter($media); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param HLS $media |
32
|
|
|
* @return array |
33
|
|
|
* @throws \Streaming\Exception\Exception |
34
|
|
|
*/ |
35
|
|
|
private function HLSFilter(HLS $media) |
36
|
|
|
{ |
37
|
|
|
$filter = []; |
38
|
|
|
$total_count = count($representations = $media->getRepresentations()); |
39
|
|
|
$counter = 0; |
40
|
|
|
$path_parts = $media->getPathInfo(); |
41
|
|
|
$dirname = str_replace("\\", "/", $path_parts["dirname"]); |
42
|
|
|
$filename = substr($path_parts["filename"], -50); |
43
|
|
|
$ts_sub_dir = ($media->getTsSubDirectory() && substr($media->getTsSubDirectory(), -1) !== "/") ? $media->getTsSubDirectory() . "/" : $media->getTsSubDirectory(); |
44
|
|
|
$base_url = ($media->getHlsBaseUrl() && substr($media->getHlsBaseUrl(), -1) !== "/") ? $media->getHlsBaseUrl() . "/" : $media->getHlsBaseUrl(); |
45
|
|
|
|
46
|
|
|
if ($ts_sub_dir) { |
47
|
|
|
FileManager::makeDir($dirname . DIRECTORY_SEPARATOR . $ts_sub_dir); |
48
|
|
|
$base_url = $base_url . $media->getTsSubDirectory() . "/"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($representations as $representation) { |
52
|
|
|
if ($representation instanceof Representation) { |
53
|
|
|
$filter[] = "-s:v"; |
54
|
|
|
$filter[] = $representation->getResize(); |
55
|
|
|
$filter[] = "-crf"; |
56
|
|
|
$filter[] = "20"; |
57
|
|
|
$filter[] = "-sc_threshold"; |
58
|
|
|
$filter[] = "0"; |
59
|
|
|
$filter[] = "-g"; |
60
|
|
|
$filter[] = "48"; |
61
|
|
|
$filter[] = "-keyint_min"; |
62
|
|
|
$filter[] = "48"; |
63
|
|
|
$filter[] = "-hls_list_size"; |
64
|
|
|
$filter[] = "0"; |
65
|
|
|
$filter[] = "-hls_time"; |
66
|
|
|
$filter[] = $media->getHlsTime(); |
67
|
|
|
$filter[] = "-hls_allow_cache"; |
68
|
|
|
$filter[] = $media->isHlsAllowCache() ? "1" : "0"; |
69
|
|
|
$filter[] = "-b:v"; |
70
|
|
|
$filter[] = $representation->getKiloBitrate() . "k"; |
71
|
|
|
$filter[] = "-maxrate"; |
72
|
|
|
$filter[] = intval($representation->getKiloBitrate() * 1.2) . "k"; |
73
|
|
|
$filter[] = "-hls_segment_filename"; |
74
|
|
|
$filter[] = $dirname . "/" . $ts_sub_dir . $filename . "_" . $representation->getHeight() . "p_%04d.ts"; |
75
|
|
|
|
76
|
|
|
if ($base_url) { |
77
|
|
|
$filter[] = "-hls_base_url"; |
78
|
|
|
$filter[] = $base_url; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($media->getHlsKeyInfoFile()) { |
82
|
|
|
$filter[] = "-hls_key_info_file"; |
83
|
|
|
$filter[] = $media->getHlsKeyInfoFile(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$filter[] = "-strict"; |
87
|
|
|
$filter[] = $media->getStrict(); |
88
|
|
|
|
89
|
|
|
if (++$counter !== $total_count) { |
90
|
|
|
$filter[] = $dirname . "/" . $filename . "_" . $representation->getHeight() . "p.m3u8"; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return $filter; |
95
|
|
|
} |
96
|
|
|
} |