-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
550 lines (473 loc) · 13.5 KB
/
script.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
var input_vid;
var canvas;
var status;
var framerates;
var progress;
var output_video;
var download
framerates=8;//for changing video speed
function RedGenerateVideo(input_v,canvas_id,progressbar,output_v,download_link,status_div){
input_vid=document.getElementById(input_v);
canvas = document.getElementById(canvas_id);
progress = document.getElementById(progressbar);
output_video=document.getElementById(output_v);
download=document.getElementById(download_link);
var status=document.getElementById(status_div);
var cw,ch;
var back = document.createElement('canvas');
var backcontext = back.getContext('2d');
var context = canvas.getContext('2d');
input_vid.addEventListener('ended',myEndHandler,false);
input_vid.addEventListener('play',myPlayHandler,false);
input_vid.addEventListener('pause',myPauseHandler,false);
// the actual demo code, yaaay
var last_time = +new Date;
var video = new BitByte.Video(framerates);
var flag=0;
var cnt=1;
function myEndHandler(e) {
console.log("Ended");
flag=1;
}
function myPlayHandler(e) {
console.log("played");
status.innerHTML="Started...";
maxcount = (input_vid.duration * 13);//old value was 8
console.log(maxcount);
progress.setAttribute("max",maxcount);
cw = input_vid.clientWidth;
ch = input_vid.clientHeight;
canvas.width = cw;
canvas.height = ch;
back.width = cw;
back.height = ch;
generate();
// draw(input_vid,context,backcontext,cw,ch);
}
function myPauseHandler(e) {
console.log("paused");
}
function draw(v,c,bc,w,h) {
// if(v.paused) return false;
// bc.drawImage(v,0,0,w,h);
// var idata = bc.getImageData(0,0,w,h);
// c.putImageData(idata,0,0);
// setTimeout(function(){draw(v,c,bc,w,h);}, 0);
if(v.paused)
return false;
// First, draw it into the backing canvas
bc.drawImage(v,0,0,w,h);
// bc.fillStyle = "#4989c7";
// Grab the pixel data from the backing canvas
var idata = bc.getImageData(0,0,w,h);
// bc.fillStyle = "rgba(0, 0, 200, 0.5)";
var data = idata.data;
// Loop through the pixels, turning them grayscale
for(var i = 0; i < data.length; i+=2) {
var r = data[i];
var g = data[i+1];
var b = data[i+2];
var brightness = (3*r+4*g+b)>>>3;
data[i] = brightness;
data[i+1] = brightness;
data[i+2] = brightness;
}
idata.data = data;
// Draw the pixels onto the visible canvas
c.putImageData(idata,0,0);
// Start over!
setTimeout(function(){draw(v,c,bc,w,h);}, 0);
}
function generate(){
video.add(context);
progress.value++;
if(flag==0)
{
status.innerHTML = "Generating Video"+(cnt++)+" FPS";
requestAnimationFrame(generate);
}
else
{
status.innerHTML = "Compiling Video";
requestAnimationFrame(createdVideo); // well, should probably use settimeout instead
}
}
function createdVideo(){
var start_time = +new Date;
var output = video.compile();
var end_time = +new Date;
var url = webkitURL.createObjectURL(output);
// output_video.src = url; //toString converts it to a URL via Object URLs, falling back to DataURL
download.style.display = '';
download.href = url;
status.innerHTML = "Compiled Video in " + (end_time - start_time) + "ms, file size: " + Math.ceil(output.size / 1024) + "KB";
}
};
window.BitByte = (function(){
function toWebM(frames, outputAsArray){
var info = checkFrames(frames);
var CLUSTER_MAX_DURATION = 30000;
var EBML = [
{
"id": 0x1a45dfa3,
"data": [
{
"data": 1,
"id": 0x4286
},
{
"data": 1,
"id": 0x42f7
},
{
"data": 4,
"id": 0x42f2
},
{
"data": 8,
"id": 0x42f3
},
{
"data": "webm",
"id": 0x4282
},
{
"data": 2,
"id": 0x4287
},
{
"data": 2,
"id": 0x4285
}
]
},
{
"id": 0x18538067,
"data": [
{
"id": 0x1549a966,
"data": [
{
"data": 1e6,
"id": 0x2ad7b1
},
{
"data": "BitByte",
"id": 0x4d80
},
{
"data": "BitByte",
"id": 0x5741
},
{
"data": doubleToString(info.duration),
"id": 0x4489
}
]
},
{
"id": 0x1654ae6b,
"data": [
{
"id": 0xae,
"data": [
{
"data": 1,
"id": 0xd7
},
{
"data": 1,
"id": 0x63c5
},
{
"data": 0,
"id": 0x9c
},
{
"data": "und",
"id": 0x22b59c
},
{
"data": "V_VP8",
"id": 0x86
},
{
"data": "VP8",
"id": 0x258688
},
{
"data": 1,
"id": 0x83
},
{
"id": 0xe0,
"data": [
{
"data": info.width,
"id": 0xb0
},
{
"data": info.height,
"id": 0xba
}
]
}
]
}
]
},
]
}
];
var frameNumber = 0;
var clusterTimecode = 0;
while(frameNumber < frames.length){
var clusterFrames = [];
var clusterDuration = 0;
do {
clusterFrames.push(frames[frameNumber]);
clusterDuration += frames[frameNumber].duration;
frameNumber++;
}while(frameNumber < frames.length && clusterDuration < CLUSTER_MAX_DURATION);
var clusterCounter = 0;
var cluster = {
"id": 0x1f43b675,
"data": [
{
"data": clusterTimecode,
"id": 0xe7
}
].concat(clusterFrames.map(function(webp){
var block = makeSimpleBlock({
discardable: 0,
frame: webp.data.slice(4),
invisible: 0,
keyframe: 1,
lacing: 0,
trackNum: 1,
timecode: Math.round(clusterCounter)
});
clusterCounter += webp.duration;
return {
data: block,
id: 0xa3
};
}))
}
EBML[1].data.push(cluster);
clusterTimecode += clusterDuration;
}
return generateEBML(EBML, outputAsArray)
}
function checkFrames(frames){
var width = frames[0].width,
height = frames[0].height,
duration = frames[0].duration;
for(var i = 1; i < frames.length; i++){
// console.log("Frames : "+frames[i].width+" "+width);
// if(frames[i].width != width) throw "Frame " + (i + 1) + " has a different width";
// if(frames[i].height != height) throw "Frame " + (i + 1) + " has a different height";
// if(frames[i].duration < 0 || frames[i].duration > 0x7fff) throw "Frame " + (i + 1) + " has a weird duration (must be between 0 and 32767)";
duration += frames[i].duration;
}
return {
duration: duration,
width: width,
height: height
};
}
function numToBuffer(num){
var parts = [];
while(num > 0){
parts.push(num & 0xff)
num = num >> 8
}
return new Uint8Array(parts.reverse());
}
function strToBuffer(str){
var arr = new Uint8Array(str.length);
for(var i = 0; i < str.length; i++){
arr[i] = str.charCodeAt(i)
}
return arr;
}
function bitsToBuffer(bits){
var data = [];
var pad = (bits.length % 8) ? (new Array(1 + 8 - (bits.length % 8))).join('0') : '';
bits = pad + bits;
for(var i = 0; i < bits.length; i+= 8){
data.push(parseInt(bits.substr(i,8),2))
}
return new Uint8Array(data);
}
function generateEBML(json, outputAsArray){
var ebml = [];
for(var i = 0; i < json.length; i++){
var data = json[i].data;
if(typeof data == 'object') data = generateEBML(data, outputAsArray);
if(typeof data == 'number') data = bitsToBuffer(data.toString(2));
if(typeof data == 'string') data = strToBuffer(data);
if(data.length){
var z = z;
}
var len = data.size || data.byteLength || data.length;
var zeroes = Math.ceil(Math.ceil(Math.log(len)/Math.log(2))/8);
var size_str = len.toString(2);
var padded = (new Array((zeroes * 7 + 7 + 1) - size_str.length)).join('0') + size_str;
var size = (new Array(zeroes)).join('0') + '1' + padded;
ebml.push(numToBuffer(json[i].id));
ebml.push(bitsToBuffer(size));
ebml.push(data)
}
if(outputAsArray){
var buffer = toFlatArray(ebml)
return new Uint8Array(buffer);
}else{
return new Blob(ebml, {type: "video/mp4"});
}
}
function toFlatArray(arr, outBuffer){
if(outBuffer == null){
outBuffer = [];
}
for(var i = 0; i < arr.length; i++){
if(typeof arr[i] == 'object'){
toFlatArray(arr[i], outBuffer)
}else{
outBuffer.push(arr[i]);
}
}
return outBuffer;
}
function toBinStr_old(bits){
var data = '';
var pad = (bits.length % 8) ? (new Array(1 + 8 - (bits.length % 8))).join('0') : '';
bits = pad + bits;
for(var i = 0; i < bits.length; i+= 8){
data += String.fromCharCode(parseInt(bits.substr(i,8),2))
}
return data;
}
function generateEBML_old(json){
var ebml = '';
for(var i = 0; i < json.length; i++){
var data = json[i].data;
if(typeof data == 'object') data = generateEBML_old(data);
if(typeof data == 'number') data = toBinStr_old(data.toString(2));
var len = data.length;
var zeroes = Math.ceil(Math.ceil(Math.log(len)/Math.log(2))/8);
var size_str = len.toString(2);
var padded = (new Array((zeroes * 7 + 7 + 1) - size_str.length)).join('0') + size_str;
var size = (new Array(zeroes)).join('0') + '1' + padded;
ebml += toBinStr_old(json[i].id.toString(2)) + toBinStr_old(size) + data;
}
return ebml;
}
function makeSimpleBlock(data){
var flags = 0;
if (data.keyframe) flags |= 128;
if (data.invisible) flags |= 8;
if (data.lacing) flags |= (data.lacing << 1);
if (data.discardable) flags |= 1;
if (data.trackNum > 127) {
throw "TrackNumber > 127 not supported";
}
var out = [data.trackNum | 0x80, data.timecode >> 8, data.timecode & 0xff, flags].map(function(e){
return String.fromCharCode(e)
}).join('') + data.frame;
return out;
}
function parseWebP(riff){
var VP8 = riff.RIFF[0].WEBP[0];
var frame_start = VP8.indexOf('\x9d\x01\x2a'); //A VP8 keyframe starts with the 0x9d012a header
for(var i = 0, c = []; i < 4; i++) c[i] = VP8.charCodeAt(frame_start + 3 + i);
var width, horizontal_scale, height, vertical_scale, tmp;
tmp = (c[1] << 8) | c[0];
width = tmp & 0x3FFF;
horizontal_scale = tmp >> 14;
tmp = (c[3] << 8) | c[2];
height = tmp & 0x3FFF;
vertical_scale = tmp >> 14;
return {
width: width,
height: height,
data: VP8,
riff: riff
}
}
function parseRIFF(string){
var offset = 0;
var chunks = {};
while (offset < string.length) {
var id = string.substr(offset, 4);
var len = parseInt(string.substr(offset + 4, 4).split('').map(function(i){
var unpadded = i.charCodeAt(0).toString(2);
return (new Array(8 - unpadded.length + 1)).join('0') + unpadded
}).join(''),2);
var data = string.substr(offset + 4 + 4, len);
offset += 4 + 4 + len;
chunks[id] = chunks[id] || [];
if (id == 'RIFF' || id == 'LIST') {
chunks[id].push(parseRIFF(data));
} else {
chunks[id].push(data);
}
}
return chunks;
}
function doubleToString(num){
return [].slice.call(
new Uint8Array(
(
new Float64Array([num])
).buffer)
, 0)
.map(function(e){
return String.fromCharCode(e)
})
.reverse()
.join('')
}
function BitByteVideo(speed, quality){
this.frames = [];
this.duration = 1000 / speed;
this.quality = quality || 0.8;
}
BitByteVideo.prototype.add = function(frame, duration){
if(typeof duration != 'undefined' && this.duration) throw "you can't pass a duration if the fps is set";
if(typeof duration == 'undefined' && !this.duration) throw "if you don't have the fps set, you ned to have durations here."
if('canvas' in frame){
frame = frame.canvas;
}
if('toDataURL' in frame){
frame = frame.toDataURL('image/webp', this.quality)
}else if(typeof frame != "string"){
throw "frame must be a a HTMLCanvasElement, a CanvasRenderingContext2D or a DataURI formatted string"
}
if (!(/^data:image\/webp;base64,/ig).test(frame)) {
throw "Input must be formatted properly as a base64 encoded DataURI of type image/webp";
}
this.frames.push({
image: frame,
duration: duration || this.duration
})
}
BitByteVideo.prototype.compile = function(outputAsArray){
return new toWebM(this.frames.map(function(frame){
var webp = parseWebP(parseRIFF(atob(frame.image.slice(23))));
webp.duration = frame.duration;
return webp;
}), outputAsArray)
}
return {
Video: BitByteVideo,
fromImageArray: function(images, fps, outputAsArray){
return toWebM(images.map(function(image){
var webp = parseWebP(parseRIFF(atob(image.slice(23))))
webp.duration = 1000 / fps;
return webp;
}), outputAsArray)
},
toWebM: toWebM
}
})()