|
1
|
|
|
<?php namespace Folour\Flavy\Extensions; |
|
2
|
|
|
|
|
3
|
|
|
use Folour\Flavy\Exceptions\CmdException; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
* @author Vadim Bova <[email protected]> |
|
8
|
|
|
* @link http://github.com/folour | http://vk.com/folour |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
class Base extends Commands |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* Flavy config |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $config = [ |
|
21
|
|
|
'ffmpeg_path' => 'ffmpeg', |
|
22
|
|
|
'ffprobe_path' => 'ffprobe' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* FFmpeg information |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_info = [ |
|
32
|
|
|
'formats' => [], |
|
33
|
|
|
|
|
34
|
|
|
'encoders' => [ |
|
35
|
|
|
'audio' => [], |
|
36
|
|
|
'video' => [] |
|
37
|
|
|
], |
|
38
|
|
|
|
|
39
|
|
|
'decoders' => [ |
|
40
|
|
|
'audio' => [], |
|
41
|
|
|
'video' => [] |
|
42
|
|
|
] |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Base constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $config |
|
49
|
|
|
*/ |
|
50
|
12 |
|
public function __construct(array $config) |
|
51
|
|
|
{ |
|
52
|
12 |
|
$this->config = array_replace($this->config, $config); |
|
53
|
12 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* Returns array of supported formats |
|
58
|
|
|
* |
|
59
|
|
|
* @throws CmdException |
|
60
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
61
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function formats() |
|
65
|
|
|
{ |
|
66
|
1 |
|
if ($this->_info['formats'] === null) { |
|
67
|
|
|
$data = $this->runCmd('get_formats', [$this->config['ffmpeg_path']]); |
|
68
|
|
|
if (is_array($data)) { |
|
69
|
|
|
$this->_info['formats'] = array_combine($data['format'], $data['mux']); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $this->_info['formats']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* Returns array of audio and video encoders |
|
79
|
|
|
* [ |
|
80
|
|
|
* 'audio' => [], |
|
81
|
|
|
* 'video' => [] |
|
82
|
|
|
* ] |
|
83
|
|
|
* |
|
84
|
|
|
* @throws CmdException |
|
85
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
86
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function encoders() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->_info['encoders']['audio'] === [] ? $this->infoPrepare(true) : $this->_info['encoders']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param bool $encoders |
|
96
|
|
|
* @return array |
|
97
|
|
|
* @throws CmdException |
|
98
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
99
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
100
|
|
|
*/ |
|
101
|
1 |
|
private function infoPrepare($encoders = false) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$data = $this->runCmd('get_' . ($encoders ? 'encoders' : 'decoders'), [$this->config['ffmpeg_path']]); |
|
104
|
1 |
|
return array_map( |
|
105
|
1 |
|
function ($key, $type) use ($data) { |
|
106
|
1 |
|
$result = []; |
|
107
|
1 |
|
return $result[$type === 'A' ? 'audio' : 'video'][] = $data['format'][$key]; |
|
108
|
1 |
|
}, array_keys($data['type']), $data['type'] |
|
109
|
1 |
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* Returns array of audio and video decoders |
|
115
|
|
|
* [ |
|
116
|
|
|
* 'audio' => [], |
|
117
|
|
|
* 'video' => [] |
|
118
|
|
|
* ] |
|
119
|
|
|
* |
|
120
|
|
|
* @throws CmdException |
|
121
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
122
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function decoders() |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->_info['decoders']['audio'] === [] ? $this->infoPrepare(false) : $this->_info['decoders']; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $format |
|
132
|
|
|
* @return bool |
|
133
|
|
|
* @throws CmdException |
|
134
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
135
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function canEncode($format = 'encoder') |
|
138
|
|
|
{ |
|
139
|
1 |
|
return in_array($format, array_flatten($this->encoders()), true); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $format |
|
144
|
|
|
* @return bool |
|
145
|
|
|
* @throws CmdException |
|
146
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
147
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function canDecode($format = 'decoder') |
|
150
|
|
|
{ |
|
151
|
1 |
|
return in_array($format, array_flatten($this->decoders()), true); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
//Helpers |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param int|string $time timestamp for conversion |
|
158
|
|
|
* @param bool $isDate mode flag, if true - $time converts from hh::mm:ss string to seconds, else conversely |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
1 |
|
protected function timestamp($time, $isDate = true) |
|
162
|
|
|
{ |
|
163
|
1 |
|
if ($isDate) { |
|
164
|
1 |
|
$time = explode(':', $time); |
|
165
|
|
|
|
|
166
|
1 |
|
return ($time[0] * 3600) + ($time[1] * 60) + (int)$time[2]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return gmdate('H:i:s', mktime(0, 0, $time)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|