Completed
Push — master ( 64cc65...e3f897 )
by Amin
03:07
created

Metadata::getVideoMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . "_" . Helper::randomString(20) . "_metadata.json";
41
        file_put_contents($filename, json_encode($metadata, JSON_PRETTY_PRINT));
42
43
        return [
44
            'filename' => $filename,
45
            'metadata' => $metadata
46
        ];
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    private function getVideoMetadata()
53
    {
54
        $probe = $this->export->getMedia()->probe();
55
        $streams = $probe['streams']->all();
56
        $format = $probe['format']->all();
57
58
        foreach ($streams as $key => $stream) {
59
            $streams[$key] = $stream->all();
60
        }
61
62
        return [
63
            'format' => $format,
64
            'streams' => $streams
65
        ];
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    private function getStreamsMetadata()
72
    {
73
        $stream_path = $this->export->getPathInfo();
74
        $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...
75
        $metadata["size_of_stream_dir"] = FileManager::directorySize($stream_path["dirname"]);
76
        $metadata["created_at"] = date("Y-m-d H:i:s");
77
78
        $metadata["qualities"] = $this->getQualities();
79
80
        $format_class = explode("\\", get_class($this->export->getFormat()));
81
        $metadata["format"] = end($format_class);
82
83
        $export_class = explode("\\", get_class($this->export));
84
        $metadata["streaming_technique"] = end($export_class);
85
86
        if ($this->export instanceof DASH) {
87
            $metadata["dash_adaption"] = $this->export->getAdaption();
88
        } elseif ($this->export instanceof HLS) {
89
            $metadata["hls_time"] = $this->export->getHlsTime();
90
            $metadata["hls_cache"] = $this->export->isHlsAllowCache();
91
            $metadata["encrypted_hls"] = (bool)$this->export->getHlsKeyInfoFile();
92
            $metadata["ts_sub_directory"] = $this->export->getTsSubDirectory();
93
            $metadata["base_url"] = $this->export->getHlsBaseUrl();
94
        }
95
96
        return $metadata;
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    private function getQualities()
103
    {
104
        $qualities = [];
105
        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

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