1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mhor\MediaInfo\Parser; |
4
|
|
|
|
5
|
|
|
use Mhor\MediaInfo\Builder\MediaInfoContainerBuilder; |
6
|
|
|
use Mhor\MediaInfo\Container\MediaInfoContainer; |
7
|
|
|
use Mhor\MediaInfo\Exception\MediainfoOutputParsingException; |
8
|
|
|
use Mhor\MediaInfo\Exception\UnknownTrackTypeException; |
9
|
|
|
|
10
|
|
|
class MediaInfoOutputParser extends AbstractXmlOutputParser |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $parsedOutput; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $output |
19
|
|
|
*/ |
20
|
4 |
|
public function parse($output) |
21
|
|
|
{ |
22
|
4 |
|
$this->parsedOutput = $this->transformXmlToArray($output); |
23
|
4 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The |
27
|
|
|
* default behavior (false) is throw an exception on unknown track types. |
28
|
|
|
* |
29
|
|
|
* @return MediaInfoContainer |
30
|
|
|
*/ |
31
|
5 |
|
public function getMediaInfoContainer($ignoreUnknownTrackTypes = false) |
32
|
|
|
{ |
33
|
5 |
|
if ($this->parsedOutput === null) { |
34
|
1 |
|
throw new \Exception('You must run `parse` before running `getMediaInfoContainer`'); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
$mediaInfoContainerBuilder = new MediaInfoContainerBuilder(); |
38
|
4 |
|
$mediaInfoContainerBuilder->setVersion($this->parsedOutput['@attributes']['version']); |
39
|
|
|
|
40
|
4 |
|
if (false === array_key_exists('File', $this->parsedOutput)) { |
41
|
1 |
|
throw new MediainfoOutputParsingException( |
42
|
|
|
'XML format of mediainfo >=17.10 command has changed, check php-mediainfo documentation' |
43
|
1 |
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
if(is_array($this)){ |
47
|
|
|
foreach ($this->parsedOutput['File']['track'] as $trackType) { |
48
|
|
|
try { |
49
|
|
|
if (isset($trackType['@attributes']['type'])) { |
50
|
|
|
$mediaInfoContainerBuilder->addTrackType($trackType['@attributes']['type'], $trackType); |
51
|
|
|
} |
52
|
|
|
} catch (UnknownTrackTypeException $ex) { |
53
|
|
|
if (!$ignoreUnknownTrackTypes) { |
54
|
|
|
// rethrow exception |
55
|
|
|
throw $ex; |
56
|
|
|
} |
57
|
|
|
// else ignore |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $mediaInfoContainerBuilder->build(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|