mirror of
https://github.com/zoriya/srt-webvtt.git
synced 2025-12-06 05:56:10 +00:00
changed string concatation process
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"prefer-promise-reject-errors":[
|
||||
"off"
|
||||
],
|
||||
"symbol-description": [
|
||||
"off"
|
||||
],
|
||||
|
||||
31
index.js
31
index.js
@@ -6,25 +6,26 @@ class WebVTTConverter {
|
||||
blobToBuffer() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.addEventListener('loadend', event => {
|
||||
reader.addEventListener('loadend', (event) => {
|
||||
const buf = event.target.result;
|
||||
resolve(new Uint8Array(buf));
|
||||
});
|
||||
reader.addEventListener('error', () => reject('Error while reading the Blob object'));
|
||||
reader.readAsArrayBuffer(this.resource)
|
||||
reader.readAsArrayBuffer(this.resource);
|
||||
});
|
||||
}
|
||||
|
||||
toVTT(utf8str) {
|
||||
static toVTT(utf8str) {
|
||||
return utf8str
|
||||
.replace(/\{\\([ibu])\}/g, '</$1>')
|
||||
.replace(/\{\\([ibu])1\}/g, '<$1>')
|
||||
.replace(/\{([ibu])\}/g, '<$1>')
|
||||
.replace(/\{\/([ibu])\}/g, '</$1>')
|
||||
.replace(/(\d\d:\d\d:\d\d),(\d\d\d)/g, '$1.$2') + '\r\n\r\n';
|
||||
.replace(/\{\\([ibu])\}/g, '</$1>')
|
||||
.replace(/\{\\([ibu])1\}/g, '<$1>')
|
||||
.replace(/\{([ibu])\}/g, '<$1>')
|
||||
.replace(/\{\/([ibu])\}/g, '</$1>')
|
||||
.replace(/(\d\d:\d\d:\d\d),(\d\d\d)/g, '$1.$2')
|
||||
.concat('\r\n\r\n');
|
||||
}
|
||||
|
||||
toTypedArray(str) {
|
||||
static toTypedArray(str) {
|
||||
const result = [];
|
||||
str.split().forEach((each, index) => {
|
||||
result.push(parseInt(str.substring(index, index + 2), 16));
|
||||
@@ -37,12 +38,12 @@ class WebVTTConverter {
|
||||
if (!(this.resource instanceof Blob)) return reject('Expecting resource to be a Blob but something else found.');
|
||||
if (!(FileReader)) return reject('No FileReader constructor found');
|
||||
if (!TextDecoder) return reject('No TextDecoder constructor found');
|
||||
this.blobToBuffer()
|
||||
.then(buffer => {
|
||||
const utf8str = new TextDecoder('utf-8').decode(buffer);
|
||||
const vttString = 'WEBVTT FILE\r\n\r\n';
|
||||
console.log(this.toTypedArray(vttString.concat(utf8str)));
|
||||
});
|
||||
WebVTTConverter.blobToBuffer()
|
||||
.then((buffer) => {
|
||||
const utf8str = new TextDecoder('utf-8').decode(buffer);
|
||||
const vttString = 'WEBVTT FILE\r\n\r\n';
|
||||
console.log(WebVTTConverter.toTypedArray(vttString.concat(utf8str)));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
6025
package-lock.json
generated
6025
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,8 @@
|
||||
"description": "Convert SRT format subtitle to WebVTT on the fly over HTML5 environmentø",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "webpack-dev-server"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Reference in New Issue
Block a user