1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mhor\MediaInfo\Runner; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
|
7
|
|
|
class MediaInfoCommandRunner |
8
|
|
|
{ |
9
|
|
|
const MEDIAINFO_COMMAND = 'mediainfo'; |
10
|
|
|
const MEDIAINFO_OLDXML_OUTPUT_ARGUMENT = '--OUTPUT=OLDXML'; |
11
|
|
|
const MEDIAINFO_XML_OUTPUT_ARGUMENT = '--OUTPUT=XML'; |
12
|
|
|
const MEDIAINFO_FULL_DISPLAY_ARGUMENT = '-f'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Process |
16
|
|
|
*/ |
17
|
|
|
protected $process; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Process $process |
21
|
|
|
*/ |
22
|
8 |
|
public function __construct(Process $process) |
23
|
|
|
{ |
24
|
8 |
|
$this->process = $process; |
25
|
8 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws \RuntimeException |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
2 |
View Code Duplication |
public function run(): string |
33
|
|
|
{ |
34
|
2 |
|
$this->process->run(); |
35
|
2 |
|
if (!$this->process->isSuccessful()) { |
36
|
1 |
|
throw new \RuntimeException($this->process->getErrorOutput()); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
return $this->process->getOutput(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Asynchronously start mediainfo operation. |
44
|
|
|
* Make call to MediaInfoCommandRunner::wait() afterwards to receive output. |
45
|
|
|
*/ |
46
|
2 |
|
public function start(): void |
47
|
|
|
{ |
48
|
|
|
// just takes advantage of symfony's underlying Process framework |
49
|
|
|
// process runs in background |
50
|
2 |
|
$this->process->start(); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Blocks until call is complete. |
55
|
|
|
* |
56
|
|
|
* @throws \Exception If this function is called before start() |
57
|
|
|
* @throws \RuntimeException |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
2 |
View Code Duplication |
public function wait(): string |
62
|
|
|
{ |
63
|
|
|
// blocks here until process completes |
64
|
2 |
|
$this->process->wait(); |
65
|
|
|
|
66
|
2 |
|
if (!$this->process->isSuccessful()) { |
67
|
1 |
|
throw new \RuntimeException($this->process->getErrorOutput()); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $this->process->getOutput(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|