Completed
Push — master ( baa637...9b813d )
by Amin
04:08 queued 11s
created

Metadata   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 43
dl 0
loc 98
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getVideoMetadata() 0 13 2
A getStreamsMetadata() 0 26 3
A extract() 0 12 1
A getResolutions() 0 11 3
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
        $name = $this->export->getPathInfo()["filename"] . "-" . bin2hex(openssl_random_pseudo_bytes(6)) . ".json";
41
        $filename = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . $name;
42
        file_put_contents($filename, json_encode($metadata, JSON_PRETTY_PRINT));
43
44
        return [
45
            'filename' => $filename,
46
            'metadata' => $metadata
47
        ];
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    private function getVideoMetadata()
54
    {
55
        $probe = $this->export->getMedia()->probe();
56
        $streams = $probe['streams']->all();
57
        $format = $probe['format']->all();
58
59
        foreach ($streams as $key => $stream) {
60
            $streams[$key] = $stream->all();
61
        }
62
63
        return [
64
            'format' => $format,
65
            'streams' => $streams
66
        ];
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72
    private function getStreamsMetadata()
73
    {
74
        $stream_path = $this->export->getPathInfo();
75
        $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...
76
        $metadata["size_of_stream_dir"] = FileManager::directorySize($stream_path["dirname"]);
77
        $metadata["created_at"] = date("Y-m-d H:i:s");
78
79
        $metadata["resolutions"] = $this->getResolutions();
80
81
        $format_class = explode("\\", get_class($this->export->getFormat()));
82
        $metadata["format"] = end($format_class);
83
84
        $export_class = explode("\\", get_class($this->export));
85
        $metadata["streaming_technique"] = end($export_class);
86
87
        if ($this->export instanceof DASH) {
88
            $metadata["dash_adaption"] = $this->export->getAdaption();
89
        } elseif ($this->export instanceof HLS) {
90
            $metadata["hls_time"] = $this->export->getHlsTime();
91
            $metadata["hls_cache"] = $this->export->isHlsAllowCache();
92
            $metadata["encrypted_hls"] = (bool)$this->export->getHlsKeyInfoFile();
93
            $metadata["ts_sub_directory"] = $this->export->getTsSubDirectory();
94
            $metadata["base_url"] = $this->export->getHlsBaseUrl();
95
        }
96
97
        return $metadata;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    private function getResolutions()
104
    {
105
        $resolutions = [];
106
        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

106
        foreach ($this->export->/** @scrutinizer ignore-call */ getRepresentations() as $key => $representation) {
Loading history...
107
            if ($representation instanceof Representation) {
108
                $resolutions[$key]["dimension"] = strtoupper($representation->getResize());
109
                $resolutions[$key]["video_bitrate"] = $representation->getKiloBitrate() * 1024;
110
            }
111
        }
112
113
        return $resolutions;
114
    }
115
}