-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtitles-octopus.js
executable file
·589 lines (536 loc) · 20.8 KB
/
subtitles-octopus.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
var SubtitlesOctopus = function (options) {
var supportsWebAssembly = false;
try {
if (typeof WebAssembly === "object"
&& typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
supportsWebAssembly = (new WebAssembly.Instance(module) instanceof WebAssembly.Instance);
}
} catch (e) {
}
console.log("WebAssembly support detected: " + (supportsWebAssembly ? "yes" : "no"));
var self = this;
self.canvas = options.canvas; // HTML canvas element (optional if video specified)
self.lossyRender = options.lossyRender; // Speedup render for heavy subs
self.isOurCanvas = false; // (internal) we created canvas and manage it
self.video = options.video; // HTML video element (optional if canvas specified)
self.canvasParent = null; // (internal) HTML canvas parent element
self.fonts = options.fonts || []; // Array with links to fonts used in sub (optional)
self.availableFonts = options.availableFonts || []; // Object with all available fonts (optional). Key is font name in lower case, value is link: {"arial": "/font1.ttf"}
self.onReadyEvent = options.onReady; // Function called when SubtitlesOctopus is ready (optional)
if (supportsWebAssembly) {
self.workerUrl = options.workerUrl || 'subtitles-octopus-worker.js'; // Link to WebAssembly worker
} else {
self.workerUrl = options.legacyWorkerUrl || 'subtitles-octopus-worker-legacy.js'; // Link to legacy worker
}
self.subUrl = options.subUrl; // Link to sub file (optional if subContent specified)
self.subContent = options.subContent || null; // Sub content (optional if subUrl specified)
self.onErrorEvent = options.onError; // Function called in case of critical error meaning sub wouldn't be shown and you should use alternative method (for instance it occurs if browser doesn't support web workers).
self.debug = options.debug || false; // When debug enabled, some performance info printed in console.
self.lastRenderTime = 0; // (internal) Last time we got some frame from worker
self.pixelRatio = window.devicePixelRatio || 1; // (internal) Device pixel ratio (for high dpi devices)
self.timeOffset = options.timeOffset || 0; // Time offset would be applied to currentTime from video (option)
self.hasAlphaBug = false;
(function() {
if (typeof ImageData.prototype.constructor === 'function') {
try {
// try actually calling ImageData, as on some browsers it's reported
// as existing but calling it errors out as "TypeError: Illegal constructor"
new window.ImageData(new Uint8ClampedArray([0, 0, 0, 0]), 1, 1);
return;
} catch (e) {
console.log("detected that ImageData is not constructable despite browser saying so");
}
}
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
window.ImageData = function () {
var i = 0;
if (arguments[0] instanceof Uint8ClampedArray) {
var data = arguments[i++];
}
var width = arguments[i++];
var height = arguments[i];
var imageData = ctx.createImageData(width, height);
if (data) imageData.data.set(data);
return imageData;
}
})();
self.workerError = function (error) {
console.error('Worker error: ', error);
if (self.onErrorEvent) {
self.onErrorEvent(error);
}
if (!self.debug) {
self.dispose();
throw new Error('Worker error: ' + error);
}
};
// Not tested for repeated usage yet
self.init = function () {
if (!window.Worker) {
self.workerError('worker not supported');
return;
}
// Worker
if (!self.worker) {
self.worker = new Worker(self.workerUrl);
self.worker.onmessage = self.onWorkerMessage;
self.worker.onerror = self.workerError;
}
self.workerActive = false;
self.createCanvas();
self.setVideo(options.video);
self.setSubUrl(options.subUrl);
self.worker.postMessage({
target: 'worker-init',
width: self.canvas.width,
height: self.canvas.height,
URL: document.URL,
currentScript: self.workerUrl,
preMain: true,
fastRender: self.lossyRender,
subUrl: self.subUrl,
subContent: self.subContent,
fonts: self.fonts,
availableFonts: self.availableFonts,
debug: self.debug
});
};
self.createCanvas = function () {
if (!self.canvas) {
if (self.video) {
self.isOurCanvas = true;
self.canvas = document.createElement('canvas');
self.canvas.className = 'libassjs-canvas';
self.canvas.style.display = 'none';
self.canvasParent = document.createElement('div');
self.canvasParent.className = 'libassjs-canvas-parent';
self.canvasParent.appendChild(self.canvas);
if (self.video.nextSibling) {
self.video.parentNode.insertBefore(self.canvasParent, self.video.nextSibling);
}
else {
self.video.parentNode.appendChild(self.canvasParent);
}
}
else {
if (!self.canvas) {
self.workerError('Don\'t know where to render: you should give video or canvas in options.');
}
}
}
self.ctx = self.canvas.getContext('2d');
self.bufferCanvas = document.createElement('canvas');
self.bufferCanvasCtx = self.bufferCanvas.getContext('2d');
// test for alpha bug, where e.g. WebKit can render a transparent pixel
// (with alpha == 0) as non-black which then leads to visual artifacts
self.bufferCanvas.width = 1;
self.bufferCanvas.height = 1;
var testBuf = new Uint8ClampedArray([0, 255, 0, 0]);
var testImage = new ImageData(testBuf, 1, 1);
self.bufferCanvasCtx.clearRect(0, 0, 1, 1);
self.ctx.clearRect(0, 0, 1, 1);
var prePut = self.ctx.getImageData(0, 0, 1, 1).data;
self.bufferCanvasCtx.putImageData(testImage, 0, 0);
self.ctx.drawImage(self.bufferCanvas, 0, 0);
var postPut = self.ctx.getImageData(0, 0, 1, 1).data;
self.hasAlphaBug = prePut[1] != postPut[1];
if (self.hasAlphaBug) {
console.log("Detected a browser having issue with transparent pixels, applying workaround");
}
};
self.setVideo = function (video) {
self.video = video;
if (self.video) {
var timeupdate = function () {
self.setCurrentTime(video.currentTime + self.timeOffset);
}
self.video.addEventListener("timeupdate", timeupdate, false);
self.video.addEventListener("playing", function () {
self.setIsPaused(false, video.currentTime + self.timeOffset);
}, false);
self.video.addEventListener("pause", function () {
self.setIsPaused(true, video.currentTime + self.timeOffset);
}, false);
self.video.addEventListener("seeking", function () {
self.video.removeEventListener("timeupdate", timeupdate);
}, false);
self.video.addEventListener("seeked", function () {
self.video.addEventListener("timeupdate", timeupdate, false);
self.setCurrentTime(video.currentTime + self.timeOffset);
}, false);
self.video.addEventListener("ratechange", function () {
self.setRate(video.playbackRate);
}, false);
self.video.addEventListener("timeupdate", function () {
self.setCurrentTime(video.currentTime + self.timeOffset);
}, false);
self.video.addEventListener("waiting", function () {
self.setIsPaused(true, video.currentTime + self.timeOffset);
}, false);
document.addEventListener("fullscreenchange", self.resizeWithTimeout, false);
document.addEventListener("mozfullscreenchange", self.resizeWithTimeout, false);
document.addEventListener("webkitfullscreenchange", self.resizeWithTimeout, false);
document.addEventListener("msfullscreenchange", self.resizeWithTimeout, false);
window.addEventListener("resize", self.resizeWithTimeout, false);
// Support Element Resize Observer
if (typeof ResizeObserver !== "undefined") {
self.ro = new ResizeObserver(self.resizeWithTimeout);
self.ro.observe(self.video);
}
if (self.video.videoWidth > 0) {
self.resize();
}
else {
self.video.addEventListener("loadedmetadata", function (e) {
e.target.removeEventListener(e.type, arguments.callee);
self.resize();
}, false);
}
}
};
self.getVideoPosition = function () {
var videoRatio = self.video.videoWidth / self.video.videoHeight;
var width = self.video.offsetWidth, height = self.video.offsetHeight;
var elementRatio = width / height;
var realWidth = width, realHeight = height;
if (elementRatio > videoRatio) realWidth = Math.floor(height * videoRatio);
else realHeight = Math.floor(width / videoRatio);
var x = (width - realWidth) / 2;
var y = (height - realHeight) / 2;
return {
width: realWidth,
height: realHeight,
x: x,
y: y
};
};
self.setSubUrl = function (subUrl) {
self.subUrl = subUrl;
};
self.renderFrameData = null;
function renderFrames() {
var data = self.renderFramesData;
var beforeDrawTime = performance.now();
self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
for (var i = 0; i < data.canvases.length; i++) {
var image = data.canvases[i];
self.bufferCanvas.width = image.w;
self.bufferCanvas.height = image.h;
var imageBuffer = new Uint8ClampedArray(image.buffer);
if (self.hasAlphaBug) {
for (var j = 3; j < imageBuffer.length; j = j + 4) {
imageBuffer[j] = (imageBuffer[j] >= 1) ? imageBuffer[j] : 1;
}
}
var imageData = new ImageData(imageBuffer, image.w, image.h);
self.bufferCanvasCtx.putImageData(imageData, 0, 0);
self.ctx.drawImage(self.bufferCanvas, image.x, image.y);
}
if (self.debug) {
var drawTime = Math.round(performance.now() - beforeDrawTime);
console.log(Math.round(data.spentTime) + ' ms (+ ' + drawTime + ' ms draw)');
self.renderStart = performance.now();
}
}
/**
* Lossy Render Mode
*
*/
function renderFastFrames() {
var data = self.renderFramesData;
var beforeDrawTime = performance.now();
self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
for (var i = 0; i < data.bitmaps.length; i++) {
var image = data.bitmaps[i];
self.ctx.drawImage(image.bitmap, image.x, image.y);
}
if (self.debug) {
var drawTime = Math.round(performance.now() - beforeDrawTime);
console.log(data.bitmaps.length + ' bitmaps, libass: ' + Math.round(data.libassTime) + 'ms, decode: ' + Math.round(data.decodeTime) + 'ms, draw: ' + drawTime + 'ms');
self.renderStart = performance.now();
}
}
self.workerActive = false;
self.frameId = 0;
self.onWorkerMessage = function (event) {
//dump('\nclient got ' + JSON.stringify(event.data).substr(0, 150) + '\n');
if (!self.workerActive) {
self.workerActive = true;
if (self.onReadyEvent) {
self.onReadyEvent();
}
}
var data = event.data;
switch (data.target) {
case 'stdout': {
console.log(data.content);
break;
}
case 'console-log': {
console.log.apply(console, JSON.parse(data.content));
break;
}
case 'console-debug': {
console.debug.apply(console, JSON.parse(data.content));
break;
}
case 'console-info': {
console.info.apply(console, JSON.parse(data.content));
break;
}
case 'console-warn': {
console.warn.apply(console, JSON.parse(data.content));
break;
}
case 'console-error': {
console.error.apply(console, JSON.parse(data.content));
break;
}
case 'stderr': {
console.error(data.content);
break;
}
case 'window': {
window[data.method]();
break;
}
case 'canvas': {
switch (data.op) {
case 'getContext': {
self.ctx = self.canvas.getContext(data.type, data.attributes);
break;
}
case 'resize': {
self.resize(data.width, data.height);
break;
}
case 'renderCanvas': {
if (self.lastRenderTime < data.time) {
self.lastRenderTime = data.time;
self.renderFramesData = data;
window.requestAnimationFrame(renderFrames);
}
break;
}
case 'renderFastCanvas': {
if (self.lastRenderTime < data.time) {
self.lastRenderTime = data.time;
self.renderFramesData = data;
window.requestAnimationFrame(renderFastFrames);
}
break;
}
case 'setObjectProperty': {
self.canvas[data.object][data.property] = data.value;
break;
}
default:
throw 'eh?';
}
break;
}
case 'tick': {
self.frameId = data.id;
self.worker.postMessage({
target: 'tock',
id: self.frameId
});
break;
}
case 'custom': {
if (self['onCustomMessage']) {
self['onCustomMessage'](event);
} else {
throw 'Custom message received but client onCustomMessage not implemented.';
}
break;
}
case 'setimmediate': {
self.worker.postMessage({
target: 'setimmediate'
});
break;
}
case 'get-events': {
console.log(data.target);
console.log(data.events);
break;
}
case 'get-styles': {
console.log(data.target);
console.log(data.styles);
break;
}
default:
throw 'what? ' + data.target;
}
};
self.resize = function (width, height, top, left) {
var videoSize = null;
top = top || 0;
left = left || 0;
if ((!width || !height) && self.video) {
videoSize = self.getVideoPosition();
width = videoSize.width * self.pixelRatio;
height = videoSize.height * self.pixelRatio;
var offset = self.canvasParent.getBoundingClientRect().top - self.video.getBoundingClientRect().top;
top = videoSize.y - offset;
left = videoSize.x;
}
if (!width || !height) {
if (!self.video) {
console.error('width or height is 0. You should specify width & height for resize.');
}
return;
}
if (
self.canvas.width != width ||
self.canvas.height != height ||
self.canvas.style.top != top ||
self.canvas.style.left != left
) {
self.canvas.width = width;
self.canvas.height = height;
if (videoSize != null) {
self.canvasParent.style.position = 'relative';
self.canvas.style.display = 'block';
self.canvas.style.position = 'absolute';
self.canvas.style.width = videoSize.width + 'px';
self.canvas.style.height = videoSize.height + 'px';
self.canvas.style.top = top + 'px';
self.canvas.style.left = left + 'px';
self.canvas.style.pointerEvents = 'none';
}
self.worker.postMessage({
target: 'canvas',
width: self.canvas.width,
height: self.canvas.height
});
}
};
self.resizeWithTimeout = function () {
self.resize();
setTimeout(self.resize, 100);
};
self.runBenchmark = function () {
self.worker.postMessage({
target: 'runBenchmark'
});
};
self.customMessage = function (data, options) {
options = options || {};
self.worker.postMessage({
target: 'custom',
userData: data,
preMain: options.preMain
});
};
self.setCurrentTime = function (currentTime) {
self.worker.postMessage({
target: 'video',
currentTime: currentTime
});
};
self.setTrackByUrl = function (url) {
self.worker.postMessage({
target: 'set-track-by-url',
url: url
});
};
self.setTrack = function (content) {
self.worker.postMessage({
target: 'set-track',
content: content
});
};
self.freeTrack = function (content) {
self.worker.postMessage({
target: 'free-track'
});
};
self.render = self.setCurrentTime;
self.setIsPaused = function (isPaused, currentTime) {
self.worker.postMessage({
target: 'video',
isPaused: isPaused,
currentTime: currentTime
});
};
self.setRate = function (rate) {
self.worker.postMessage({
target: 'video',
rate: rate
});
};
self.dispose = function () {
self.worker.postMessage({
target: 'destroy'
});
self.worker.terminate();
self.workerActive = false;
// Remove the canvas element to remove residual subtitles rendered on player
if (self.video) {
self.video.parentNode.removeChild(self.canvasParent);
}
};
self.createEvent = function (event) {
self.worker.postMessage({
target: 'create-event',
event: event
});
};
self.getEvents = function () {
self.worker.postMessage({
target: 'get-events'
});
};
self.setEvent = function (event, index) {
self.worker.postMessage({
target: 'set-event',
event: event,
index: index
});
};
self.removeEvent = function (index) {
self.worker.postMessage({
target: 'remove-event',
index: index
});
};
self.createStyle = function (style) {
self.worker.postMessage({
target: 'create-style',
style: style
});
};
self.getStyles = function () {
self.worker.postMessage({
target: 'get-styles'
});
};
self.setStyle = function (style, index) {
self.worker.postMessage({
target: 'set-style',
style: style,
index: index
});
};
self.removeStyle = function (index) {
self.worker.postMessage({
target: 'remove-style',
index: index
});
};
self.init();
};
if (typeof SubtitlesOctopusOnLoad == 'function') {
SubtitlesOctopusOnLoad();
}
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = SubtitlesOctopus
}
}