1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License; |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* https://opensource.org/licenses/MIT |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use AYazdanpanah\FFMpegStreaming\FFMpeg; |
20
|
|
|
use FFMpeg\Exception\ExceptionInterface; |
21
|
|
|
use AYazdanpanah\FFMpegStreaming\Format\HEVC; |
22
|
|
|
use AYazdanpanah\FFMpegStreaming\Format\X264; |
23
|
|
|
|
24
|
|
|
if (! function_exists('dash')) { |
25
|
|
|
/** |
26
|
|
|
* Auto generate dash MPD file |
27
|
|
|
* |
28
|
|
|
* @param string $input_path |
29
|
|
|
* @param callable $listener |
30
|
|
|
* @param string|null $save_path |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
function dash(string $input_path, string $save_path = null, callable $listener = null) |
34
|
|
|
{ |
35
|
|
|
$format = new HEVC(); |
36
|
|
|
|
37
|
|
|
if (is_callable($listener)) { |
38
|
|
|
$format->on('progress', $listener); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
return FFMpeg::create() |
|
|
|
|
43
|
|
|
->open($input_path) |
44
|
|
|
->DASH() |
45
|
|
|
->setFormat($format) |
46
|
|
|
->autoGenerateRepresentations() |
47
|
|
|
->setAdaption('id=0,streams=v id=1,streams=a') |
48
|
|
|
->save($save_path); |
49
|
|
|
} catch (ExceptionInterface $e) { |
50
|
|
|
return "Failed: error: " . $e->getMessage(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (! function_exists('hls')) { |
56
|
|
|
/** |
57
|
|
|
* Auto generate HLS M3U8 file |
58
|
|
|
* |
59
|
|
|
* @param string $input_path |
60
|
|
|
* @param callable|null $listener |
61
|
|
|
* @param string|null $save_path |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
function hls(string $input_path, string $save_path = null, callable $listener = null) |
65
|
|
|
{ |
66
|
|
|
$format = new X264(); |
67
|
|
|
|
68
|
|
|
if (is_callable($listener)) { |
69
|
|
|
$format->on('progress', $listener); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
return FFMpeg::create() |
|
|
|
|
74
|
|
|
->open($input_path) |
75
|
|
|
->HLS() |
76
|
|
|
->setFormat($format) |
77
|
|
|
->autoGenerateRepresentations() |
78
|
|
|
->save($save_path); |
79
|
|
|
} catch (ExceptionInterface $e) { |
80
|
|
|
return "Failed: error: " . $e->getMessage(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |