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