scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of PHP-FFmpeg.
5 *
6 * (c) Alchemy <info@alchemy.fr>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace FFMpeg\FFProbe;
13  
14 use FFMpeg\FFProbe;
15 use FFMpeg\Exception\InvalidArgumentException;
16  
17 class OutputParser implements OutputParserInterface
18 {
19 /**
20 * {@inheritdoc}
21 */
22 public function parse($type, $data)
23 {
24 switch ($type) {
25 case FFProbe::TYPE_FORMAT:
26 return $this->parseFormat($data);
27 break;
28 case FFProbe::TYPE_STREAMS:
29 return $this->parseStreams($data);
30 break;
31 default:
32 throw new InvalidArgumentException(sprintf('Unknown data type %s', $type));
33 }
34 }
35  
36 private function parseFormat($data)
37 {
38 $ret = array();
39  
40 foreach (explode(PHP_EOL, $data) as $line) {
41  
42 if (in_array($line, array('[FORMAT]', '[/FORMAT]'))) {
43 continue;
44 }
45  
46 $chunks = explode('=', $line);
47 $key = array_shift($chunks);
48  
49 if ('' === trim($key)) {
50 continue;
51 }
52  
53 $value = trim(implode('=', $chunks));
54  
55 if ('nb_streams' === $key) {
56 $value = (int) $value;
57 }
58  
59 if (0 === strpos($key, 'TAG:')) {
60 if (!isset($ret['tags'])) {
61 $ret['tags'] = array();
62 }
63 $ret['tags'][substr($key, 4)] = $value;
64 } else {
65 $ret[$key] = $value;
66 }
67 }
68  
69 return array('format' => $ret);
70 }
71  
72 private function parseStreams($data)
73 {
74 $ret = array();
75 $n = -1;
76  
77 foreach (explode(PHP_EOL, $data) as $line) {
78  
79 if ($line == '[STREAM]') {
80 $n ++;
81 $ret[$n] = array();
82 continue;
83 }
84 if ($line == '[/STREAM]') {
85 continue;
86 }
87  
88 $chunks = explode('=', $line);
89 $key = array_shift($chunks);
90  
91 if ('' === trim($key)) {
92 continue;
93 }
94  
95 $value = trim(implode('=', $chunks));
96  
97 if ('N/A' === $value) {
98 continue;
99 }
100 if ('profile' === $key && 'unknown' === $value) {
101 continue;
102 }
103  
104 if (in_array($key, array('index', 'width', 'height', 'channels', 'bits_per_sample', 'has_b_frames', 'level', 'start_pts', 'duration_ts'))) {
105 $value = (int) $value;
106 }
107  
108 if (0 === strpos($key, 'TAG:')) {
109 if (!isset($ret[$n]['tags'])) {
110 $ret[$n]['tags'] = array();
111 }
112 $ret[$n]['tags'][substr($key, 4)] = $value;
113 } elseif (0 === strpos($key, 'DISPOSITION:')) {
114 if (!isset($ret[$n]['disposition'])) {
115 $ret[$n]['disposition'] = array();
116 }
117 $ret[$n]['disposition'][substr($key, 12)] = $value;
118 } else {
119 $ret[$n][$key] = $value;
120 }
121 }
122  
123 return array('streams' => $ret);
124 }
125 }