|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mhor\MediaInfo\Runner; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
|
6
|
|
|
|
|
7
|
|
|
class MediaInfoCommandRunner |
|
8
|
|
|
{ |
|
9
|
|
|
const FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=OLDXML', '-f']; |
|
10
|
|
|
const XML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=XML', '-f']; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $filePath; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Process |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $process; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $command = 'mediainfo'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $arguments = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $filePath |
|
34
|
|
|
* @param string $command |
|
35
|
|
|
* @param array $arguments |
|
36
|
|
|
* @param Process $process |
|
37
|
|
|
* @param bool $forceOldXmlOutput |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
$filePath, |
|
41
|
|
|
$command = null, |
|
42
|
|
|
array $arguments = null, |
|
43
|
|
|
Process $process = null, |
|
44
|
|
|
$forceOldXmlOutput = false |
|
45
|
|
|
) { |
|
46
|
|
|
$this->filePath = $filePath; |
|
47
|
|
|
if ($command !== null) { |
|
48
|
|
|
$this->command = $command; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->arguments = self::XML_OUTPUT_FORMAT_ARGUMENTS; |
|
52
|
|
|
if ($forceOldXmlOutput) { |
|
53
|
|
|
$this->arguments = self::FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($arguments !== null) { |
|
57
|
|
|
$this->arguments = $arguments; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// /path/to/mediainfo $MEDIAINFO_VAR0 $MEDIAINFO_VAR1... |
|
61
|
|
|
// args are given through ENV vars in order to have system escape them |
|
62
|
|
|
|
|
63
|
|
|
$args = $this->arguments; |
|
64
|
|
|
array_unshift($args, $this->filePath); |
|
65
|
|
|
|
|
66
|
|
|
$env = [ |
|
67
|
|
|
'LANG' => setlocale(LC_CTYPE, 0), |
|
68
|
|
|
]; |
|
69
|
|
|
$finalCommand = [$this->command]; |
|
70
|
|
|
|
|
71
|
|
|
$i = 0; |
|
72
|
|
|
foreach ($args as $value) { |
|
73
|
|
|
$var = 'MEDIAINFO_VAR_'.$i++; |
|
74
|
|
|
$finalCommand[] = '"$'.$var.'"'; |
|
75
|
|
|
$env[$var] = $value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$finalCommandString = implode(' ', $finalCommand); |
|
79
|
|
|
|
|
80
|
|
|
if (null !== $process) { |
|
81
|
|
|
$process->setCommandLine($finalCommandString); |
|
|
|
|
|
|
82
|
|
|
$process->setEnv($env); |
|
83
|
|
|
$this->process = $process; |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->process = new Process($finalCommandString, null, $env); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws \RuntimeException |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function run() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->process->run(); |
|
97
|
|
|
if (!$this->process->isSuccessful()) { |
|
98
|
|
|
throw new \RuntimeException($this->process->getErrorOutput()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->process->getOutput(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Asynchronously start mediainfo operation. |
|
106
|
|
|
* Make call to MediaInfoCommandRunner::wait() afterwards to receive output. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function start() |
|
109
|
|
|
{ |
|
110
|
|
|
// just takes advantage of symfony's underlying Process framework |
|
111
|
|
|
// process runs in background |
|
112
|
|
|
$this->process->start(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Blocks until call is complete. |
|
117
|
|
|
* |
|
118
|
|
|
* @throws \Exception If this function is called before start() |
|
119
|
|
|
* @throws \RuntimeException |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function wait() |
|
124
|
|
|
{ |
|
125
|
|
|
if ($this->process == null) { |
|
126
|
|
|
throw new \Exception('You must run `start` before running `wait`'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// blocks here until process completes |
|
130
|
|
|
$this->process->wait(); |
|
131
|
|
|
|
|
132
|
|
|
if (!$this->process->isSuccessful()) { |
|
133
|
|
|
throw new \RuntimeException($this->process->getErrorOutput()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->process->getOutput(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.