Skip to content

Commit fe39f50

Browse files
author
yusitnikov
committed
Fix duration when section is present but invalid
1 parent d70a274 commit fe39f50

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121

2222
`ysFixWebmDuration` will parse and fix your file asynchronously and will call your callback once the result is ready.
2323

24-
If the original blob already contains duration metadata section, the callback will receive it without any changes made.
24+
If the original blob already contains duration metadata section and the duration value is not empty, the callback will receive it without any changes made.
2525

2626
Example:
2727

fix-webm-duration.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
this.source = new Uint8Array(length);
271271
for (var i = 0; i < length; i++) {
272272
var hex = this.data.substr(i * 2, 2);
273-
this.source[i] = parseInt(hex, 2);
273+
this.source[i] = parseInt(hex, 16);
274274
}
275275
};
276276
WebmUint.prototype.getValue = function() {
@@ -284,11 +284,27 @@
284284
WebmBase.call(this, name, type || 'Float');
285285
}
286286
doInherit(WebmFloat, WebmBase);
287+
WebmFloat.prototype.getFloatArrayType = function() {
288+
return this.source && this.source.length === 4 ? Float32Array : Float64Array;
289+
};
290+
WebmFloat.prototype.updateBySource = function() {
291+
var byteArray = this.source.reverse();
292+
var floatArrayType = this.getFloatArrayType();
293+
var floatArray = new floatArrayType(byteArray.buffer);
294+
this.data = floatArray[0];
295+
};
287296
WebmFloat.prototype.updateByData = function() {
288-
var floatArray = new Float64Array([ this.data ]);
297+
var floatArrayType = this.getFloatArrayType();
298+
var floatArray = new floatArrayType([ this.data ]);
289299
var byteArray = new Uint8Array(floatArray.buffer);
290300
this.source = byteArray.reverse();
291301
};
302+
WebmFloat.prototype.getValue = function() {
303+
return this.data;
304+
};
305+
WebmFloat.prototype.setValue = function(value) {
306+
this.setData(value);
307+
};
292308

293309
function WebmContainer(name, type) {
294310
WebmBase.call(this, name, type || 'Container');
@@ -411,20 +427,26 @@
411427

412428
var durationSection = infoSection.getSectionById(0x489);
413429
if (durationSection) {
414-
console.log('[fix-webm-duration] Duration section is present');
415-
return false;
430+
if (durationSection.getValue() <= 0) {
431+
console.log('[fix-webm-duration] Duration section is present, but the value is empty');
432+
durationSection.setValue(duration);
433+
} else {
434+
console.log('[fix-webm-duration] Duration section is present');
435+
return false;
436+
}
437+
} else {
438+
console.log('[fix-webm-duration] Duration section is missing');
439+
// append Duration section
440+
durationSection = new WebmFloat('Duration', 'Float');
441+
durationSection.setValue(duration);
442+
infoSection.data.push({
443+
id: 0x489,
444+
data: durationSection
445+
});
416446
}
417447

418-
console.log('[fix-webm-duration] Info section is missing');
419448
// set default time scale to 1 millisecond (1000000 nanoseconds)
420449
timeScaleSection.setValue(1000000);
421-
// append Duration section
422-
durationSection = new WebmFloat('Duration', 'Float');
423-
durationSection.setData(duration);
424-
infoSection.data.push({
425-
id: 0x489,
426-
data: durationSection
427-
});
428450
infoSection.updateByData();
429451
segmentSection.updateByData();
430452
this.updateByData();

0 commit comments

Comments
 (0)