@sub37/webvtt-adapter 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Adapter.js +86 -8
- package/lib/InvalidFormatError.d.ts +1 -1
- package/package.json +4 -4
package/lib/Adapter.js
CHANGED
|
@@ -3,7 +3,9 @@ import { EmptyStyleDeclarationError } from "./EmptyStyleDeclarationError.js";
|
|
|
3
3
|
import { InvalidFormatError } from "./InvalidFormatError.js";
|
|
4
4
|
import { MissingContentError } from "@sub37/adapter-utils/MissingContentError";
|
|
5
5
|
import * as Parser from "./Parser/index.js";
|
|
6
|
-
|
|
6
|
+
import { parseMs } from "./Parser/Timestamps.utils.js";
|
|
7
|
+
const WEBVTT_HEADER_SECTION = /^(?:[\uFEFF\n\s]*)?WEBVTT/;
|
|
8
|
+
const X_TIMESTAMP_MAP_REGEX = /^X-TIMESTAMP-MAP=([^,\r\n]*),\s*([^,\r\n]*)/;
|
|
7
9
|
const BLOCK_MATCH_REGEX = /(?<blocktype>(?:REGION|STYLE|NOTE))[\s\r\n]*(?<payload>[\w\W]*)/;
|
|
8
10
|
const CUE_MATCH_REGEX = /(?:(?<cueid>[^\n\t]*)\s+)?(?<starttime>(?:(?:\d\d:)?(?:\d\d:)(?:\d\d)\.\d{3}))\s-->\s(?<endtime>(?:(?:\d\d:)?(?:\d\d:)(?:\d\d)\.\d{3}))\s*?(?:(?<attributes>[^\r\n]*?)\s*)[\r\n]+\s*(?<text>(?:.+\s*)+)/;
|
|
9
11
|
const TABS_REGEX = /\t+/g;
|
|
@@ -35,6 +37,7 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
35
37
|
const cueIdsList = new Set();
|
|
36
38
|
const cues = [];
|
|
37
39
|
const content = String(rawContent).replace(/\r?\n/g, "\n");
|
|
40
|
+
let cuesOffsetMs = 0;
|
|
38
41
|
const block = {
|
|
39
42
|
start: 0,
|
|
40
43
|
cursor: 0,
|
|
@@ -76,6 +79,12 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
76
79
|
* According to WebVTT standard, Region and style blocks should be
|
|
77
80
|
* placed above the cues. So we try to ignore them if they are mixed.
|
|
78
81
|
*/
|
|
82
|
+
if (isHeader(blockEvaluationResult) && latestBlockPhase & BlockType.HEADER) {
|
|
83
|
+
const [, payload] = blockEvaluationResult;
|
|
84
|
+
if (payload.offsetMs) {
|
|
85
|
+
cuesOffsetMs = payload.offsetMs;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
79
88
|
const shouldProcessNonCues = latestBlockPhase & (BlockType.REGION | BlockType.STYLE | BlockType.HEADER);
|
|
80
89
|
if (isRegion(blockEvaluationResult) && shouldProcessNonCues) {
|
|
81
90
|
const [blockType, parsedContent] = blockEvaluationResult;
|
|
@@ -154,8 +163,8 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
154
163
|
}
|
|
155
164
|
const cue = CueNode.from(latestRootCue, {
|
|
156
165
|
id: parsedCue.id || `cue-${block.start}-${block.cursor}`,
|
|
157
|
-
startTime: parsedCue.startTime,
|
|
158
|
-
endTime: parsedCue.endTime,
|
|
166
|
+
startTime: parsedCue.startTime + cuesOffsetMs,
|
|
167
|
+
endTime: parsedCue.endTime + cuesOffsetMs,
|
|
159
168
|
content: parsedCue.text,
|
|
160
169
|
renderingModifiers: parsedCue.renderingModifiers,
|
|
161
170
|
});
|
|
@@ -263,14 +272,77 @@ export default class WebVTTAdapter extends BaseAdapter {
|
|
|
263
272
|
}
|
|
264
273
|
}
|
|
265
274
|
function evaluateBlock(content, start, end) {
|
|
275
|
+
const contentSection = content.substring(start, end);
|
|
266
276
|
if (start === 0) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
277
|
+
const headerLines = contentSection.split("\n");
|
|
278
|
+
let webvttHeaderFound = false;
|
|
279
|
+
let offsetMs = 0;
|
|
280
|
+
for (const line of headerLines) {
|
|
281
|
+
const trimmedLine = line.trim();
|
|
282
|
+
if (!trimmedLine.length) {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* @TODO From down here, error handling might be improved by creating one
|
|
287
|
+
* error type per issue.
|
|
288
|
+
*/
|
|
289
|
+
if (!webvttHeaderFound && !WEBVTT_HEADER_SECTION.test(trimmedLine)) {
|
|
290
|
+
throw new InvalidFormatError("WEBVTT_HEADER_MISSING", content.substring(start, end));
|
|
291
|
+
}
|
|
292
|
+
webvttHeaderFound = true;
|
|
293
|
+
const xTimestampMapMatch = trimmedLine.match(X_TIMESTAMP_MAP_REGEX);
|
|
294
|
+
if (xTimestampMapMatch !== null) {
|
|
295
|
+
const [, itemOne, itemTwo] = xTimestampMapMatch;
|
|
296
|
+
if (!itemOne || !itemTwo) {
|
|
297
|
+
return new InvalidFormatError("WEBVTT_HEADER_X_TIMESTAMP_MAP_INVALID", content.substring(start, end));
|
|
298
|
+
}
|
|
299
|
+
let localTime = undefined;
|
|
300
|
+
let mpegtsTime = undefined;
|
|
301
|
+
for (const item of [itemOne, itemTwo]) {
|
|
302
|
+
/**
|
|
303
|
+
* @TODO when we'll have a way to report non-critical errors here,
|
|
304
|
+
* we should report an error when the key is not LOCAL or MPEGTS or
|
|
305
|
+
* when the offsetMs has already been set because of duplicated key.
|
|
306
|
+
*
|
|
307
|
+
* if (offsetMs > 0) {
|
|
308
|
+
* ...
|
|
309
|
+
* }
|
|
310
|
+
*/
|
|
311
|
+
const firstColonIndex = item.indexOf(":");
|
|
312
|
+
const key = item.substring(0, firstColonIndex).trim();
|
|
313
|
+
const value = item.substring(firstColonIndex + 1).trim();
|
|
314
|
+
if (!value) {
|
|
315
|
+
return new InvalidFormatError("WEBVTT_HEADER_X_TIMESTAMP_MAP_INVALID", content.substring(start, end));
|
|
316
|
+
}
|
|
317
|
+
if (key === "LOCAL") {
|
|
318
|
+
try {
|
|
319
|
+
localTime = parseMs(value);
|
|
320
|
+
}
|
|
321
|
+
catch (err) {
|
|
322
|
+
/**
|
|
323
|
+
* @TODO this should be treated as non-critical error but we don't have a way to report it yet.
|
|
324
|
+
* For now, we just set the localTime to 0 and continue.
|
|
325
|
+
*/
|
|
326
|
+
localTime = 0;
|
|
327
|
+
}
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (key === "MPEGTS") {
|
|
331
|
+
mpegtsTime = parseInt(value, 10);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (!isXTimestampMapValueValue(localTime) || !isXTimestampMapValueValue(mpegtsTime)) {
|
|
336
|
+
return new InvalidFormatError("WEBVTT_HEADER_X_TIMESTAMP_MAP_INVALID", content.substring(start, end));
|
|
337
|
+
}
|
|
338
|
+
offsetMs = (mpegtsTime / 90000) * 1000 - localTime;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (!webvttHeaderFound) {
|
|
342
|
+
return new InvalidFormatError("WEBVTT_HEADER_MISSING", content.substring(start, end));
|
|
270
343
|
}
|
|
271
|
-
return [BlockType.HEADER,
|
|
344
|
+
return [BlockType.HEADER, { offsetMs }];
|
|
272
345
|
}
|
|
273
|
-
const contentSection = content.substring(start, end);
|
|
274
346
|
const blockMatch = contentSection.match(BLOCK_MATCH_REGEX);
|
|
275
347
|
if (blockMatch?.groups["blocktype"]) {
|
|
276
348
|
switch (blockMatch.groups["blocktype"]) {
|
|
@@ -314,6 +386,9 @@ function evaluateBlock(content, start, end) {
|
|
|
314
386
|
});
|
|
315
387
|
return [BlockType.CUE, cueParsingResult];
|
|
316
388
|
}
|
|
389
|
+
function isHeader(evaluation) {
|
|
390
|
+
return Boolean(evaluation[0] & BlockType.HEADER);
|
|
391
|
+
}
|
|
317
392
|
function isRegion(evalutation) {
|
|
318
393
|
return Boolean(evalutation[0] & BlockType.REGION);
|
|
319
394
|
}
|
|
@@ -333,4 +408,7 @@ function isCue(evaluation) {
|
|
|
333
408
|
function isError(evaluation) {
|
|
334
409
|
return !Array.isArray(evaluation) && evaluation instanceof Error;
|
|
335
410
|
}
|
|
411
|
+
function isXTimestampMapValueValue(value) {
|
|
412
|
+
return Number.isFinite(value);
|
|
413
|
+
}
|
|
336
414
|
//# sourceMappingURL=Adapter.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* When VTT file doesn't start with WEBVTT format
|
|
3
3
|
*/
|
|
4
|
-
type Reason = "WEBVTT_HEADER_MISSING" | "UNKNOWN_BLOCK_ENTITY" | "INVALID_CUE_FORMAT";
|
|
4
|
+
type Reason = "WEBVTT_HEADER_MISSING" | "UNKNOWN_BLOCK_ENTITY" | "INVALID_CUE_FORMAT" | "WEBVTT_HEADER_X_TIMESTAMP_MAP_INVALID";
|
|
5
5
|
export declare class InvalidFormatError extends Error {
|
|
6
6
|
constructor(reason: Reason, dataBlock: string);
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sub37/webvtt-adapter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A subtitles adapter for WebVTT subtitles",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@sub37/adapter-utils": "^1.0.
|
|
8
|
+
"@sub37/adapter-utils": "^1.0.3"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "
|
|
11
|
+
"build": "pnpm tsc --build",
|
|
12
12
|
"test": "pnpm build && pnpm --prefix ../.. run test",
|
|
13
13
|
"prepublishOnly": "pnpm build"
|
|
14
14
|
},
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"files": [
|
|
29
29
|
"lib/**/*.+(js|d.ts)!(*.map)"
|
|
30
30
|
],
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "e5343df3847b48e7d94021d0af597e7e6d8a10e1"
|
|
32
32
|
}
|