Skip to content

Commit

Permalink
feat: parse edts boxes (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
nklhtv committed Mar 5, 2021
1 parent 4285042 commit 989bffd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
36 changes: 36 additions & 0 deletions lib/tools/mp4-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
'use strict';

var MAX_UINT32 = Math.pow(2, 32);

var
inspectMp4,
textifyMp4,
Expand Down Expand Up @@ -123,6 +125,40 @@ var
avgBitrate: view.getUint32(8)
};
},
edts: function edts(data) {
return {
boxes: inspectMp4(data)
};
},
elst: function elst(data) {
var
view = new DataView(data.buffer, data.byteOffset, data.byteLength),
result = {
version: view.getUint8(0),
flags: new Uint8Array(data.subarray(1, 4)),
edits: []
},
entryCount = view.getUint32(4),
i;
for (i = 8; entryCount; entryCount--) {
if (result.version === 0) {
result.edits.push({
segmentDuration: view.getUint32(i),
mediaTime: view.getInt32(i + 4),
mediaRate: view.getUint16(i + 8) + view.getUint16(i + 10) / (256 * 256)
});
i += 12;
} else {
result.edits.push({
segmentDuration: (view.getUint32(i) * MAX_UINT32) + view.getUint32(i + 4),
mediaTime: (view.getUint32(i + 8) * MAX_UINT32) + view.getUint32(i + 12),
mediaRate: view.getUint16(i + 16) + view.getUint16(i + 18) / (256 * 256)
});
i += 20;
}
}
return result;
},
esds: function(data) {
return {
version: data[0],
Expand Down
34 changes: 31 additions & 3 deletions test/mp4-inspector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ QUnit.test('can parse a moov', function(assert) {
var result = QUnit.dump.parse(mp4.tools.inspect(new Uint8Array(data)));
var expected = QUnit.dump.parse([{
type: 'moov',
size: 981,
size: 1061,
boxes: [{
type: 'mvhd',
version: 1,
Expand All @@ -231,7 +231,7 @@ QUnit.test('can parse a moov', function(assert) {
nextTrackId: 2
}, {
type: 'trak',
size: 439,
size: 475,
boxes: [{
type: 'tkhd',
flags: new Uint8Array([0, 0, 0]),
Expand All @@ -247,6 +247,20 @@ QUnit.test('can parse a moov', function(assert) {
matrix: new Uint32Array(unityMatrix),
width: 300,
height: 150
}, {
type: 'edts',
size: 36,
boxes: [{
type: 'elst',
size: 28,
version: 0,
flags: new Uint8Array([0, 0, 0]),
edits: [{
segmentDuration: 0,
mediaTime: 1024,
mediaRate: 1.5
}]
}]
}, {
type: 'mdia',
size: 327,
Expand Down Expand Up @@ -349,7 +363,7 @@ QUnit.test('can parse a moov', function(assert) {
}]
}, {
type: 'trak',
size: 414,
size: 458,
boxes: [{
type: 'tkhd',
flags: new Uint8Array([0, 0, 0]),
Expand All @@ -365,6 +379,20 @@ QUnit.test('can parse a moov', function(assert) {
matrix: new Uint32Array(unityMatrix),
width: 300,
height: 150
}, {
type: 'edts',
size: 44,
boxes: [{
type: 'elst',
size: 36,
version: 1,
flags: new Uint8Array([0, 0, 0]),
edits: [{
segmentDuration: 0,
mediaTime: 1152921504606847000,
mediaRate: 1.5
}]
}]
}, {
type: 'mdia',
size: 302,
Expand Down
16 changes: 16 additions & 0 deletions test/utils/mp4-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ module.exports.sampleMoov =
unityMatrix,
0x01, 0x2c, 0x00, 0x00, // 300 in 16.16 fixed-point
0x00, 0x96, 0x00, 0x00), // 150 in 16.16 fixed-point
box('edts',
box('elst',
0x00, // version
0x00, 0x00, 0x00, // flags
0x00, 0x00, 0x00, 0x01, // entry_count
0x00, 0x00, 0x00, 0x00, // segment_duration
0x00, 0x00, 0x04, 0x00, // media_time
0x00, 0x01, 0x80, 0x00)), // media_rate
box('mdia',
box('mdhd',
0x01, // version 1
Expand Down Expand Up @@ -208,6 +216,14 @@ module.exports.sampleMoov =
unityMatrix,
0x01, 0x2c, 0x00, 0x00, // 300 in 16.16 fixed-point
0x00, 0x96, 0x00, 0x00), // 150 in 16.16 fixed-point
box('edts',
box('elst',
0x01, // version
0x00, 0x00, 0x00, // flags
0x00, 0x00, 0x00, 0x01, // entry_count
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // segment_duration
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // media_time
0x00, 0x01, 0x80, 0x00)), // media_rate
box('mdia',
box('mdhd',
0x01, // version 1
Expand Down

0 comments on commit 989bffd

Please sign in to comment.