-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
442 lines (430 loc) · 14.6 KB
/
index.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta content="utf-8" http-equiv="encoding" />
<meta name="copyright" content="© 2020 Steve Seguin" />
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
<!-- Primary Meta Tags -->
<title>CAPTION.Ninja</title>
<meta name="title" content="CAPTION.Ninja" />
<meta name="description" content="This is a free-to-use captioning tool for OBS. Speech-to-text is done using Machine Learning" />
<meta name="author" content="Steve Seguin" />
<style>
@font-face {
font-family: 'Cousine';
src: url('fonts/Cousine-Bold.ttf') format('truetype');
}
button {
max-width: 600px;
}
body {
margin:0;
padding:0 10px;
height:100%;
border: 0;
display: flex;
flex-direction: column-reverse;
position:absolute;
bottom:0;
overflow:hidden;
}
.output {
margin:0;
background-color: #0000;
color: white;
font-family: Cousine, monospace;
font-size: 3.2em;
line-height: 1.1em;
letter-spacing: 0.0em;
padding: 0em;
text-shadow: 0.05em 0.05em 0px rgba(0,0,0,1);
}
.output span {
background-color: black;
padding: 8px 8px 0px 8px;
margin:0;
}
a {
color:white;
font-size:1.2em;
text-transform: none;
}
.github {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAd5JREFUOE+d1MurjlEUBvDfIZQZShEGmMhQiZGZlAlyK4eEAZHcyiVRyq1cUq5FwsD1MJGJiT/AzEzRcR8xMnGOW89pv9q9fScfe/J977o8e61nP2v16HzGYBWWYA4mlbBPeI5HuI9v7fSeDnircRqTh7msMX/ALtyr42rAEbiAzX8BaruTsx0/46gBL/0HWAMe0G01YNq8Xbz5PYuN2ISvCHc5oWEsruA69mJF8a0Mr6kwD/C64mw3zpSgCfjc6nE8vhRbAE+U/+8xI4DrcKNKOol9XfJ4CimgOb0BfIilxfIds/GyS8BZeIGRJf5BAN9gWjE8xcIuwZqwZ1hQPvoDOIBRxRCiN/wj4C30lpyBNmAmYNk/Aj7G4pIz2G75I6Y2Iu0CONwlZ2Ldcl+rqmjvWhdgCdmCi1Xs0KOsxc3C5WDhcw8uI9+dzugyVZFNw3/i1jTCfoVIJmo/VDiJeJ9gPX4U1CRHs4swrnXTO8xsZjlAd9GP+WU1zcPxckGde7WMZbvy5eirl8N5bMUxHMSvYdo9igMt3znsiK29vuLI1ojYU+2dwmWdnwv3V4YskozfUAGdFmzaz4KdgiM43KomNGTW32JnGd0/IZ0A48wrhpPMdFZ+feZievjqpILf7lRg3csIRqAAAAAASUVORK5CYII=");
background-color: #FFF !important;
width: 4px;
height: 12px;
background-repeat: no-repeat;
display: inline-block;
top: 2px;
position: relative;
left: 2px;
filter: invert(100%);
-webkit-filter: invert(100%);
}
.small-text-container {
background-color: black;
padding: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="interm" class="output">
<font style='font-size:60%;line-height: 1.4em;'><span>Welcome to CAPTION.Ninja - A Free Captioning Tool</span></font>
<div class="small-text-container">
<font style='font-size:30%;line-height: 1.4em;'>
<span>
<br />
<strong>📖 Quick Start:</strong>
<ol>
<li>Accept the microphone permissions when prompted.</li>
<li>Start speaking - your words will appear as captions.</li>
<li>For live streaming, add this link to OBS as a browser source overlay: 👉 <a id="shareLink" href="overlay.html" target='_blank'>*ERROR GENERATING LINK*</a></li>
</ol>
<strong>💡 Tips:</strong>
<ul>
<li>Use Google Chrome for best results.</li>
<li>To save the transcription: Select all (CTRL+A), copy (CTRL+C), and paste into a text editor (CTRL+V).</li>
<li>Change languages by updating the URL. <a href="https://cloud.google.com/speech-to-text/docs/languages" target="_blank">View language codes here</a></li>
</ul>
<strong>🔬🛠️ Advanced Features:</strong>
<ul>
<li>Try our <a href='./translate'>LIVE TRANSLATION version here</a>❗</li>
<li>Use a <a href='https://www.vb-audio.com/Cable/'>Virtual Audio Cable</a> to caption from other audio sources</li>
<li>Add &translate={bg,cs,nl,en,et,de,fr,is,it,nb,nn,fa,pl,pt,ru,es,uk} to the overlay page URL for translation</li>
</ul>
<strong>🆘 Need Help?</strong>
<ul>
<li>Check out more options and details on <a href='https://github.com/steveseguin/captionninja'>GitHub <span class="github"></span></a></li>
<li>Ask for help on <a href='https://discord.vdo.ninja'>our Discord server<span class="discord"></span></a> - @steve for help there</li>
</ul>
</span>
</font>
</div>
<br />
</div>
<div id="output" class="output"></div>
<script>
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function getOldStorage() {
var values = [],
keys = Object.keys(localStorage),
i = keys.length;
while ( i-- ) {
values.push( localStorage.getItem(keys[i]) );
}
return values;
}
function removeStorage(cname){
localStorage.removeItem(cname);
}
function setStorage(cname, cvalue, exdays=999){
var now = new Date();
var item = {
value: cvalue,
expiry: now.getTime() + (exdays * 24 * 60 * 60 * 1000),
};
try{
localStorage.setItem(cname, JSON.stringify(item));
}catch(e){errorlog(e);}
}
function getStorage(cname) {
try {
var itemStr = localStorage.getItem(cname);
} catch(e){
errorlog(e);
return;
}
if (!itemStr) {
return "";
}
var item = JSON.parse(itemStr);
var now = new Date();
if (now.getTime() > item.expiry) {
localStorage.removeItem(cname);
return "";
}
return item.value;
}
function updateURL(param, force=false) {
var para = param.split('=');
if (!(urlParams.has(para[0].toLowerCase()))){
if (history.pushState){
var arr = window.location.href.split('?');
var newurl;
if (arr.length > 1 && arr[1] !== '') {
newurl = window.location.href + '&' +param;
} else {
newurl = window.location.href + '?' +param;
}
window.history.pushState({path:newurl},'',newurl);
}
} else if (force){
if (history.pushState){
var href = new URL(window.location.href);
if (para.length==1){
href.searchParams.set(para[0].toLowerCase(), "");
} else {
href.searchParams.set(para[0].toLowerCase(), para[1]);
}
log(href.toString());
window.history.pushState({path:href.toString()},'',href.toString());
}
}
}
(function (w) {
w.URLSearchParams = w.URLSearchParams || function (searchString) {
var self = this;
self.searchString = searchString;
self.get = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(self.searchString);
if (results == null) {
return null;
}
else {
return decodeURI(results[1]) || 0;
}
};
};
})(window);
var urlParams = new URLSearchParams(window.location.search);
var sanitize = function(string) {
var temp = document.createElement('div');
temp.textContent = string;
return temp.textContent
.substring(0, Math.min(temp.textContent.length, 500))
.trim();
};
function generateStreamID(){
var text = "";
var possible = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
for (var i = 0; i < 7; i++){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var roomID = "test";
if (urlParams.has("room")){
roomID = urlParams.get("room");
} else if (urlParams.has("ROOM")){
roomID = urlParams.get("ROOM");
} else {
roomID = generateStreamID();
updateURL("room="+roomID);
}
var label = false;
if (urlParams.has("label")){
label = urlParams.get("label");
}
var myLang = "en-US";
if (urlParams.has("lang")){
myLang = urlParams.get("lang");
} else {
updateURL("lang="+myLang);
}
console.log("Language: "+myLang);
var srtFile = "";
var timestamp_start = null
var timestamp_stop = null
var timestamp_intern = null;
var logcounter=0;
var counter=0;
var url = document.URL.substr(0,document.URL.lastIndexOf('/'));
var oldSaves = Object.keys(localStorage);
if (oldSaves.length){
for (var i =0;i<oldSaves.length;i++){
if (oldSaves.length - 1== i){
if (oldSaves[i].startsWith("transcription_"+roomID+"_")){
try {
var button = document.createElement("button");
button.onclick = function(){
var srtFile = getStorage(this.dataset.key);
download(this.dataset.key+".srt", srtFile);
localStorage.removeItem(this.dataset.key);
};
button.dataset.key = oldSaves[i];
button.innerHTML = "Download previous saved transcription";
document.body.prepend(button);
}catch(e){console.error(e);}
}
} else if (oldSaves[i].startsWith("transcription_"+roomID+"_")){
localStorage.removeItem(i);
}
}
}
document.getElementById("shareLink").href= url+"/overlay?room="+roomID;
document.getElementById("shareLink").innerHTML = url+"/overlay?room="+roomID;
navigator.clipboard.writeText(url+"/overlay?room="+roomID).then(() => {
}, () => {
});
var apiKey = false;
var socket;
function connectWebSocket() {
if (socket) {
socket.close();
socket = null;
}
socket = new WebSocket("wss://api.caption.ninja:443");
socket.onclose = function() {
console.log("WebSocket connection closed. Attempting to reconnect...");
setTimeout(connectWebSocket, 500);
};
socket.onopen = function() {
console.log("WebSocket connected. Joining room...");
socket.send(JSON.stringify({"join":roomID}));
};
socket.onerror = function(error) {
console.error("WebSocket error:", error);
if (socket) {
socket.close();
socket = null;
}
setTimeout(connectWebSocket, 5000);
};
}
connectWebSocket();
var ends = 0;
var final_transcript = '';
var interim_transcript = "";
var recognition = null;
var idle = null;
setup();
function setup(){
if ('webkitSpeechRecognition' in window){
if (recognition) {
recognition.onend = null;
recognition.abort();
}
recognition = new webkitSpeechRecognition();
if (myLang){
recognition.lang = myLang;
}
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function(){
console.log("started transcription");
setTimeout(function(){
ends = 0;
}, 2000);
};
recognition.onerror = function(event){
console.log(event);
};
recognition.onend = function(e){
console.log(e);
console.log("Stopped transcription");
if (e.type === "end"){
ends +=1;
}
if (ends > 3){
alert("WARNING: Cannot enable transcription service\n\nThe service will fail completely if more than one transcription session is currently active on your computer.\n\nPlease close other Caption.Ninja tabs or other transcription services and then wait a few minutes before retrying.\n\nIt might also fail if your browser does not offer a transcription service; use the official Chrome browser instead if unsure.");
}
setup();
};
recognition.onresult = function(event){
counter+=1;
var interim_transcript = '';
if(typeof(event.results) == 'undefined'){
return;
}
let new_final_transcript = '';
for(var i = event.resultIndex; i < event.results.length; ++i){
if(event.results[i].isFinal){
new_final_transcript += ' ' + sanitize(event.results[i][0].transcript);
} else {
interim_transcript += " " +sanitize(event.results[i][0].transcript);
interim_transcript = interim_transcript.trim();
}
}
final_transcript = new_final_transcript.trim();
if (final_transcript){
console.log("FINAL:", final_transcript);
document.getElementById("output").innerHTML+="<span id='final_"+counter+"'>"+final_transcript+"</span><br />";
document.getElementById("interm").innerHTML="";
try {
if (timestamp_start===null){
timestamp_start = Date.now();
timestamp_intern = Date.now();
}
timestamp_stop = Date.now();
timestamp_stop = Date.now() - timestamp_start;
var date = new Date(0);
date.setSeconds(timestamp_stop/1000);
timestamp_stop = date.toISOString().replace(".",",").replace("1970-01-01T","");
timestamp_stop = timestamp_stop.substring(0, timestamp_stop.length - 1);
timestamp_intern -= timestamp_start;
var date = new Date(0);
date.setSeconds(timestamp_intern/1000);
timestamp_intern = date.toISOString().replace(".",",").replace("1970-01-01T","");
timestamp_intern = timestamp_intern.substring(0, timestamp_intern.length - 1);
} catch(e){console.error(e);}
try {
logcounter+=1;
srtFile += logcounter + "\n";
srtFile += timestamp_intern + " --\> " + timestamp_stop + "\n";
timestamp_intern=null;
srtFile += ">> " + final_transcript + "\n\n";
setStorage("transcription_"+roomID+"_"+timestamp_start, srtFile, 1);
if (logcounter===1){
var button = document.createElement("button");
button.onclick = function(){
download("transcription_"+roomID+"_"+timestamp_start+".srt", srtFile);
};
button.innerHTML = "Download transcription";
button.style.position = "fixed";
button.style.top = "0";
button.style.left = "0";
document.body.appendChild(button);
}
} catch(e){console.error(e);}
try {
if (label){
socket.send(JSON.stringify({"msg":true, "final":final_transcript, "id":counter, "label":label, "ln": myLang}));
} else {
socket.send(JSON.stringify({"msg":true, "final":final_transcript, "id":counter, "ln": myLang}));
}
final_transcript="";
interim_transcript="";
timestamp_start = null;
timestamp_stop = null;
timestamp_intern = null;
} catch(e){
console.error(e);
}
} else {
if (timestamp_start===null){
timestamp_start = Date.now();
}
if (timestamp_intern===null){
timestamp_intern = Date.now();
}
console.log("INTERM:", interim_transcript);
document.getElementById("interm").innerHTML="<span>"+interim_transcript+"</span><br />";
try {
if (label){
socket.send(JSON.stringify({"msg":true, "interm":interim_transcript, "id":counter, "label":label, "ln": myLang}));
} else {
socket.send(JSON.stringify({"msg":true, "interm":interim_transcript, "id":counter, "ln": myLang}));
}
} catch(e){
console.error(e);
interim_transcript="";
}
}
};
recognition.start();
}
}
</script>
</body>
</html>