-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.js
More file actions
227 lines (211 loc) · 7.41 KB
/
Copy pathtranscript.js
File metadata and controls
227 lines (211 loc) · 7.41 KB
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
globalThis.AtlasTranscript = Object.freeze({
normalizeSegments(segments) {
const candidates = [];
for (const segment of Array.isArray(segments) ? segments : []) {
const text = String(segment?.text || "")
.replace(/\s+/g, " ")
.trim();
if (!text) continue;
const start = Math.max(0, Number(segment.start) || 0);
const duration = Math.max(0, Number(segment.duration) || 0);
candidates.push({ start, duration, text });
}
candidates.sort(
(left, right) =>
left.start - right.start || left.duration - right.duration || left.text.localeCompare(right.text)
);
const normalized = [];
for (const candidate of candidates) {
const previous = normalized[normalized.length - 1];
if (
previous &&
previous.text === candidate.text &&
Math.abs(previous.start - candidate.start) < 1
) {
continue;
}
normalized.push(candidate);
}
return normalized;
},
parseJson3(payload) {
const events = Array.isArray(payload?.events) ? payload.events : [];
return this.normalizeSegments(
events.map((event) => ({
start: (Number(event.tStartMs) || 0) / 1000,
duration: (Number(event.dDurationMs) || 0) / 1000,
text: (event.segs || []).map((segment) => segment.utf8 || "").join("")
}))
);
},
parseTimedTextXml(xml) {
const source = String(xml || "");
const segments = [];
const textPattern = /<text\b([^>]*)>([\s\S]*?)<\/text>/gi;
let match;
while ((match = textPattern.exec(source))) {
const attributes = parseXmlAttributes(match[1]);
segments.push({
start: Number(attributes.start) || 0,
duration: Number(attributes.dur) || 0,
text: decodeXmlText(match[2])
});
}
if (segments.length === 0) {
const paragraphPattern = /<p\b([^>]*)>([\s\S]*?)<\/p>/gi;
while ((match = paragraphPattern.exec(source))) {
const attributes = parseXmlAttributes(match[1]);
const text = match[2]
.replace(/<s\b[^>]*>/gi, "")
.replace(/<\/s>/gi, "");
segments.push({
start: (Number(attributes.t) || 0) / 1000,
duration: (Number(attributes.d) || 0) / 1000,
text: decodeXmlText(text)
});
}
}
return this.normalizeSegments(segments);
},
parseInnertubeTranscript(payload) {
const segments = [];
walkJson(payload, (node) => {
const renderer = node?.transcriptSegmentRenderer;
if (!renderer) return;
const start = (Number(renderer.startMs) || 0) / 1000;
const end = (Number(renderer.endMs) || 0) / 1000;
segments.push({
start,
duration: Math.max(0, end - start),
text: readText(renderer.snippet)
});
});
return this.normalizeSegments(segments);
},
getInnertubeLanguageChoices(payload) {
const choices = [];
const seen = new Set();
walkJson(payload, (node) => {
for (const item of Array.isArray(node?.subMenuItems) ? node.subMenuItems : []) {
const title = readText(item.title);
const continuation =
item.continuation?.reloadContinuationData?.continuation ||
item.serviceEndpoint?.getTranscriptEndpoint?.params ||
item.navigationEndpoint?.getTranscriptEndpoint?.params ||
"";
if (!title || seen.has(`${title}:${continuation}`)) continue;
seen.add(`${title}:${continuation}`);
choices.push({ title, selected: Boolean(item.selected), continuation });
}
});
return choices;
},
chooseTrack(tracks, preferredLanguage) {
const available = Array.isArray(tracks) ? tracks : [];
if (available.length === 0) return null;
const preferred = String(preferredLanguage || "").toLowerCase();
const preferredBase = preferred.split("-")[0];
const exactManual = available.find(
(track) => track.languageCode?.toLowerCase() === preferred && track.kind !== "asr"
);
const exact = available.find(
(track) => track.languageCode?.toLowerCase() === preferred
);
const baseManual = available.find(
(track) =>
preferredBase &&
track.languageCode?.toLowerCase().split("-")[0] === preferredBase &&
track.kind !== "asr"
);
const base = available.find(
(track) =>
preferredBase && track.languageCode?.toLowerCase().split("-")[0] === preferredBase
);
return (
available.find((track) => track.id?.toLowerCase() === preferred) ||
exactManual ||
exact ||
baseManual ||
base ||
available.find((track) => track.kind !== "asr") ||
available[0]
);
},
toTimestampedText(segments) {
return this.normalizeSegments(segments)
.map((segment) => `[${formatClock(segment.start)}] ${segment.text}`)
.join("\n");
},
toVtt(segments) {
const normalized = this.normalizeSegments(segments);
const cues = normalized.map((segment, index) => {
const start = segment.start;
const nextStart = normalized[index + 1]?.start;
const end = Math.max(
start + 0.5,
segment.duration ? start + segment.duration : nextStart || start + 3
);
return `${index + 1}\n${formatVttTime(start)} --> ${formatVttTime(end)}\n${segment.text}`;
});
return `WEBVTT\n\n${cues.join("\n\n")}\n`;
}
});
function walkJson(value, visit) {
if (!value || typeof value !== "object") return;
visit(value);
for (const child of Object.values(value)) {
if (Array.isArray(child)) {
child.forEach((entry) => walkJson(entry, visit));
} else if (child && typeof child === "object") {
walkJson(child, visit);
}
}
}
function readText(value) {
if (typeof value === "string") return value;
if (typeof value?.simpleText === "string") return value.simpleText;
return Array.isArray(value?.runs)
? value.runs.map((run) => run?.text || "").join("")
: "";
}
function parseXmlAttributes(source) {
const attributes = {};
const pattern = /([\w:-]+)\s*=\s*(["'])(.*?)\2/g;
let match;
while ((match = pattern.exec(source))) attributes[match[1]] = match[3];
return attributes;
}
function decodeXmlText(source) {
return String(source || "")
.replace(/<br\s*\/?>/gi, "\n")
.replace(/<[^>]+>/g, "")
.replace(/&#x([0-9a-f]+);/gi, (_match, value) =>
String.fromCodePoint(Number.parseInt(value, 16))
)
.replace(/&#(\d+);/g, (_match, value) =>
String.fromCodePoint(Number.parseInt(value, 10))
)
.replace(/ /g, " ")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/&/g, "&");
}
function formatClock(totalSeconds) {
const value = Math.max(0, Math.floor(totalSeconds));
const hours = Math.floor(value / 3600);
const minutes = Math.floor((value % 3600) / 60);
const seconds = value % 60;
return hours
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
: `${minutes}:${String(seconds).padStart(2, "0")}`;
}
function formatVttTime(totalSeconds) {
const milliseconds = Math.max(0, Math.round(totalSeconds * 1000));
const hours = Math.floor(milliseconds / 3_600_000);
const minutes = Math.floor((milliseconds % 3_600_000) / 60_000);
const seconds = Math.floor((milliseconds % 60_000) / 1000);
const remainder = milliseconds % 1000;
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}.${String(remainder).padStart(3, "0")}`;
}