Completed
Push — master ( e18231...8c2386 )
by Amin
02:57
created

StreamingAnalytics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 1
b 1
f 1
nc 1
nop 2
dl 0
loc 4
rs 10
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 Streaming\MediaInfo\MediaInfo;
17
18
class StreamingAnalytics
19
{
20
    /**
21
     * @var Export
22
     */
23
    private $export;
24
    /**
25
     * @var string
26
     */
27
    private $mediaInfoBinary;
28
29
    /**
30
     * StreamingAnalytics constructor.
31
     * @param Export $export
32
     * @param string $binary
33
     */
34
    public function __construct(Export $export, string $binary)
35
    {
36
        $this->export = $export;
37
        $this->mediaInfoBinary = $binary;
38
    }
39
40
    /**
41
     * @return mixed
42
     * @throws Exception\Exception
43
     */
44
    public function analyse()
45
    {
46
        $metadata["original"] = $this->getOriginalMetadata();
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...
47
        $metadata["streams"] = $this->getStreamsMetadata();
48
        $metadata["general"] = $this->getGeneralMetadata();
49
50
        $filename = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . "analyse.json";
51
        file_put_contents($filename, json_encode($metadata));
52
53
        return $metadata;
54
    }
55
56
    /**
57
     * @return mixed
58
     * @throws Exception\Exception
59
     */
60
    private function getOriginalMetadata()
61
    {
62
        $media_info = MediaInfo::initialize($this->export->getMedia()->getPath(), $this->mediaInfoBinary);
63
64
        $streams = $media_info->all();
65
66
        foreach ($streams as $key => $stream){
67
            $streams[$key] = $stream->all();
68
        }
69
70
        return $streams;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    private function getStreamsMetadata()
77
    {
78
        $metadata["qualities"] = $this->getQualities();
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...
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"] = ($this->export->getHlsKeyInfoFile() !== "") ? true : false;
92
        }
93
94
        return $metadata;
95
    }
96
97
    private function getQualities()
98
    {
99
        $qualities = [];
100
        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

100
        foreach ($this->export->/** @scrutinizer ignore-call */ getRepresentations() as $key => $representation) {
Loading history...
101
            if ($representation instanceof Representation) {
102
                $qualities[$key]["dimensions"] = strtoupper($representation->getResize());
103
                $qualities[$key]["kilobitrate"] = $representation->getKiloBitrate();
104
            }
105
        }
106
107
        return $qualities;
108
    }
109
110
    private function getGeneralMetadata()
111
    {
112
        $video_path = $this->export->getMedia()->getPath();
113
        $metadata["path_to_video"] = $video_path;
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...
114
        $metadata["dir_path_to_video"] = pathinfo($video_path)["dirname"];
115
        $metadata["basename_of_video"] = pathinfo($video_path)["basename"];
116
        $metadata["extension_of_video"] = pathinfo($video_path)["extension"];
117
        $metadata["mime_content_type_of_video"] = !is_file($video_path)?:mime_content_type($video_path);
118
        $metadata["size_of_video"] = !is_file($video_path)?:filesize($video_path);
119
120
        $stream_path = $this->export->getPathInfo();
121
        $metadata["dir_path_to_stream"] = $stream_path["dirname"];
122
        $metadata["path_to_stream"] = $stream_path["dirname"] . DIRECTORY_SEPARATOR . $stream_path["basename"];
123
        $metadata["size_of_stream_dir"] = Helper::directorySize($stream_path["dirname"]);
124
        $metadata["datetime"] = date("Y-m-d H:i:s");
125
        $metadata["time"] = time();
126
127
        return $metadata;
128
    }
129
}