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
|
|
|
use Folour\Flavy\Exceptions\CmdException; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
abstract class Commands |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* Commands for get needed information from stdout |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
protected $_cmd = [ |
23
|
|
|
'get_formats' => [ |
24
|
|
|
'regex' => '/(?P<mux>(D\s|\sE|DE))\s(?P<format>\S{3,11})\s/', |
25
|
|
|
'cmd' => '%s -formats', |
26
|
|
|
'returns' => ['format', 'mux'] |
27
|
|
|
], |
28
|
|
|
|
29
|
|
|
'get_encoders' => [ |
30
|
|
|
'regex' => '/(?P<type>(A|V)).....\s(?P<format>\S{3,20})\s/', |
31
|
|
|
'cmd' => '%s -encoders', |
32
|
|
|
'returns' => ['type', 'format'] |
33
|
|
|
], |
34
|
|
|
|
35
|
|
|
'get_decoders' => [ |
36
|
|
|
'regex' => '/(?P<type>(A|V)).....\s(?P<format>\S{3,20})\s/', |
37
|
|
|
'cmd' => '%s -decoders', |
38
|
|
|
'returns' => ['type', 'format'] |
39
|
|
|
], |
40
|
|
|
|
41
|
|
|
'get_file_info' => [ |
42
|
|
|
'cmd' => '%s -v quiet -print_format %s -show_format -show_streams -pretty -i "%s" 2>&1' |
43
|
|
|
], |
44
|
|
|
|
45
|
|
|
'get_thumbnails' => [ |
46
|
|
|
'cmd' => '%s -i "%s" -vf fps=1/%d -vframes %d "%s"' |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* Run command and return output |
53
|
|
|
* |
54
|
|
|
* @param string $cmd shell command or key of pre-defined commands |
55
|
|
|
* @param array $args Command arguments |
56
|
|
|
* |
57
|
|
|
* @return string|bool Command output |
58
|
|
|
* @throws CmdException |
59
|
|
|
*/ |
60
|
|
|
protected function runCmd($cmd, $args = []) |
61
|
|
|
{ |
62
|
|
|
$prop = isset($this->_cmd[$cmd]) ? $this->_cmd[$cmd] : []; |
63
|
|
|
$cmd = isset($this->_cmd[$cmd]) ? $this->_cmd[$cmd]['cmd'] : $cmd; |
64
|
|
|
|
65
|
|
|
$process = new Process(vsprintf($cmd, array_values($args))); |
66
|
|
|
$process->run(); |
67
|
|
|
|
68
|
|
|
if($process->isSuccessful()) { |
69
|
|
|
$output = $process->getOutput(); |
70
|
|
|
|
71
|
|
|
if(isset($prop['regex'])) { |
72
|
|
|
if(preg_match_all($prop['regex'], $output, $matches)) { |
73
|
|
|
return array_only($matches, $prop['returns']); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $output; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new CmdException($process->getErrorOutput()); |
81
|
|
|
} |
82
|
|
|
} |