Base::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Folour\Flavy\Extensions;
2
3
/**
4
 *
5
 * @author Vadim Bova <[email protected]>
6
 * @link   http://github.com/folour | http://vk.com/folour
7
 *
8
 */
9
10
class Base extends Commands
11
{
12
13
    /**
14
     *
15
     * Flavy config
16
     *
17
     * @var array
18
     */
19
    protected $config = [
20
        'ffmpeg_path'   => 'ffmpeg',
21
        'ffprobe_path'  => 'ffprobe'
22
    ];
23
24
    /**
25
     *
26
     * FFmpeg information
27
     *
28
     * @var array
29
     */
30
    private $_info = [
31
        'formats'   => [],
32
33
        'encoders'  => [
34
            'audio' => [],
35
            'video' => []
36
        ],
37
38
        'decoders'  => [
39
            'audio' => [],
40
            'video' => []
41
        ]
42
    ];
43
44
    /**
45
     * Base constructor.
46
     *
47
     * @param array $config
48
     */
49
    public function __construct(array $config)
50
    {
51
        $this->config = array_replace($this->config, $config);
52
    }
53
54
    /**
55
     *
56
     * Returns array of supported formats
57
     *
58
     * @return array
59
     */
60
    public function formats()
61
    {
62
        if($this->_info['formats'] === null) {
63
            $data = $this->runCmd('get_formats', [$this->config['ffmpeg_path']]);
64
            if(is_array($data)) {
65
                $this->_info['formats'] = array_combine($data['format'], $data['mux']);
66
            }
67
        }
68
69
        return $this->_info['formats'];
70
    }
71
72
    /**
73
     *
74
     * Returns array of audio and video encoders
75
     * [
76
     *     'audio' => [],
77
     *     'video' => []
78
     * ]
79
     *
80
     * @return array
81
     */
82 View Code Duplication
    public function encoders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if($this->_info['encoders']['audio'] === []) {
85
            $data = $this->runCmd('get_encoders', [$this->config['ffmpeg_path']]);
86
            if(is_array($data)) {
87
                foreach($data['type'] as $key => $type) {
88
                    $this->_info['encoders'][($type == 'A' ? 'audio' : 'video')][] = $data['format'][$key];
89
                }
90
            }
91
        }
92
93
        return $this->_info['encoders'];
94
    }
95
96
    /**
97
     *
98
     * Returns array of audio and video decoders
99
     * [
100
     *     'audio' => [],
101
     *     'video' => []
102
     * ]
103
     *
104
     * @return array
105
     */
106 View Code Duplication
    public function decoders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        if($this->_info['decoders']['audio'] === []) {
109
            $data = $this->runCmd('get_decoders', [$this->config['ffmpeg_path']]);
110
            if(is_array($data)) {
111
                foreach($data['type'] as $key => $type) {
112
                    $this->_info['decoders'][($type == 'A' ? 'audio' : 'video')][] = $data['format'][$key];
113
                }
114
            }
115
        }
116
117
        return $this->_info['decoders'];
118
    }
119
120
    /**
121
     * @param string $format
122
     * @return bool
123
     */
124
    public function canEncode($format)
125
    {
126
        return in_array($format, array_flatten($this->encoders()));
127
    }
128
129
    /**
130
     * @param string $format
131
     * @return bool
132
     */
133
    public function canDecode($format)
134
    {
135
        return in_array($format, array_flatten($this->decoders()));
136
    }
137
138
    //Helpers
139
140
    /**
141
     * @param int|string $time timestamp for conversion
142
     * @param bool $isDate mode flag, if true - $time converts from hh::mm:ss string to seconds, else conversely
143
     * @return string
144
     */
145
    protected function timestamp($time, $isDate = true)
146
    {
147
        if($isDate) {
148
            $time = explode(':', $time);
149
150
            return ($time[0] * 3600) + ($time[1] * 60) + (ceil($time[2]));
151
        }
152
153
        return gmdate('H:i:s', mktime(0, 0, $time));
154
    }
155
}