-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmp4.js
167 lines (138 loc) · 3.86 KB
/
mp4.js
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
/*
* MP4 System Layer Parser
*
* Copyright (c) 2014 Kenny.SW Lee
* Dual licensed under the MIT licenses.
* - http://www.opensource.org/licenses/mit-license.php
*
* Author: Kenny.SW Lee <[email protected]>
* Version: 0.0.1
*/
(function() {
"use strict";
function MP4(blob /* Uint8Buffer */) {
this.data = blob;
this.rootBox = new Box();
this.rootBox.type = 'isom';
this.rootJSON = {'labe':'root'};
}
MP4.prototype.parse = function() {
var bufferView = new BufferView(this.data, 0, this.data.length);
Box.parse(bufferView, this.rootBox);
};
MP4.prototype.getJSON = function(box, parent) {
if (!box || !box.boxes) {
return;
}
var obj = {};
obj.label = 'Box - ' + box.type;
parent.children = parent.children || [];
parent.children.push(obj);
for (var prop in box) {
if ((typeof box[prop]) === "function" || prop === 'boxes' || prop === 'constructor' || prop === '_parent') {
continue;
}
obj.children = obj.children || [];
var property = {'label': prop + ':' + box[prop]};
if ( Object.prototype.toString.call( box[prop] ) === '[object Array]') {
//property = {'label': prop};
//property.children = [];
property.children = [];
for (var i = 0; i < box[prop].length && box[prop][i].count; ++i) {
property.children.push({'label': i + " - count: " + box[prop][i].count + " delta: " + box[prop][i].delta});
}
for (var i = 0; i < box[prop].length && box[prop][i].first_chunk; ++i) {
property.children.push({'label': i + " - first_chunk: " + box[prop][i].first_chunk + " samples_per_chunk: " + box[prop][i].samples_per_chunk + " sample_description_index: " + box[prop][i].sample_description_index});
}
}
obj.children.push(property);
}
for (var i = 0; i < box.boxes.length; ++i) {
this.getJSON(box.boxes[i], obj);
}
return this.rootJSON;
};
MP4.prototype.getDuration = function() {
var mvhd = _getMovieHeaderBox(this.rootBox);
var duration = mvhd.duration / mvhd.timeScale;
return duration;
};
MP4.prototype.getTrackCount = function() {
var moov = _getMovieBox(this.rootBox);
var count = 0;
for (var i = 0; i < moov.boxes.length; ++i) {
if (moov.boxes[i].type === 'trak') {
count++;
}
}
return count;
};
MP4.prototype.getTrackType = function(trackIndex) {
var moov = _getMovieBox(this.rootBox);
var tkhd = null;
var curTrak = 0;
for (var i = 0; i < moov.boxes.length; ++i) {
if (moov.boxes[i].type === 'trak') {
if (curTrak == trackIndex) {
tkhd = _getBox(moov.boxes[i], 'tkhd');
break;
}
curTrak++;
}
}
return (tkhd.volume == 1)? 'Audio' : 'Video';
};
MP4.prototype.getResolution = function() {
var moov = _getMovieBox(this.rootBox);
var tkhd = null;
var curTrak = 0;
for (var i = 0; i < moov.boxes.length; ++i) {
if (moov.boxes[i].type === 'trak') {
tkhd = _getBox(moov.boxes[i], 'tkhd');
if (tkhd.volume === 0) {
break;
}
}
}
return [tkhd.width, tkhd.height];
};
MP4.prototype.getVolume = function() {
var moov = _getMovieBox(this.rootBox);
var tkhd = null;
var curTrak = 0;
for (var i = 0; i < moov.boxes.length; ++i) {
if (moov.boxes[i].type === 'trak') {
tkhd = _getBox(moov.boxes[i], 'tkhd');
if (tkhd.volume == 1) {
break;
}
}
}
return tkhd.volume;
};
// Private functions
function _getBox(box, type) {
var queueBox = [];
for (var i = 0; i < box.boxes.length; ++i) {
if (box.boxes[i].type === type) {
return box.boxes[i];
}
if (box.boxes[i].boxes.length > 0) {
queueBox.push(box.boxes[i]);
}
}
for (i = 0; i < queueBox.length; ++i) {
return _getBox(queueBox[i], type);
}
}
function _getMovieHeaderBox(box) {
var mvhd = _getBox(box, 'mvhd');
return mvhd;
}
function _getMovieBox(box) {
var moov = _getBox(box, 'moov');
return moov;
}
// Export
window.MP4 = MP4;
})();