Passed
Push — master ( 4c970b...bd012b )
by Amin
03:15
created

Metadata   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 47
dl 0
loc 108
rs 10
c 1
b 1
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getResolutions() 0 11 3
A getVideoMetadata() 0 13 2
A getStreamsMetadata() 0 26 3
A saveAsJson() 0 7 1
A extract() 0 13 2
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();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$metadata was never initialized. Although not strictly required by PHP, it is generally a good practice to add $metadata = array(); before regardless.
Loading history...
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"];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$metadata was never initialized. Although not strictly required by PHP, it is generally a good practice to add $metadata = array(); before regardless.
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method getRepresentations() does not exist on Streaming\Export. Since it exists in all sub-types, consider adding an abstract or default implementation to Streaming\Export. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        foreach ($this->export->/** @scrutinizer ignore-call */ getRepresentations() as $key => $representation) {
Loading history...
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
}