-
Notifications
You must be signed in to change notification settings - Fork 43
/
WebMWriter.js
669 lines (566 loc) · 27.1 KB
/
WebMWriter.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**
* WebM video encoder for Google Chrome. This implementation is suitable for creating very large video files, because
* it can stream Blobs directly to a FileWriter without buffering the entire video in memory.
*
* When FileWriter is not available or not desired, it can buffer the video in memory as a series of Blobs which are
* eventually returned as one composite Blob.
*
* By Nicholas Sherlock.
*
* Based on the ideas from Whammy: https://github.com/antimatter15/whammy
*
* Released under the WTFPLv2 https://en.wikipedia.org/wiki/WTFPL
*/
"use strict";
(function() {
var WebMWriter = function(ArrayBufferDataStream, BlobBuffer) {
function extend(base, top) {
var
target = {};
[base, top].forEach(function(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
target[prop] = obj[prop];
}
}
});
return target;
}
/**
* Decode a Base64 data URL into a binary string.
*
* Returns the binary string, or false if the URL could not be decoded.
*/
function decodeBase64WebPDataURL(url) {
if (typeof url !== "string" || !url.match(/^data:image\/webp;base64,/i)) {
return false;
}
return window.atob(url.substring("data:image\/webp;base64,".length));
}
/**
* Convert a raw binary string (one character = one output byte) to an ArrayBuffer
*/
function stringToArrayBuffer(string) {
var
buffer = new ArrayBuffer(string.length),
int8Array = new Uint8Array(buffer);
for (var i = 0; i < string.length; i++) {
int8Array[i] = string.charCodeAt(i);
}
return buffer;
}
/**
* Convert the given canvas to a WebP encoded image and return the image data as a string.
*/
function renderAsWebP(canvas, quality) {
var
frame = typeof canvas === 'string' && /^data:image\/webp/.test(canvas)
? canvas
: canvas.toDataURL('image/webp', quality);
return decodeBase64WebPDataURL(frame);
}
function extractKeyframeFromWebP(webP) {
// Assume that Chrome will generate a Simple Lossy WebP which has this header:
var
keyframeStartIndex = webP.indexOf('VP8 ');
if (keyframeStartIndex == -1) {
throw "Failed to identify beginning of keyframe in WebP image";
}
// Skip the header and the 4 bytes that encode the length of the VP8 chunk
keyframeStartIndex += 'VP8 '.length + 4;
return webP.substring(keyframeStartIndex);
}
// Just a little utility so we can tag values as floats for the EBML encoder's benefit
function EBMLFloat32(value) {
this.value = value;
}
function EBMLFloat64(value) {
this.value = value;
}
/**
* Write the given EBML object to the provided ArrayBufferStream.
*
* The buffer's first byte is at bufferFileOffset inside the video file. This is used to complete offset and
* dataOffset fields in each EBML structure, indicating the file offset of the first byte of the EBML element and
* its data payload.
*/
function writeEBML(buffer, bufferFileOffset, ebml) {
// Is the ebml an array of sibling elements?
if (Array.isArray(ebml)) {
for (var i = 0; i < ebml.length; i++) {
writeEBML(buffer, bufferFileOffset, ebml[i]);
}
// Is this some sort of raw data that we want to write directly?
} else if (typeof ebml === "string") {
buffer.writeString(ebml);
} else if (ebml instanceof Uint8Array) {
buffer.writeBytes(ebml);
} else if (ebml.id){
// We're writing an EBML element
ebml.offset = buffer.pos + bufferFileOffset;
buffer.writeUnsignedIntBE(ebml.id); // ID field
// Now we need to write the size field, so we must know the payload size:
if (Array.isArray(ebml.data)) {
// Writing an array of child elements. We won't try to measure the size of the children up-front
var
sizePos, dataBegin, dataEnd;
if (ebml.size === -1) {
// Write the reserved all-one-bits marker to note that the size of this element is unknown/unbounded
buffer.writeByte(0xFF);
} else {
sizePos = buffer.pos;
/* Write a dummy size field to overwrite later. 4 bytes allows an element maximum size of 256MB,
* which should be plenty (we don't want to have to buffer that much data in memory at one time
* anyway!)
*/
buffer.writeBytes([0, 0, 0, 0]);
}
dataBegin = buffer.pos;
ebml.dataOffset = dataBegin + bufferFileOffset;
writeEBML(buffer, bufferFileOffset, ebml.data);
if (ebml.size !== -1) {
dataEnd = buffer.pos;
ebml.size = dataEnd - dataBegin;
buffer.seek(sizePos);
buffer.writeEBMLVarIntWidth(ebml.size, 4); // Size field
buffer.seek(dataEnd);
}
} else if (typeof ebml.data === "string") {
buffer.writeEBMLVarInt(ebml.data.length); // Size field
ebml.dataOffset = buffer.pos + bufferFileOffset;
buffer.writeString(ebml.data);
} else if (typeof ebml.data === "number") {
// Allow the caller to explicitly choose the size if they wish by supplying a size field
if (!ebml.size) {
ebml.size = buffer.measureUnsignedInt(ebml.data);
}
buffer.writeEBMLVarInt(ebml.size); // Size field
ebml.dataOffset = buffer.pos + bufferFileOffset;
buffer.writeUnsignedIntBE(ebml.data, ebml.size);
} else if (ebml.data instanceof EBMLFloat64) {
buffer.writeEBMLVarInt(8); // Size field
ebml.dataOffset = buffer.pos + bufferFileOffset;
buffer.writeDoubleBE(ebml.data.value);
} else if (ebml.data instanceof EBMLFloat32) {
buffer.writeEBMLVarInt(4); // Size field
ebml.dataOffset = buffer.pos + bufferFileOffset;
buffer.writeFloatBE(ebml.data.value);
} else if (ebml.data instanceof Uint8Array) {
buffer.writeEBMLVarInt(ebml.data.byteLength); // Size field
ebml.dataOffset = buffer.pos + bufferFileOffset;
buffer.writeBytes(ebml.data);
} else {
throw "Bad EBML datatype " + typeof ebml.data;
}
} else {
throw "Bad EBML datatype " + typeof ebml.data;
}
}
return function(options) {
var
MAX_CLUSTER_DURATION_MSEC = 5000,
DEFAULT_TRACK_NUMBER = 1,
writtenHeader = false,
videoWidth = 0, videoHeight = 0,
clusterFrameBuffer = [],
clusterStartTime = 0,
clusterDuration = 0,
optionDefaults = {
quality: 0.95, // WebM image quality from 0.0 (worst) to 1.0 (best)
fileWriter: null, // Chrome FileWriter in order to stream to a file instead of buffering to memory (optional)
fd: null, // Node.JS file descriptor to write to instead of buffering (optional)
// You must supply one of:
frameDuration: null, // Duration of frames in milliseconds
frameRate: null, // Number of frames per second
},
seekPoints = {
Cues: {id: new Uint8Array([0x1C, 0x53, 0xBB, 0x6B]), positionEBML: null},
SegmentInfo: {id: new Uint8Array([0x15, 0x49, 0xA9, 0x66]), positionEBML: null},
Tracks: {id: new Uint8Array([0x16, 0x54, 0xAE, 0x6B]), positionEBML: null},
},
ebmlSegment,
segmentDuration = {
"id": 0x4489, // Duration
"data": new EBMLFloat64(0)
},
seekHead,
cues = [],
blobBuffer = new BlobBuffer(options.fileWriter || options.fd);
function fileOffsetToSegmentRelative(fileOffset) {
return fileOffset - ebmlSegment.dataOffset;
}
/**
* Create a SeekHead element with descriptors for the points in the global seekPoints array.
*
* 5 bytes of position values are reserved for each node, which lie at the offset point.positionEBML.dataOffset,
* to be overwritten later.
*/
function createSeekHead() {
var
seekPositionEBMLTemplate = {
"id": 0x53AC, // SeekPosition
"size": 5, // Allows for 32GB video files
"data": 0 // We'll overwrite this when the file is complete
},
result = {
"id": 0x114D9B74, // SeekHead
"data": []
};
for (var name in seekPoints) {
var
seekPoint = seekPoints[name];
seekPoint.positionEBML = Object.create(seekPositionEBMLTemplate);
result.data.push({
"id": 0x4DBB, // Seek
"data": [
{
"id": 0x53AB, // SeekID
"data": seekPoint.id
},
seekPoint.positionEBML
]
});
}
return result;
}
/**
* Write the WebM file header to the stream.
*/
function writeHeader() {
seekHead = createSeekHead();
var
ebmlHeader = {
"id": 0x1a45dfa3, // EBML
"data": [
{
"id": 0x4286, // EBMLVersion
"data": 1
},
{
"id": 0x42f7, // EBMLReadVersion
"data": 1
},
{
"id": 0x42f2, // EBMLMaxIDLength
"data": 4
},
{
"id": 0x42f3, // EBMLMaxSizeLength
"data": 8
},
{
"id": 0x4282, // DocType
"data": "webm"
},
{
"id": 0x4287, // DocTypeVersion
"data": 2
},
{
"id": 0x4285, // DocTypeReadVersion
"data": 2
}
]
},
segmentInfo = {
"id": 0x1549a966, // Info
"data": [
{
"id": 0x2ad7b1, // TimecodeScale
"data": 1e6 // Times will be in miliseconds (1e6 nanoseconds per step = 1ms)
},
{
"id": 0x4d80, // MuxingApp
"data": "webm-writer-js",
},
{
"id": 0x5741, // WritingApp
"data": "webm-writer-js"
},
segmentDuration // To be filled in later
]
},
tracks = {
"id": 0x1654ae6b, // Tracks
"data": [
{
"id": 0xae, // TrackEntry
"data": [
{
"id": 0xd7, // TrackNumber
"data": DEFAULT_TRACK_NUMBER
},
{
"id": 0x73c5, // TrackUID
"data": DEFAULT_TRACK_NUMBER
},
{
"id": 0x9c, // FlagLacing
"data": 0
},
{
"id": 0x22b59c, // Language
"data": "und"
},
{
"id": 0x86, // CodecID
"data": "V_VP8"
},
{
"id": 0x258688, // CodecName
"data": "VP8"
},
{
"id": 0x83, // TrackType
"data": 1
},
{
"id": 0xe0, // Video
"data": [
{
"id": 0xb0, // PixelWidth
"data": videoWidth
},
{
"id": 0xba, // PixelHeight
"data": videoHeight
}
]
}
]
}
]
};
ebmlSegment = {
"id": 0x18538067, // Segment
"size": -1, // Unbounded size
"data": [
seekHead,
segmentInfo,
tracks,
]
};
var
bufferStream = new ArrayBufferDataStream(256);
writeEBML(bufferStream, blobBuffer.pos, [ebmlHeader, ebmlSegment]);
blobBuffer.write(bufferStream.getAsDataArray());
// Now we know where these top-level elements lie in the file:
seekPoints.SegmentInfo.positionEBML.data = fileOffsetToSegmentRelative(segmentInfo.offset);
seekPoints.Tracks.positionEBML.data = fileOffsetToSegmentRelative(tracks.offset);
writtenHeader = true;
}
/**
* Create a SimpleBlock keyframe header using these fields:
* timecode - Time of this keyframe
* trackNumber - Track number from 1 to 126 (inclusive)
* frame - Raw frame data payload string
*
* Returns an EBML element.
*/
function createKeyframeBlock(keyframe) {
var
bufferStream = new ArrayBufferDataStream(1 + 2 + 1);
if (!(keyframe.trackNumber > 0 && keyframe.trackNumber < 127)) {
throw "TrackNumber must be > 0 and < 127";
}
bufferStream.writeEBMLVarInt(keyframe.trackNumber); // Always 1 byte since we limit the range of trackNumber
bufferStream.writeU16BE(keyframe.timecode);
// Flags byte
bufferStream.writeByte(
1 << 7 // Keyframe
);
return {
"id": 0xA3, // SimpleBlock
"data": [
bufferStream.getAsDataArray(),
keyframe.frame
]
};
}
/**
* Create a Cluster node using these fields:
*
* timecode - Start time for the cluster
*
* Returns an EBML element.
*/
function createCluster(cluster) {
return {
"id": 0x1f43b675,
"data": [
{
"id": 0xe7, // Timecode
"data": Math.round(cluster.timecode)
}
]
};
}
function addCuePoint(trackIndex, clusterTime, clusterFileOffset) {
cues.push({
"id": 0xBB, // Cue
"data": [
{
"id": 0xB3, // CueTime
"data": clusterTime
},
{
"id": 0xB7, // CueTrackPositions
"data": [
{
"id": 0xF7, // CueTrack
"data": trackIndex
},
{
"id": 0xF1, // CueClusterPosition
"data": fileOffsetToSegmentRelative(clusterFileOffset)
}
]
}
]
});
}
/**
* Write a Cues element to the blobStream using the global `cues` array of CuePoints (use addCuePoint()).
* The seek entry for the Cues in the SeekHead is updated.
*/
function writeCues() {
var
ebml = {
"id": 0x1C53BB6B,
"data": cues
},
cuesBuffer = new ArrayBufferDataStream(16 + cues.length * 32); // Pretty crude estimate of the buffer size we'll need
writeEBML(cuesBuffer, blobBuffer.pos, ebml);
blobBuffer.write(cuesBuffer.getAsDataArray());
// Now we know where the Cues element has ended up, we can update the SeekHead
seekPoints.Cues.positionEBML.data = fileOffsetToSegmentRelative(ebml.offset);
}
/**
* Flush the frames in the current clusterFrameBuffer out to the stream as a Cluster.
*/
function flushClusterFrameBuffer() {
if (clusterFrameBuffer.length == 0) {
return;
}
// First work out how large of a buffer we need to hold the cluster data
var
rawImageSize = 0;
for (var i = 0; i < clusterFrameBuffer.length; i++) {
rawImageSize += clusterFrameBuffer[i].frame.length;
}
var
buffer = new ArrayBufferDataStream(rawImageSize + clusterFrameBuffer.length * 32), // Estimate 32 bytes per SimpleBlock header
cluster = createCluster({
timecode: Math.round(clusterStartTime),
});
for (var i = 0; i < clusterFrameBuffer.length; i++) {
cluster.data.push(createKeyframeBlock(clusterFrameBuffer[i]));
}
writeEBML(buffer, blobBuffer.pos, cluster);
blobBuffer.write(buffer.getAsDataArray());
addCuePoint(DEFAULT_TRACK_NUMBER, Math.round(clusterStartTime), cluster.offset);
clusterFrameBuffer = [];
clusterStartTime += clusterDuration;
clusterDuration = 0;
}
function validateOptions() {
// Derive frameDuration setting if not already supplied
if (!options.frameDuration) {
if (options.frameRate) {
options.frameDuration = 1000 / options.frameRate;
} else {
throw "Missing required frameDuration or frameRate setting";
}
}
}
function addFrameToCluster(frame) {
frame.trackNumber = DEFAULT_TRACK_NUMBER;
// Frame timecodes are relative to the start of their cluster:
frame.timecode = Math.round(clusterDuration);
clusterFrameBuffer.push(frame);
clusterDuration += frame.duration;
if (clusterDuration >= MAX_CLUSTER_DURATION_MSEC) {
flushClusterFrameBuffer();
}
}
/**
* Rewrites the SeekHead element that was initially written to the stream with the offsets of top level elements.
*
* Call once writing is complete (so the offset of all top level elements is known).
*/
function rewriteSeekHead() {
var
seekHeadBuffer = new ArrayBufferDataStream(seekHead.size),
oldPos = blobBuffer.pos;
// Write the rewritten SeekHead element's data payload to the stream (don't need to update the id or size)
writeEBML(seekHeadBuffer, seekHead.dataOffset, seekHead.data);
// And write that through to the file
blobBuffer.seek(seekHead.dataOffset);
blobBuffer.write(seekHeadBuffer.getAsDataArray());
blobBuffer.seek(oldPos);
}
/**
* Rewrite the Duration field of the Segment with the newly-discovered video duration.
*/
function rewriteDuration() {
var
buffer = new ArrayBufferDataStream(8),
oldPos = blobBuffer.pos;
// Rewrite the data payload (don't need to update the id or size)
buffer.writeDoubleBE(clusterStartTime);
// And write that through to the file
blobBuffer.seek(segmentDuration.dataOffset);
blobBuffer.write(buffer.getAsDataArray());
blobBuffer.seek(oldPos);
}
/**
* Add a frame to the video.
*
* @param {HTMLCanvasElement} canvas
* @param {Number} [overrideFrameDuration] - Set a duration for this frame (in milliseconds) that differs from the default
*/
this.addFrame = function(canvas, overrideFrameDuration) {
if (!writtenHeader) {
videoWidth = canvas.width || 0;
videoHeight = canvas.height || 0;
writeHeader();
}
var
webP = renderAsWebP(canvas, options.quality);
if (!webP) {
throw "Couldn't decode WebP frame, does the browser support WebP?";
}
addFrameToCluster({
frame: extractKeyframeFromWebP(webP),
duration: overrideFrameDuration === undefined ? options.frameDuration : overrideFrameDuration
});
};
/**
* Finish writing the video and return a Promise to signal completion.
*
* If the destination device was memory (i.e. options.fileWriter was not supplied), the Promise is resolved with
* a Blob with the contents of the entire video.
*/
this.complete = function() {
if (!writtenHeader) {
writeHeader();
}
flushClusterFrameBuffer();
writeCues();
rewriteSeekHead();
rewriteDuration();
return blobBuffer.complete('video/webm');
};
this.getWrittenSize = function() {
return blobBuffer.length;
};
options = extend(optionDefaults, options || {});
validateOptions();
};
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = WebMWriter(require("./ArrayBufferDataStream"), require("./BlobBuffer"));
} else {
window.WebMWriter = WebMWriter(ArrayBufferDataStream, BlobBuffer);
}
})();