-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebVTT.beta.mjs
251 lines (244 loc) · 12.7 KB
/
WebVTT.beta.mjs
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
// refer: https://www.w3.org/TR/webvtt1/
export default class WebVTT {
static name = "WebVTT";
static version = "2.2.0";
static about = () => console.log(`\n🟧 ${this.name} v${this.version}\n`);
static parse(vtt = new String, options = { milliseconds: true, timeStamp: true, line: "single", lineBreak: "\n" }) {
console.log(`🚧 ${this.name}, parse WebVTT`, "");
// 使用[^]而非[(\r\n)\r\n]来匹配换行符 执行效率提高一倍 https://stackoverflow.com/a/16119722
// 数组化srt/webVTT格式的headers的Regex,预留webVTT的CSSboxes以便未来修改字幕样式
// .*::cue.*(\(.*\))?((\n|.)*})?
// .*::cue.*(?:\(.*\))?(?:(?:\n|.)*})?
/***************** v1.0.0-beta *****************/
//const webVTT_headers_Regex = /(?<fileType>WEBVTT)?[^]{2}?(?<CSSStyle>STYLE)[^]/;
/***************** v1.1.0-beta *****************/
//const webVTT_headers_Regex = /^(?:(?<fileType>WEBVTT)[^][^])?(?:(?<CSSStyle>STYLE)[^](?<CSSboxes>.*)[^][^])?/;
/***************** v1.2.0 *****************/
//const webVTT_headers_Regex = /^(?:(?<fileType>WEBVTT)[^][^])?(?:(?<CSSStyle>STYLE)[^](?<CSSboxes>.*::cue.*(?:\(.*\))?(?:(?:\n|.)*})?)[^][^])?/;
/***************** v1.3.0-beta *****************/
//const headers_WEBVTT_Regex = /^(?<fileType>WEBVTT)?[^](?<Xoptions>.+[^])*/;
//const headers_STYLE_Regex = /^(?<Style>STYLE)[^](?<Boxes>.*::cue.*(\(.*\))?((\n|.)*}$)?)/m;
//const headers_STYLE_Regex = /^(?<CSSStyle>STYLE)[^](?<CSSboxes>.*::cue.*(?:\(.*\))?(?:(?:\n|.)*}$)?)/m;
//const webVTT_headers_Regex = /^(?<fileType>WEBVTT)?[^](?<Xoptions>X-.+[^])*[^]((?<CSSStyle>STYLE)[^](?<CSSboxes>.*::cue.*(?:\(.*\))?(?:(?:\n|.)*})*)[^][^])?/;
//$.log(`🚧 ${this.name}, parse WebVTT`, `webVTT_Regex内容: ${webVTT_headers_Regex}`, "");
//$.log(`🚧 ${this.name}, parse WebVTT`, `vtt.match(webVTT_headers_Regex)内容: ${vtt.match(webVTT_headers_Regex)}`, "");
//$.log(`🚧 ${this.name}, parse WebVTT`, `vtt.match(webVTT_headers_Regex).groups内容: ${vtt.match(webVTT_headers_Regex).groups}`, "");
// 数组化srt/webVTT格式的body的Regex,预留webVTT的options以便未来修改字幕属性
// matchAll版
//const webVTT_body_Regex = (this.opts.includes("milliseconds")) ? /(?<srtNum>\d+)?[^]?(?<timeLine>(?<startTime>(?:\d\d:)?\d\d:\d\d(?:\.|,)\d\d\d) --> (?<endTime>(?:\d\d:)?\d\d:\d\d(?:\.|,)\d\d\d)) ?(?<options>.+)?[^](?<text>.+)[^][^]/g
// : /(?<srtNum>\d+)?[^]?(?<timeLine>(?<startTime>(?:\d\d:)?\d\d:\d\d)(?:\.|,)\d\d\d --> (?<endTime>(?:\d\d:)?\d\d:\d\d)(?:\.|,)\d\d\d) ?(?<options>.+)?[^](?<text>.+)[^][^]/g;
// match版
//const body_CUE_Regex = (this.opts.includes("milliseconds")) ? /^(?:(?<srtNum>\d+)[(\r\n)\r\n])?(?<timeLine>(?<startTime>(?:\d\d:)?\d\d:\d\d(?:\.|,)\d\d\d) --> (?<endTime>(?:\d\d:)?\d\d:\d\d(?:\.|,)\d\d\d)) ?(?<options>.+)?[^](?<text>.*[^]*)$/
// : /^(?:(?<srtNum>\d+)[(\r\n)\r\n])?(?<timeLine>(?<startTime>(?:\d\d:)?\d\d:\d\d)(?:\.|,)\d\d\d --> (?<endTime>(?:\d\d:)?\d\d:\d\d)(?:\.|,)\d\d\d) ?(?<options>.+)?[^](?<text>.*[^]*)$/
//const body_CUE_Regex = (this.opts.includes("milliseconds")) ? /^((?<srtNum>\d+)(\r\n|\r|\n))?(?<timeLine>(?<startTime>(\d\d:)?\d\d:\d\d[\.,]\d\d\d) --> (?<endTime>(\d\d:)?\d\d:\d\d[\.,]\d\d\d)) ?(?<options>.+)?[^](?<text>[\s\S]*)$/
//: /^((?<srtNum>\d+)(\r\n|\r|\n))?(?<timeLine>(?<startTime>(\d\d:)?\d\d:\d\d)[\.,]\d\d\d --> (?<endTime>(?:\d\d:)?\d\d:\d\d)(?:\.|,)\d\d\d) ?(?<options>.+)?[^](?<text>[\s\S]*)$/
/***************** v1.8.0-beta *****************/
const WebVTTCueRegex = (options.milliseconds) ? /^((?<index>\d+)(\r\n|\r|\n))?(?<timing>(?<startTime>[0-9:.,]+) --> (?<endTime>[0-9:.,]+)) ?(?<settings>.+)?[^](?<text>[\s\S]*)?$/
: /^((?<index>\d+)(\r\n|\r|\n))?(?<timing>(?<startTime>[0-9:]+)[0-9.,]+ --> (?<endTime>[0-9:]+)[0-9.,]+) ?(?<settings>.+)?[^](?<text>[\s\S]*)?$/
//$.log(`🚧 ${this.name}, parse WebVTT`, `webVTT_body_Regex内容: ${webVTT_body_Regex}`, "");
/***************** v1.0.0-beta *****************/
/*
let array = vtt.split(/[(\r\n)\r\n]{2,}/);
let json = {
headers: (array[0] == "WEBVTT") ? array.slice(0, 3) : null,
body: (array[0] == "WEBVTT") ? array.slice(3) : array
};
/*
if (json.body[0] == "WEBVTT") {
$.log(`🚧 ${$.name}, parse WebVTT`, `is webVTT`, "");
json.head = json.body.slice(0, 3) // webVTT文件头
$.log(`🚧 ${$.name}, parse WebVTT`, `json.head内容: ${json.head}`, "");
json.body = json.body.slice(3) // 去除webVTT文件头后的字幕文件
//$.log(`🚧 ${$.name}, parse WebVTT`, `json.body内容: ${json.body}`, "");
}
*/
/***************** v1.1.0-beta *****************/
/*
// 字符串末位追加两个换行符
//vtt = vtt + "\n\n"
// 直接用matchAll构建数组
let json = {
headers: vtt.match(webVTT_headers_Regex)?.groups ?? null, //正则
body: [...vtt.matchAll(webVTT_body_Regex)].filter(Boolean) //正则,去空
};
*/
/***************** v1.2.0-beta *****************/
// 使用map映射对数组化的字幕进行正则命名组筛选
//json.body = json.body.map(item => item = item.match(webVTT_body_Regex)?.groups ?? "");
// 或者用forEach实现
/*
json.body.forEach((item, i) => {
json.body[i] = item.match(webVTT_Regex)?.groups ?? "";
});
*/
/***************** v1.2.0 *****************/
//let array = vtt.split(/[(\r\n)\r\n]{2,}/);
/*
let json = {
headers: vtt.match(webVTT_headers_Regex)?.groups ?? null,
body: vtt.split(/[(\r\n)\r\n]{2,}/).map(item => item = item.match(webVTT_body_Regex)?.groups ?? "")
};
*/
/***************** v1.3.0-beta *****************/
/*
let json = {
headers: vtt.match(headers_WEBVTT_Regex)?.groups ?? null,
CSS: vtt.match(headers_STYLE_Regex)?.groups ?? null,
body: vtt.split(/\r\n\r\n|\r\r|\n\n/).map(item => {
//$.log(`🚧 ${$.name}`, `item: ${item}`);
item = item.match(body_CUE_Regex)?.groups ?? ""
//$.log(`🚧 ${$.name}`, `${item?.text ?? ""}`, "");
return item;
})
*/
/***************** v2.0.0 *****************/
const Array = vtt.split(/\r\n\r\n|\r\r|\n\n/);
console.log(`🚧 ${$.name}`, `Array: ${Array}`);
const Json = { headers: {}, comments: [], style: "", body: [] };
Array.forEach(item => {
item = item.trim();
console.log(`🚧 ${$.name}`, `item.substring(0, 5).trim(): ${item.substring(0, 5).trim()}`);
switch (item.substring(0, 5).trim()) {
case "WEBVT": {
let cues = item.split(/\r\n|\r|\n/);
console.log(`🚧 ${$.name}`, `cues: ${cues}`);
Json.headers.type = cues.shift();
Json.headers.options = cues;
break;
};
case "NOTE": {
//let cues = item.split("NOTE ").trimEnd();
//$.log(`🚧 ${$.name}`, `cues: ${cues}`);
//Json.comments = cues;
Json.comments.push(item);
break;
};
case "STYLE": {
let cues = item.split(/\r\n|\r|\n/);
cues.shift();
Json.style = cues.join(options.lineBreak);
break;
};
default:
/***************** v2.0.0 *****************/
//Json.body[i] = item.match(body_CUE_Regex)?.groups ?? undefined;
/***************** v2.1.0 *****************/
let cue = item.match(WebVTTCueRegex)?.groups;
if (cue) {
if (Json.headers?.type !== "WEBVTT") {
cue.timing = cue?.timing?.replace?.(",", ".");
cue.startTime = cue?.startTime?.replace?.(",", ".");
cue.endTime = cue?.endTime?.replace?.(",", ".");
}
if (options.timeStamp) {
let ISOString = cue?.startTime?.replace?.(/(.*)/, "1970-01-01T$1Z")
cue.timeStamp = (options.milliseconds) ? Date.parse(ISOString) : Date.parse(ISOString) / 1000;
}
cue.text = cue?.text?.trimEnd?.();
switch (options.line) {
case "single":
cue.text = cue?.text?.replace?.(/\r\n|\r|\n/, " ");
break;
case "multi":
cue.text = cue?.text?.split?.(/\r\n|\r|\n/);
break;
};
Json.body.push(cue);
};
break;
}
});
console.log(`🚧 ${this.name}, parse WebVTT`, `Json.headers: ${JSON.stringify(Json.headers)}`, "");
console.log(`🚧 ${this.name}, parse WebVTT`, `Json.comments: ${JSON.stringify(Json.comments)}`, "");
console.log(`🚧 ${this.name}, parse WebVTT`, `Json.style: ${JSON.stringify(Json.style)}`, "");
console.log(`🚧 ${this.name}, parse WebVTT`, `Json.body: ${JSON.stringify(Json.body)}`, "");
/***************** v2.0.0 *****************/
/*
// 数组去空(不符合正则筛选的数据)
Json.body = Json.body.filter(Boolean);
// 使用map映射对JSON字幕进行格式化
Json.body = Json.body.map((item, i) => {
// 加入索引号方便文本传输翻译字幕
item.index = i;
// SRT格式字幕转换时间分隔符
// 原版
//if (Json.headers?.[0] !== "WEBVTT") {
// 正式版
if (Json.headers?.type !== "WEBVTT") {
item.timeLine = item.timeLine.replace(",", ".");
item.startTime = item.startTime.replace(",", ".");
item.endTime = item.endTime.replace(",", ".");
}
// 是否包含UNIX时间戳
if (this.opts.includes("timeStamp")) {
let ISOString = item.startTime.replace(/(.*)/, "1970-01-01T$1Z")
item.timeStamp = this.opts.includes("milliseconds") ? Date.parse(ISOString) : Date.parse(ISOString) / 1000;
}
// 从两头去掉空白字符的字符串
item.text = item.text?.trim() ?? "_";
// 是否将多行文本分割为数组,方便未来提供交替插入文本功能
// \r\n, \r, \n 是三种不同系统的换行方式
if (this.opts.includes("singleLine")) {
item.text = item.text.replace(/\r\n|\r|\n/, " ");
} else if (this.opts.includes("multiLine")) {
item.text = item.text.split(/\r\n|\r|\n/);
}
return item
});
*/
/*
// 时间: SRT的逗号替换为WebVTT的句号
if (Json.headers[0] !== "WEBVTT") Json.body.forEach((item, i) => {
Json.body[i].timeLine = item.timeLine.replace(",", ".");
Json.body[i].startTime = item.startTime.replace(",", ".");
});
// 时间: 转换为ISO标准格式再转为UNIX时间戳的数字对象再除以1000以变更为单位秒
if (opts.includes("timeStamp")) Json.body.forEach((item, i) => {
let toISOstring = item.startTime.replace(/(.*)/, "1970-01-01T$1Z")
Json.body[i].timeStamp = Date.parse((opts.includes("milliseconds")) ? toISOstring : toISOstring / 1000)
});
// 文本: 分割多行文本为数组
if (opts.includes("multiText")) Json.body.forEach((item, i) => {
Json.body[i].text = item.text.split(/[(\r\n)\r\n]/); // \r\n, \r, \n 是三种不同系统的换行方式
});
*/
//console.log(`🚧 ${this.name}, parse WebVTT`, `Json.headers内容: ${JSON.stringify(Json.headers)}`, "");
//console.log(`🚧 ${this.name}, parse WebVTT`, `Json.body内容: ${JSON.stringify(Json.body)}`, "");
return Json;
};
static stringify(json = { headers: {}, comments: [], style: "", body: [] }, options = { milliseconds: true, timeStamp: true, line: "single", lineBreak: "\n" }) {
console.log(`🚧 ${this.name}, stringify WebVTT`, "");
//const newLine = (this.opts.includes("\n")) ? "\n" : (this.opts.includes("\r")) ? "\r" : (this.opts.includes("\r\n")) ? "\r\n" : "\n";
let vtt = [
//json.headers = json.headers?.fileType || "WEBVTT",
/***************** v1.0.0-beta *****************/
//json.headers = (json.headers?.[0] == "WEBVTT") ? json.headers.join(newLine + newLine) : "WEBVTT",
/***************** v1.2.0 *****************/
//json.headers = (json?.headers?.CSSStyle) ? ["WEBVTT", "STYLE" + newLine + json.headers.CSSboxes].join(newLine + newLine) : "WEBVTT",
/***************** v1.3.0-beta *****************/
//json.headers = [json.headers?.fileType || "WEBVTT", json.headers?.Xoptions || null].join(options.newLine),
//json.CSS = json.CSS?.Style ? [json.CSS.Style, json.CSS.Boxes].join(options.newLine) : null,
/***************** v2.0.0 *****************/
json.headers = [json.headers?.type || "", json.headers?.options || ""].flat(Infinity).join(options.lineBreak),
json.comments = json?.comments?.join?.(options.lineBreak),
json.style = (json?.style?.length > 0) ? ["STYLE", json.style].join(options.lineBreak) : "",
json.body = json.body.map(item => {
if (Array.isArray(item.text)) item.text = item.text.join(options.lineBreak);
item = `${(item.index) ? item.index + options.lineBreak : ""}${item.timing} ${item?.settings ?? ""}${options.lineBreak}${item.text}`;
return item;
}).join(options.lineBreak + options.lineBreak)
].join(options.lineBreak + options.lineBreak).trim() + options.lineBreak + options.lineBreak;
// 按步骤分行写法
/*
if (options.includes("cue"))
json.headers = (json.headers[0] == "WEBVTT") ? json.headers.join(newLine + newLine) : "WEBVTT"
json.body = json.body.map(item => {
if (Array.isArray(item.text)) item.text = item.text.join(newLine);
item = `${item.timeLine} ${item.options}${newLine}${item.text}`.join(newLine + newLine);
return item;
})
json = json.join(newLine + newLine);
*/
return vtt;
};
};