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
|
|
|
use Streaming\FFMpeg; |
13
|
|
|
use FFMpeg\Exception\ExceptionInterface; |
14
|
|
|
use Streaming\Format\HEVC; |
15
|
|
|
use Streaming\Format\X264; |
16
|
|
|
|
17
|
|
|
if (!function_exists('dash')) { |
18
|
|
|
/** |
19
|
|
|
* Auto generate dash MPD file |
20
|
|
|
* |
21
|
|
|
* @param string $input_path |
22
|
|
|
* @param callable $listener |
23
|
|
|
* @param string|null $save_path |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
function dash(string $input_path, string $save_path = null, callable $listener = null) |
27
|
|
|
{ |
28
|
|
|
$format = new HEVC(); |
29
|
|
|
|
30
|
|
|
if (is_callable($listener)) { |
31
|
|
|
$format->on('progress', $listener); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
return FFMpeg::create() |
36
|
|
|
->open($input_path) |
37
|
|
|
->DASH() |
38
|
|
|
->setFormat($format) |
39
|
|
|
->autoGenerateRepresentations() |
40
|
|
|
->setAdaption('id=0,streams=v id=1,streams=a') |
41
|
|
|
->save($save_path); |
42
|
|
|
} catch (ExceptionInterface $e) { |
43
|
|
|
return "Failed: error: " . $e->getMessage(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!function_exists('hls')) { |
49
|
|
|
/** |
50
|
|
|
* Auto generate HLS M3U8 file |
51
|
|
|
* |
52
|
|
|
* @param string $input_path |
53
|
|
|
* @param string|null $save_path |
54
|
|
|
* @param callable|null $listener |
55
|
|
|
* @param string $hls_key |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
function hls(string $input_path, string $save_path = null, callable $listener = null, $hls_key = "") |
59
|
|
|
{ |
60
|
|
|
$format = new X264(); |
61
|
|
|
|
62
|
|
|
if (is_callable($listener)) { |
63
|
|
|
$format->on('progress', $listener); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
return FFMpeg::create() |
68
|
|
|
->open($input_path) |
69
|
|
|
->HLS() |
70
|
|
|
->setFormat($format) |
71
|
|
|
->autoGenerateRepresentations() |
72
|
|
|
->setHlsKeyInfoFile($hls_key) |
73
|
|
|
->save($save_path); |
74
|
|
|
} catch (ExceptionInterface $e) { |
75
|
|
|
return "Failed: error: " . $e->getMessage(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!function_exists('encrypted_hls')) { |
81
|
|
|
/** |
82
|
|
|
* Auto generate HLS M3U8 file |
83
|
|
|
* |
84
|
|
|
* @param string $input_path |
85
|
|
|
* @param string|null $save_path |
86
|
|
|
* @param callable|null $listener |
87
|
|
|
* @param null $url |
|
|
|
|
88
|
|
|
* @param null $path |
|
|
|
|
89
|
|
|
* @param string $binary |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
function encrypted_hls(string $input_path, string $save_path = null, callable $listener = null, $url = null, $path = null, $binary = 'openssl') |
93
|
|
|
{ |
94
|
|
|
$format = new X264(); |
95
|
|
|
|
96
|
|
|
if (is_callable($listener)) { |
97
|
|
|
$format->on('progress', $listener); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
return FFMpeg::create() |
102
|
|
|
->open($input_path) |
103
|
|
|
->HLS() |
104
|
|
|
->setFormat($format) |
105
|
|
|
->autoGenerateRepresentations() |
106
|
|
|
->generateRandomKeyInfo($url, $path, $binary) |
107
|
|
|
->save($save_path); |
108
|
|
|
} catch (ExceptionInterface $e) { |
109
|
|
|
return "Failed: error: " . $e->getMessage(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |