This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
169 lines (141 loc) · 4.63 KB
/
demo.html
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
<html lang="en"><head>
<meta charset="utf-8">
<style>
html {
box-sizing: border-box;
font-size: 100%;
background: #333;
font-family: sans-serif;
color: #333;
}
video {
display: block;
max-height: 25em;
margin: 0 auto;
}
#transcript {
width: 50%;
margin: 0 auto;
margin-top: 3em;
padding: 2em;
background-color: white;
border: 1px solid black;
}
#transcript * {
font-family: 'Verdana';
font-size: 1.5em;
line-height: 1.5em;
margin: 0 .5em;
}
#transcript *:first-child {
margin-left: 0;
}
#transcript .active {
color: green;
border-bottom: 2px solid green;
font-weight: bold;
}
</style>
<script>
const getVTT = path => fetch(path).then(response => response.text());
const parseVTT = vtt => {
const lines = vtt
.split(/\n/)
.filter(_ => _.length > 1)
.slice(1);
const pairs = lines.reduce((prev, next) => {
if (!prev.time) {
prev.sets.push({ 'time' : next });
} else {
prev.sets[prev.sets.length-1].text = next;
}
prev.time = !prev.time;
return prev;
}, {
time: false,
sets: []
}).sets;
const getTimeParts = time => {
const parts = time.split(/\:/);
const sec = parts[1].split('.');
return {
minute: parts[0],
second: sec[0],
millisecond: sec[1]
};
};
const details = pairs.map(_ => {
const time = _.time;
const parts = time.split(' ');
return {
start : getTimeParts(parts[0]),
end : getTimeParts(parts[2]),
text : _.text
};
});
const intervals = details.map(_ => {
const end =
(parseInt(_.end.minute) * 60000) +
(parseInt(_.end.second) * 1000) +
parseInt(_.end.millisecond);
const start =
(parseInt(_.start.minute) * 60000) +
(parseInt(_.start.second) * 1000) +
parseInt(_.start.millisecond);
const duration = end - start;
return {
end : end,
start: start,
duration: duration,
text: _.text
};
});
return intervals;
};
const configureIntervals = intervals => {
const target = document.getElementById('transcript');
let index = 0;
let offset = 0;
intervals.forEach((interval, i) => {
const el = document.createElement('span');
el.textContent = interval.text;
el.classList.add('interval-' + i);
target.appendChild(el);
});
const mark = el => {
Array.from(target.querySelectorAll('*')).forEach(e => {
e.classList.remove('active');
el.classList.add('active');
});
}
const next = () => {
const current = intervals[index];
if (current === undefined) return;
const el = document.querySelector('.interval-' + index);
setTimeout(() => {
index++;
mark(el);
offset = current.start;
next();
}, current.start - offset);
};
next();
};
const setupTranscript = () => {
const videoEl = document.querySelector('video');
const vttPath = videoEl.dataset.track;
getVTT(vttPath).then(parseVTT).then(intervals => {
videoEl.addEventListener('play', () => {
configureIntervals(intervals);
});
});
};
document.addEventListener('DOMContentLoaded', setupTranscript);
</script>
</head>
<body class=" hasGoogleVoiceExt">
<video src="video.mp4" controls="" data-track="subs.vtt">
</video>
<div id="transcript">
</div>
</body></html>