This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
asr-viewer.js
334 lines (275 loc) · 10.8 KB
/
asr-viewer.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
let $$ = (selector, scope = document) => {
return [...scope.querySelectorAll(selector)];
};
let player;
let playerContainer = document.querySelector("#player");
let transcriptSearchText = document.querySelector("#transcript-search");
let availableTranscripts = document.querySelector("#available-transcripts");
let transcriptContainer = document.querySelector("#transcript");
function displayItem(itemId) {
let itemElement = availableTranscripts.querySelector(
`[data-item-id="${itemId}"]`
);
if (!itemElement) {
console.warn(
"Couldn't find an option matching selected item ID:",
itemId
);
return;
}
document.body.dataset.displayItem = itemId;
let transcriptUrl = itemElement.dataset.transcriptUrl;
let itemUrl = itemElement.dataset.itemUrl;
let mediaType = itemElement.dataset.mediaType;
let mediaSourceUrl =
itemElement.dataset.mediaStreamSrc ||
itemElement.dataset.mediaMasterSrc;
let subtitleUrl = transcriptUrl
.replace("results", "webvtt")
.replace(".json", ".vtt");
let title = itemElement.title;
document.location.hash = "#" + itemElement.dataset.itemId;
initializePlayer(playerContainer, mediaType, mediaSourceUrl, subtitleUrl);
displayTranscript(transcriptContainer, transcriptUrl, itemUrl, title);
}
function removeChildren(element) {
while (element.firstChild) {
element.firstChild.remove();
}
}
function displayTranscript(transcriptContainer, transcriptUrl, itemUrl, title) {
let cardTitle = transcriptContainer.querySelector(".card-title");
cardTitle.textContent = "Loading:" + transcriptUrl;
let transcriptBody = transcriptContainer.querySelector("tbody");
removeChildren(transcriptBody);
fetch(transcriptUrl)
.then((response) => {
return response.json();
})
.then((data) => {
removeChildren(cardTitle);
var titleLink = document.createElement("a");
titleLink.target = "_blank";
titleLink.textContent = `${title} (${data.jobName})`;
titleLink.href = itemUrl;
cardTitle.append(titleLink);
renderTranscript(data.results, transcriptBody);
});
}
function renderTranscript(results, tableBody) {
let lastTime = 0;
let items = results.items;
let row, timeCell, textCell;
for (const item of items) {
// We parse time values into floating-point numbers which will be set on
// the element itself for JavaScript access and exposed rounded to one
// place in the element's data attributes for display using CSS
let startTime = Number.parseFloat(item.start_time);
let endTime = Number.parseFloat(item.end_time);
// Some transcripts have punctuation values which lack time codes and,
// in all observed cases, accompany a previous item which does have
// timing information.
if (Number.isNaN(startTime)) {
if (row && row.endTime) {
startTime = row.endTime;
} else {
console.warn("Unexpected item without a start time:", item);
}
}
if (Number.isNaN(endTime)) {
if (row && row.endTime) {
endTime = row.endTime;
} else {
console.warn("Unexpected item without an end time:", item);
}
}
if (!row || startTime - lastTime > 1.5) {
row = document.createElement("tr");
row.className = "timecode";
row.startTime = row.dataset.startTime = startTime;
row.endTime = row.dataset.endTime = endTime;
tableBody.append(row);
timeCell = document.createElement("th");
timeCell.className = "timecode";
timeCell.startTime = startTime;
timeCell.dataset.startTime = startTime.toFixed(1);
row.append(timeCell);
textCell = document.createElement("td");
row.append(textCell);
}
timeCell.endTime = endTime;
timeCell.dataset.endTime = endTime.toFixed(1);
row.endTime = row.dataset.endTime = endTime;
if (item.alternatives.length > 0) {
// The format allows more than one alternative but this has not been
// observed in the wild:
let alternative = item.alternatives[0];
let span = document.createElement("span");
span.classList.add("timecode", item.type);
span.textContent = alternative.content;
if (alternative.confidence) {
let roundedConfidence = Math.round(
Number.parseFloat(alternative.confidence) * 10
);
// This value is an integer used by the CSS to shade less confident words:
span.dataset.confidence = roundedConfidence.toFixed(0);
}
// We'll set these values directly on the element so floating point
// comparisons can be made without requiring any string parsing in
// the highlighting update path
span.startTime = startTime;
span.endTime = endTime;
if (item.start_time) {
span.title = item.alternatives
.map((i) => {
let formated = item.start_time + "s: " + i.content;
if (i.confidence) {
formated +=
" (" +
(100 * Number.parseFloat(i.confidence)).toFixed(
1
) +
"%)";
}
return formated;
})
.join(", ");
}
textCell.append(span);
}
lastTime = endTime;
}
}
function loadAllTranscripts() {
fetch("index.json")
.then((response) => {
return response.json();
})
.then((data) => {
let tBody = availableTranscripts.querySelector("tbody");
Object.entries(data)
.sort((a, b) => {
return a[1].title.localeCompare(b[1].title);
})
.forEach(([k, v]) => {
// We'll create new table rows for the available transcripts table which contain data attributes which other JS will use to load the actual transcript when selected:
let row = document.createElement("tr");
row.value = k;
row.dataset.itemId = k;
row.dataset.transcriptUrl = "results/" + k + ".json"; // FIXME: put this in the JSON!
row.dataset.itemUrl = v.item_url;
row.dataset.mediaType = v.media_stream_url.match(/\.mp4$/)
? "video"
: "audio";
row.dataset.mediaMasterSrc = v.media_master_url;
row.dataset.mediaStreamSrc = v.media_stream_url;
row.dataset.language = v.language;
row.title = v.title;
let idCell = document.createElement("th");
let titleCell = document.createElement("td");
idCell.textContent = k;
titleCell.textContent = v.title;
row.append(idCell);
row.append(titleCell);
tBody.append(row);
});
let itemId = document.location.hash.replace("#", "");
if (itemId) {
$$("tr", availableTranscripts).filter((element) => {
if (element.dataset.itemId == itemId) {
displayItem(itemId);
}
});
}
});
}
function initializePlayer(
playerContainer,
tagName,
mediaSourceUrl,
subtitleUrl
) {
removeChildren(playerContainer);
player = document.createElement(tagName);
player.autoplay = true;
player.preload = "metadata";
player.controls = true;
player.src = mediaSourceUrl;
let subtitleTrack = document.createElement("track");
subtitleTrack.label = subtitleTrack.kind = "subtitles";
subtitleTrack.type = "text/vtt";
subtitleTrack.src = subtitleUrl;
player.append(subtitleTrack);
playerContainer.append(player);
player.addEventListener("timeupdate", () => {
let currentTime = player.currentTime;
$$(".highlighted").forEach((element) =>
element.classList.remove("highlighted")
);
$$(".timecode").forEach((element) => {
if (
element.startTime <= currentTime &&
element.endTime >= currentTime
) {
element.classList.add("highlighted");
}
});
});
}
transcriptSearchText.addEventListener("focus", () => {
document.body.classList.add("search-active");
});
transcriptSearchText.addEventListener("input", () => {
let searchText = transcriptSearchText.value.trim().toLocaleLowerCase();
if (!searchText) {
availableTranscripts.querySelectorAll("[hidden]").forEach((element) => {
element.removeAttribute("hidden");
});
} else {
availableTranscripts
.querySelectorAll("tr[data-item-id]")
.forEach((element) => {
// TODO: calculate this once on load
let elementText =
`${element.dataset.itemId} ${element.title}`.toLocaleLowerCase();
if (elementText.includes(searchText)) {
element.removeAttribute("hidden");
} else {
element.setAttribute("hidden", "hidden");
}
});
}
let itemCount = availableTranscripts.querySelectorAll(
"tbody tr:not([hidden])"
).length;
availableTranscripts.querySelector(
"caption"
).textContent = `${itemCount} items`;
});
availableTranscripts.addEventListener("click", (event) => {
let transcriptId = event.target.parentNode.dataset.itemId;
if (transcriptId) {
document.body.classList.remove("search-active");
displayItem(transcriptId);
}
});
transcriptContainer.addEventListener("click", (event_) => {
let startTime =
event_.target.startTime || event_.target.parentNode.startTime;
if (startTime) {
player.currentTime = startTime;
player.play();
}
});
document.addEventListener("keydown", (event) => {
if (event.key == " " && player && event.target != transcriptSearchText) {
if (player.paused) {
player.play();
} else {
player.pause();
}
event.preventDefault();
return false;
}
});
loadAllTranscripts();