mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-09 16:53:47 +02:00
Add ESLint and lint a bunch
This commit is contained in:
+42
-42
@@ -1,24 +1,24 @@
|
||||
export type SubtitleCue = {
|
||||
timestamp: number, // in seconds
|
||||
duration: number, // in seconds
|
||||
text: string,
|
||||
identifier?: string,
|
||||
settings?: string,
|
||||
notes?: string
|
||||
timestamp: number; // in seconds
|
||||
duration: number; // in seconds
|
||||
text: string;
|
||||
identifier?: string;
|
||||
settings?: string;
|
||||
notes?: string;
|
||||
};
|
||||
|
||||
export type SubtitleConfig = {
|
||||
description: string
|
||||
description: string;
|
||||
};
|
||||
|
||||
export type SubtitleMetadata = {
|
||||
config?: SubtitleConfig
|
||||
config?: SubtitleConfig;
|
||||
};
|
||||
|
||||
type SubtitleParserOptions = {
|
||||
codec: 'webvtt',
|
||||
output: (cue: SubtitleCue, metadata: SubtitleMetadata) => unknown,
|
||||
error: (error: Error) => unknown
|
||||
codec: 'webvtt';
|
||||
output: (cue: SubtitleCue, metadata: SubtitleMetadata) => unknown;
|
||||
error: (error: Error) => unknown;
|
||||
};
|
||||
|
||||
const cueBlockHeaderRegex = /(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g;
|
||||
@@ -42,16 +42,16 @@ export class SubtitleParser {
|
||||
|
||||
if (!this.preambleText) {
|
||||
if (!preambleStartRegex.test(text)) {
|
||||
let error = new Error('WebVTT preamble incorrect.');
|
||||
const error = new Error('WebVTT preamble incorrect.');
|
||||
this.options.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
match = cueBlockHeaderRegex.exec(text);
|
||||
let preamble = text.slice(0, match?.index ?? text.length).trimEnd();
|
||||
const preamble = text.slice(0, match?.index ?? text.length).trimEnd();
|
||||
|
||||
if (!preamble) {
|
||||
let error = new Error('No WebVTT preamble provided.');
|
||||
const error = new Error('No WebVTT preamble provided.');
|
||||
this.options.error(error);
|
||||
throw error;
|
||||
}
|
||||
@@ -64,37 +64,37 @@ export class SubtitleParser {
|
||||
}
|
||||
}
|
||||
|
||||
while (match = cueBlockHeaderRegex.exec(text)) {
|
||||
let notes = text.slice(0, match.index);
|
||||
let cueIdentifier = match[1];
|
||||
let matchEnd = match.index! + match[0].length;
|
||||
let bodyStart = text.indexOf('\n', matchEnd) + 1;
|
||||
let cueSettings = text.slice(matchEnd, bodyStart).trim();
|
||||
while ((match = cueBlockHeaderRegex.exec(text))) {
|
||||
const notes = text.slice(0, match.index);
|
||||
const cueIdentifier = match[1];
|
||||
const matchEnd = match.index! + match[0].length;
|
||||
const bodyStart = text.indexOf('\n', matchEnd) + 1;
|
||||
const cueSettings = text.slice(matchEnd, bodyStart).trim();
|
||||
let bodyEnd = text.indexOf('\n\n', matchEnd);
|
||||
if (bodyEnd === -1) bodyEnd = text.length;
|
||||
|
||||
let startTime = parseSubtitleTimestamp(match[2]!);
|
||||
let endTime = parseSubtitleTimestamp(match[3]!);
|
||||
let duration = endTime - startTime;
|
||||
const startTime = parseSubtitleTimestamp(match[2]!);
|
||||
const endTime = parseSubtitleTimestamp(match[3]!);
|
||||
const duration = endTime - startTime;
|
||||
|
||||
let body = text.slice(bodyStart, bodyEnd).trim();
|
||||
const body = text.slice(bodyStart, bodyEnd).trim();
|
||||
|
||||
text = text.slice(bodyEnd).trimStart();
|
||||
cueBlockHeaderRegex.lastIndex = 0;
|
||||
|
||||
let cue: SubtitleCue = {
|
||||
const cue: SubtitleCue = {
|
||||
timestamp: startTime / 1000,
|
||||
duration: duration / 1000,
|
||||
text: body,
|
||||
identifier: cueIdentifier,
|
||||
settings: cueSettings,
|
||||
notes
|
||||
notes,
|
||||
};
|
||||
|
||||
let meta: SubtitleMetadata = {};
|
||||
const meta: SubtitleMetadata = {};
|
||||
if (!this.preambleEmitted) {
|
||||
meta.config = {
|
||||
description: this.preambleText
|
||||
description: this.preambleText,
|
||||
};
|
||||
this.preambleEmitted = true;
|
||||
}
|
||||
@@ -106,23 +106,23 @@ export class SubtitleParser {
|
||||
|
||||
const timestampRegex = /(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/;
|
||||
export const parseSubtitleTimestamp = (string: string) => {
|
||||
let match = timestampRegex.exec(string);
|
||||
const match = timestampRegex.exec(string);
|
||||
if (!match) throw new Error('Expected match.');
|
||||
|
||||
return 60 * 60 * 1000 * Number(match[1] || '0') +
|
||||
60 * 1000 * Number(match[2]) +
|
||||
1000 * Number(match[3]) +
|
||||
Number(match[4]);
|
||||
return 60 * 60 * 1000 * Number(match[1] || '0')
|
||||
+ 60 * 1000 * Number(match[2])
|
||||
+ 1000 * Number(match[3])
|
||||
+ Number(match[4]);
|
||||
};
|
||||
|
||||
export const formatSubtitleTimestamp = (timestamp: number) => {
|
||||
let hours = Math.floor(timestamp / (60 * 60 * 1000));
|
||||
let minutes = Math.floor((timestamp % (60 * 60 * 1000)) / (60 * 1000));
|
||||
let seconds = Math.floor((timestamp % (60 * 1000)) / 1000);
|
||||
let milliseconds = timestamp % 1000;
|
||||
const hours = Math.floor(timestamp / (60 * 60 * 1000));
|
||||
const minutes = Math.floor((timestamp % (60 * 60 * 1000)) / (60 * 1000));
|
||||
const seconds = Math.floor((timestamp % (60 * 1000)) / 1000);
|
||||
const milliseconds = timestamp % 1000;
|
||||
|
||||
return hours.toString().padStart(2, '0') + ':' +
|
||||
minutes.toString().padStart(2, '0') + ':' +
|
||||
seconds.toString().padStart(2, '0') + '.' +
|
||||
milliseconds.toString().padStart(3, '0');
|
||||
};
|
||||
return hours.toString().padStart(2, '0') + ':'
|
||||
+ minutes.toString().padStart(2, '0') + ':'
|
||||
+ seconds.toString().padStart(2, '0') + '.'
|
||||
+ milliseconds.toString().padStart(3, '0');
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user