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
|
|
|
class Metadata |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Export |
20
|
|
|
*/ |
21
|
|
|
private $export; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Metadata constructor. |
25
|
|
|
* @param Export $export |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Export $export) |
28
|
|
|
{ |
29
|
|
|
$this->export = $export; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function extract() |
36
|
|
|
{ |
37
|
|
|
$metadata["video"] = $this->getVideoMetadata(); |
|
|
|
|
38
|
|
|
$metadata["streams"] = $this->getStreamsMetadata(); |
39
|
|
|
|
40
|
|
|
$filename = 'It is not possible to save metadata to clouds.'; |
41
|
|
|
if(!$this->export->isTmpDir()){ |
42
|
|
|
$filename = $this->saveAsJson($metadata); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return [ |
46
|
|
|
'filename' => $filename, |
47
|
|
|
'metadata' => $metadata |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
private function getVideoMetadata() |
55
|
|
|
{ |
56
|
|
|
$probe = $this->export->getMedia()->probe(); |
57
|
|
|
$streams = $probe['streams']->all(); |
58
|
|
|
$format = $probe['format']->all(); |
59
|
|
|
|
60
|
|
|
foreach ($streams as $key => $stream) { |
61
|
|
|
$streams[$key] = $stream->all(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return [ |
65
|
|
|
'format' => $format, |
66
|
|
|
'streams' => $streams |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
private function getStreamsMetadata() |
74
|
|
|
{ |
75
|
|
|
$stream_path = $this->export->getPathInfo(); |
76
|
|
|
$metadata["filename"] = $stream_path["dirname"] . DIRECTORY_SEPARATOR . $stream_path["basename"]; |
|
|
|
|
77
|
|
|
$metadata["size_of_stream_dir"] = FileManager::directorySize($stream_path["dirname"]); |
78
|
|
|
$metadata["created_at"] = date("Y-m-d H:i:s"); |
79
|
|
|
|
80
|
|
|
$metadata["resolutions"] = $this->getResolutions(); |
81
|
|
|
|
82
|
|
|
$format_class = explode("\\", get_class($this->export->getFormat())); |
83
|
|
|
$metadata["format"] = end($format_class); |
84
|
|
|
|
85
|
|
|
$export_class = explode("\\", get_class($this->export)); |
86
|
|
|
$metadata["streaming_technique"] = end($export_class); |
87
|
|
|
|
88
|
|
|
if ($this->export instanceof DASH) { |
89
|
|
|
$metadata["dash_adaption"] = $this->export->getAdaption(); |
90
|
|
|
} elseif ($this->export instanceof HLS) { |
91
|
|
|
$metadata["hls_time"] = $this->export->getHlsTime(); |
92
|
|
|
$metadata["hls_cache"] = $this->export->isHlsAllowCache(); |
93
|
|
|
$metadata["encrypted_hls"] = (bool)$this->export->getHlsKeyInfoFile(); |
94
|
|
|
$metadata["ts_sub_directory"] = $this->export->getTsSubDirectory(); |
95
|
|
|
$metadata["base_url"] = $this->export->getHlsBaseUrl(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $metadata; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
private function getResolutions() |
105
|
|
|
{ |
106
|
|
|
$resolutions = []; |
107
|
|
|
foreach ($this->export->getRepresentations() as $key => $representation) { |
|
|
|
|
108
|
|
|
if ($representation instanceof Representation) { |
109
|
|
|
$resolutions[$key]["dimension"] = strtoupper($representation->getResize()); |
110
|
|
|
$resolutions[$key]["video_bitrate"] = $representation->getKiloBitrate() * 1024; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $resolutions; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function saveAsJson($metadata) |
118
|
|
|
{ |
119
|
|
|
$name = $this->export->getPathInfo()["filename"] . "-" . bin2hex(openssl_random_pseudo_bytes(6)) . ".json"; |
120
|
|
|
$filename = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . $name; |
121
|
|
|
file_put_contents($filename, json_encode($metadata, JSON_PRETTY_PRINT)); |
122
|
|
|
|
123
|
|
|
return $filename; |
124
|
|
|
} |
125
|
|
|
} |