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; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use Streaming\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
class Metadata |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Export |
22
|
|
|
*/ |
23
|
|
|
private $export; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Metadata constructor. |
27
|
|
|
* @param Export $export |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Export $export) |
30
|
|
|
{ |
31
|
|
|
$this->export = $export; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
private function getVideoMetadata(): array |
38
|
|
|
{ |
39
|
|
|
$probe = $this->export->getMedia()->probe(); |
40
|
|
|
$streams = $probe['streams']->all(); |
41
|
|
|
$format = $probe['format']->all(); |
42
|
|
|
|
43
|
|
|
foreach ($streams as $key => $stream) { |
44
|
|
|
$streams[$key] = $stream->all(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return [ |
48
|
|
|
'format' => $format, |
49
|
|
|
'streams' => $streams |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
private function getStreamsMetadata(): array |
57
|
|
|
{ |
58
|
|
|
$stream_path = $this->export->getPathInfo(); |
59
|
|
|
$filename = $stream_path["dirname"] . DIRECTORY_SEPARATOR . $stream_path["basename"]; |
60
|
|
|
$export_class = explode("\\", get_class($this->export)); |
61
|
|
|
$format_class = explode("\\", get_class($this->export->getFormat())); |
62
|
|
|
|
63
|
|
|
$metadata = [ |
64
|
|
|
"filename" => $filename, |
65
|
|
|
"size_of_stream_dir" => File::directorySize($stream_path["dirname"]), |
66
|
|
|
"created_at" => file_exists($filename) ? date("Y-m-d H:i:s", filemtime($filename)) : 'The file has been deleted', |
67
|
|
|
"resolutions" => $this->getResolutions(), |
68
|
|
|
"format" => end($format_class), |
69
|
|
|
"streaming_technique" => end($export_class) |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if ($this->export instanceof DASH) { |
73
|
|
|
$metadata = array_merge($metadata, ["seg_duration" => $this->export->getSegDuration()]); |
74
|
|
|
} elseif ($this->export instanceof HLS) { |
75
|
|
|
$metadata = array_merge( |
76
|
|
|
$metadata, |
77
|
|
|
[ |
78
|
|
|
"hls_time" => $this->export->getHlsTime(), |
79
|
|
|
"hls_cache" => (bool)$this->export->isHlsAllowCache(), |
80
|
|
|
"encrypted_hls" => (bool)$this->export->getHlsKeyInfoFile(), |
81
|
|
|
"ts_sub_directory" => $this->export->getTsSubDirectory(), |
82
|
|
|
"base_url" => $this->export->getHlsBaseUrl() |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $metadata; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function getResolutions(): array |
94
|
|
|
{ |
95
|
|
|
$resolutions = []; |
96
|
|
|
if (!method_exists($this->export, 'getRepresentations')) { |
97
|
|
|
return $resolutions; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($this->export->getRepresentations() as $representation) { |
101
|
|
|
$resolutions[] = [ |
102
|
|
|
"dimension" => strtoupper($representation->getResize()), |
103
|
|
|
"video_kilo_bitrate" => $representation->getKiloBitrate(), |
104
|
|
|
"audio_kilo_bitrate" => $representation->getAudioKiloBitrate() ?? "Not specified" |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $resolutions; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getMetadata(): array |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
|
|
"video" => $this->getVideoMetadata(), |
118
|
|
|
"streams" => $this->getStreamsMetadata() |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $filename |
124
|
|
|
* @param int $opts |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function saveAsJson(string $filename = null, int $opts = null): string |
128
|
|
|
{ |
129
|
|
|
if (is_null($filename)) { |
130
|
|
|
if ($this->export->isTmpDir()) { |
131
|
|
|
throw new InvalidArgumentException("It is a temp directory! It is not possible to save it"); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$name = uniqid(($this->export->getPathInfo()["filename"] ?? "meta") . "-") . ".json"; |
135
|
|
|
$filename = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . $name; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
file_put_contents( |
139
|
|
|
$filename, |
140
|
|
|
json_encode($this->getMetadata(), $opts ?? JSON_PRETTY_PRINT) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $filename; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string|null $save_to |
148
|
|
|
* @param int|null $opts |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function export(string $save_to = null, int $opts = null): array |
152
|
|
|
{ |
153
|
|
|
return array_merge($this->getMetadata(), ['filename' => $this->saveAsJson($save_to, $opts)]); |
154
|
|
|
} |
155
|
|
|
} |