-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMP4Info.php
299 lines (276 loc) · 7.64 KB
/
MP4Info.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* MP4Info
*
* @author Tommy Lacroix <[email protected]>
* @copyright Copyright (c) 2006-2009 Tommy Lacroix
* @license LGPL version 3, http://www.gnu.org/licenses/lgpl.html
* @package php-mp4info
* @link $HeadURL$
*/
// ---
/**
* MP4Info main class
*
* @author Tommy Lacroix <[email protected]>
* @version 1.1.20090611 $Id$
*/
class MP4Info {
// {{{ Audio codec types
const MP4_AUDIO_CODEC_UNCOMPRESSED = 0x00;
const MP4_AUDIO_CODEC_MP3 = 0x02;
const MP4_AUDIO_CODEC_LINEAR_PCM = 0x03;
const MP4_AUDIO_CODEC_LOG_PCM = 0x04;
const MP4_AUDIO_CODEC_AAC = 0xe0;
// }}}
// {{{ Video codec types
const MP4_VIDEO_CODEC_DVPP = 0x01;
const MP4_VIDEO_CODEC_DV5P = 0x02;
const MP4_VIDEO_CODEC_H264 = 0xe0;
// }}}
/**
* Debug mode
*
* @var bool
*/
public static $debugMode = false;
/**
* Get information from MP4 file
*
* @author Tommy Lacroix <[email protected]>
* @param string $file
* @return array
* @access public
* @static
*/
public static function getInfo($file) {
// Open file
$f = fopen($file,'rb');
if (!$f) {
throw new Exception('Cannot open file: '.$file);
}
// Get all boxes
try {
while (($box = MP4Info_Box::fromStream($f))) {
$boxes[] = $box;
}
} catch (Exception $e) { }
// Close
fclose($f);
// Return info
return self::getInfoFromBoxes($boxes);
} // getInfo method
/**
* Get information from MP4 boxes
*
* @author Tommy Lacroix <[email protected]>
* @param string $file
* @return array
* @access public
* @static
*/
public static function getInfoFromBoxes($boxes, &$context=null) {
if ($context === null) {
$context = new stdClass();
$context->hasVideo = false;
$context->hasAudio = false;
$context->video = new stdClass();
$context->audio = new stdClass();
}
foreach ($boxes as &$box) {
// Interpret box
switch ($box->getBoxTypeStr()) {
case 'hdlr':
switch ($box->getHandlerType()) {
case MP4Info_Box_hdlr::HANDLER_VIDEO:
$context->hasVideo = true;
break;
case MP4Info_Box_hdlr::HANDLER_SOUND:
$context->hasAudio = true;
break;
case chr(0x17).'Tim': // QuickTime Specific
case chr(0x18).'App': // QuickTime Specific
case chr(0x19).'App': // QuickTime Specific
break;
}
$context->creationTime = $box->getCreationTime();
$context->modificationTime = $box->getModificationTime();
break;
case 'mvhd':
$context->duration = $box->getRealDuration();
break;
case 'ilst':
if ($box->hasValue('©too')) {
$context->encoder = $box->getValue('©too');
}
break;
case 'uuid':
$meta = $box->getXMPMetaData();
if ($meta !== false) {
// Try to get duration
if (!isset($context->duration)) {
if (preg_match('/<(|[a-z]+:)duration[\s\n\r]([^>]*)>/im',$meta,$m)) {
if (preg_match_all('/xmpDM:([a-z]+)="([^"]+)"/',$m[2],$mm)) {
$value = $scale = false;
foreach ($mm[1] as $k=>$v) {
if (($v == 'value') || ($v == 'scale')) {
if (preg_match('/^1\/([0-9]+)$/',$mm[2][$k],$mmm)) {
$mm[2][$k] = 1/$mmm[1];
}
$$v = $mm[2][$k];
}
}
if (($value !== false) && ($scale !== false)) {
$context->duration = $value*$scale;
}
}
}
}
// Try to get size
if ((!isset($context->width)) || (!isset($context->height))) {
if (preg_match('/<(|[a-z]+:)videoFrameSize[\s\n\r]([^>]*)>/im',$meta,$m)) {
if (preg_match_all('/[a-z]:([a-z]+)="([^"]+)"/',$m[2],$mm)) {
$w = $h = false;
foreach ($mm[1] as $k=>$v) {
if (($v == 'w') || ($v == 'h')) {
$$v = $mm[2][$k];
}
}
if ($w != false) {
$context->video->width = $w;
$context->hasVideo = true;
}
if ($h != false) {
$context->video->height = $h;
$context->hasVideo = true;
}
}
}
}
// Try to get encoder
if (preg_match('/softwareAgent="([^"]+)"/i',$meta,$m)) {
$context->encoder = $m[1];
}
// Try to get audio channels
if (preg_match('/audioChannelType="([^"]+)"/i',$meta,$m)) {
switch (strtolower($m[1])) {
case 'stereo':
case '2':
$context->audio->channels = 2;
$context->hasAudio = true;
break;
case 'mono':
case '1':
$context->audio->channels = 1;
$context->hasAudio = true;
break;
case '5.1':
case '5':
$context->audio->channels = 5;
$context->hasAudio = true;
break;
}
}
// Try to get audio frequency
if (preg_match('/audioSampleRate="([^"]+)"/i',$meta,$m)) {
$context->audio->frequency = $m[1]/1000;
$context->hasAudio = true;
}
// Try to get video frame rate
if (preg_match('/videoFrameRate="([^"]+)"/i',$meta,$m)) {
$context->video->fps = $m[1];
$context->hasVideo = true;
}
//print htmlentities($meta);
}
break;
case 'stsd':
$values = $box->getValues();
foreach (array_keys($values) as $codec) {
switch ($codec) {
case 'sowt':
case 'twos':
case 'in24':
case 'in32':
case 'fl32':
case 'fl64':
$context->audio->codec = self::MP4_AUDIO_CODEC_LINEAR_PCM;
$context->audio->codecStr = 'Linear PCM';
$context->hasAudio = true;
break;
case 'alaw':
case 'ulaw':
$context->audio->codec = self::MP4_AUDIO_CODEC_LOG_PCM;
$context->audio->codecStr = 'Logarithmic PCM';
$context->hasAudio = true;
break;
case '.mp3':
$context->audio->codec = self::MP4_AUDIO_CODEC_MP3;
$context->audio->codecStr = 'MP3';
$context->hasAudio = true;
break;
case 'mp4a':
case 'mp4s':
$context->audio->codec = self::MP4_AUDIO_CODEC_AAC;
$context->audio->codecStr = 'AAC';
$context->hasAudio = true;
break;
case 'avc1':
case 'h264':
case 'H264':
$context->video->codec = self::MP4_VIDEO_CODEC_H264;
$context->video->codecStr = 'H.264';
$context->hasVideo = true;
break;
case 'dvpp':
$context->video->codec = self::MP4_VIDEO_CODEC_DVPP;
$context->video->codecStr = 'DVCPRO PAL';
$context->hasVideo = true;
break;
case 'dv5p':
$context->video->codec = self::MP4_VIDEO_CODEC_DV5P;
$context->video->codecStr = 'DVCPRO50';
$context->hasVideo = true;
break;
}
}
break;
case 'tkhd':
if ($box->getWidth() > 0) {
$context->hasVideo = true;
$context->video->width = $box->getWidth();
$context->video->height = $box->getHeight();
$context->hasVideo = true;
}
break;
}
// Process children
if ($box->hasChildren()) {
self::getInfoFromBoxes($box->children(), $context);
}
}
return $context;
} // getInfoFromBoxes method
/**
* Display boxes for debugging
*
* @param MP4Info_Box[] $boxes
* @param int $level
* @access public
* @static
*/
public static function displayBoxes($boxes,$level=0) {
foreach ($boxes as $box) {
print str_repeat(' ',$level*4) . $box->toString() . '<br>';
if ($box->hasChildren()) {
$this->displayBoxes($box->children(), $level+1);
}
}
} // displayBoxes method
} // MP4Info class
// ---
// {{{ Dependencies
include "MP4Info/Helper.php";
include "MP4Info/Exception.php";
include "MP4Info/Box.php";
// }}} Dependencies