-
Notifications
You must be signed in to change notification settings - Fork 20
/
bandersnatch.html
477 lines (407 loc) · 12.4 KB
/
bandersnatch.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bandersnatch</title>
<style>
body { margin: auto; text-align: center; overflow:hidden; }
video { width: 100%; height: 100%;}
.controls { margin: auto; width: 100%; height: 100%; margin-top: -5.8% }
ul { font: 20px sans-serif; display: flex; align-items: stretch; justify-content: space-around; width: 100%; margin: 10px 0 10px 0; padding: 0; }
li { display: block; flex: 0 1 auto; list-style-type: none; }
#progress {background-color: white; width: 0%; height: 4px; margin: auto; border-radius: 4px; position: relative; }
#choices { position: relative; top: 10px;}
#choiceCaption, #interactionZones, #nextSegments, #nextSegment { display:none; } /* comment to show links */
a {color: #ffffffa6; text-decoration: none; text-shadow: 0px 0px 10px #000; animation: choices 3s forwards; }
a:hover {color:white; }
a:active, a:focus {color:white; text-decoration: underline solid white }
@keyframes choices {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
video::cue {font: 60% sans-serif; color: white; background: none; text-shadow: 0px 0px 10px #000; }
#c { margin: auto; height: 100%; width: 100%; }
#c:-webkit-full-screen { width: 100vw; height: 100vh;}
#c:-moz-full-screen { width: 100vw; height: 100vh;}
#c:-ms-fullscreen { width: 100vw; height: 100vh;}
#c:fullscreen { width: 100vw; height: 100vh;}
</style>
<!-- bandersnatch object https://gist.githubusercontent.com/jonluca/860f3f445e7d84054822276fd058301a (add bandersnatch= line on top) -->
<script src="bandersnatch.js"></script>
<!-- SegmentMap object https://github.com/jolbol1/Bandersnatch/blob/master/SegmentMap.json (add SegmentMap= line on top) -->
<script src="SegmentMap.js"></script>
<script>
var segmentMap = SegmentMap;
var bv = bandersnatch.videos['80988062'].interactiveVideoMoments.value;
var choicePoints = bv.choicePointNavigatorMetadata.choicePointsMetadata.choicePoints;
var momentsBySegment = bv.momentsBySegment;
var segmentGroups = bv.segmentGroups;
var captions = {};
var currentSegment;
var currentMoment;
var nextSegment = null;
var momentSelected = null;
var persistentState = bv.stateHistory;
var globalChoices = {};
function msToString(ms) {
return new Date(ms).toUTCString().split(' ')[4];
}
function getCurrentMs() {
return Math.round(document.getElementById("video").currentTime * 1000.0);
}
function generateJs(cond) {
if (cond[0] == 'persistentState') {
return '!!persistentState["' + cond[1] + '"]';
} else if (cond[0] == 'not') {
return '!(' + generateJs(cond[1]) + ')';
} else if (cond[0] == 'and') {
var conds = [];
for (var i = 1; i < cond.length; i++) {
conds.push('(' + generateJs(cond[i]) + ')');
}
return '(' + conds.join(' && ') + ')';
} else if (cond[0] == 'or') {
var conds = [];
for (var i = 1; i < cond.length; i++) {
conds.push('(' + generateJs(cond[i]) + ')');
}
return '(' + conds.join(' || ') + ')';
} else {
console.log('unsupported condition!', cond);
return 'true';
}
}
function checkPrecondition(segmentId) {
let precondition = bv.preconditions[segmentId];
if (precondition) {
let cond = generateJs(precondition);
let match = eval(cond);
console.log(cond, '==', match);
return match;
}
return true;
}
function findSegment(id) {
if (id.startsWith('nsg-')) {
id = id.substr(4);
}
if (SegmentMap.segments[id]) {
// check precondition
return id;
}
if (segmentGroups[id]) {
for(v of segmentGroups[id]) {
if (v.segmentGroup) {
return findSegment(v.segmentGroup);
} else if (v.segment) {
// check precondition
return v.segment;
} else {
if (checkPrecondition(v))
return v;
}
}
}
return id;
}
function getChoiceMs(choiceId) {
var segmentId = findSegment(choiceId);
return getSegmentMs(segmentId);
}
function getSegmentId(ms) {
for(const [k, v] of Object.entries(SegmentMap.segments)) {
if (ms>=v.startTimeMs && ms<v.endTimeMs) {
return k;
}
}
return null;
}
function getSegmentMs(segmentId) {
return segmentMap.segments[segmentId].startTimeMs;
}
function getMoment(ms) {
for(const [k, v] of Object.entries(momentsBySegment)) {
for(r of v) if (r.type=='scene:cs_bs') {
if (ms>=r.startMs && ms<r.endMs) {
return r;
}
}
}
return null;
}
function newList(id) {
var ul = document.getElementById(id);
while (ul.firstChild) {
ul.removeChild(ul.firstChild);
}
return ul;
}
function addItem(ul, text, url) {
var li = document.createElement("li");
var a = document.createElement("a");
a.textContent = text;
a.setAttribute('href', url);
li.appendChild(a);
ul.appendChild(li);
}
function setNextSegment(segmentId, comment) {
console.log('setNextSegment', segmentId, comment);
nextSegment = segmentId;
var ul = newList("nextSegment");
var caption = 'nextSegment: '+segmentId;
addItem(ul, comment ? caption + ' ('+comment+')' : caption,
'javascript:playSegment("'+segmentId+'")');
}
function addZones(segmentId) {
var ul = newList("interactionZones");
var caption = 'currentSegment('+ segmentId + ')';
addItem(ul, caption, 'javascript:playSegment("'+segmentId+'")');
var v = segmentMap.segments[segmentId];
if (v && v.ui && v.ui.interactionZones) {
var index = 0;
for (z of v.ui.interactionZones) {
var startMs = z[0];
var stopMs = z[1];
var caption = segmentId+' interactionZone ' + index;
addItem(ul, caption, 'javascript:seek('+startMs+')');
index++;
}
}
var ul = newList("nextSegments");
for (const [k, v] of Object.entries(segmentMap.segments[segmentId].next)) {
var caption = captions[k] ? captions[k] : k;
if (segmentMap.segments[segmentId].defaultNext==k) {
caption = '['+caption+']';
setNextSegment(k);
}
addItem(ul, caption, 'javascript:playSegment("'+k+'")');
}
}
function addChoices(r) {
var ul = newList("choices");
document.getElementById("choiceCaption").innerHTML = '';
if (!r) return;
index = 0;
for (x of r.choices) {
console.log(x.id, 'choice saved');
globalChoices[x.id] = x;
var caption = r.defaultChoiceIndex==index ? '['+x.text+']' : x.text;
addItem(ul, caption, 'javascript:choice("'+
(x.segmentId ? x.segmentId : (x.sg ? x.sg : x.id))+'", "'+x.text+'", "'+x.id+'")');
index++;
}
document.getElementById("choiceCaption").innerHTML = choicePoints[r.id].description;
}
function updateProgressBar(ms, r) {
var p = 0;
if (r && ms>r.startMs && ms<r.endMs) {
p = 100 - Math.floor((ms - r.startMs) * 100 / (r.endMs - r.startMs));
}
document.getElementById("progress").style.width = p + '%';
}
var timerId = 0;
var switchFrom = null;
var switchTo = null;
function ontimeout(nextSegment) {
console.log('ontimeout', nextSegment);
if (switchFrom!=currentSegment || switchTo!=nextSegment) {
playSegment(nextSegment);
}
switchFrom = currentSegment;
switchTo = nextSegment;
}
function ontimeupdate(evt) {
var ms = getCurrentMs();
var segmentId = getSegmentId(ms);
// ontimeupdate resolution is about a second, better use timer
clearTimeout(timerId);
if (segmentId && nextSegment && nextSegment!=segmentId) {
var timeLeft = SegmentMap.segments[segmentId].endTimeMs - ms;
timerId = setTimeout(ontimeout, timeLeft, nextSegment);
}
if (currentSegment != segmentId) {
console.log('ontimeupdate', currentSegment, segmentId, ms, msToString(ms));
currentSegment = segmentId;
addZones(segmentId);
currentMoment = null;
addChoices(0);
}
var r = getMoment(ms);
if (r && momentSelected!=r.id) {
updateProgressBar(ms, r);
if (currentMoment!=r.id) {
currentMoment = r.id;
console.log('interaction', currentMoment);
addChoices(r);
}
} else {
currentMoment = null;
addChoices(0);
updateProgressBar(0);
}
}
function jumpForward(ms) {
var ms = getCurrentMs();
var segmentId = getSegmentId(ms);
var v = segmentMap.segments[segmentId];
var interactionMs = 0;
if (v && v.ui && v.ui.interactionZones) {
for (z of v.ui.interactionZones) {
var startMs = z[0];
var stopMs = z[1];
if (ms<startMs)
interactionMs = startMs;
}
}
if (interactionMs) {
seek(interactionMs);
} else {
playSegment(nextSegment);
}
}
function jumpBack() {
var ms = getCurrentMs();
var segmentId = getSegmentId(ms);
var startMs = getSegmentMs(segmentId);
var previousSegment = getSegmentId(startMs - 1000);
console.log('jumpBack from-to', segmentId, previousSegment);
playSegment(previousSegment);
}
function toggleFullScreen() {
console.log('toggleFullScreen');
var c = document.getElementById("c");
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {
if (c.requestFullscreen) {
c.requestFullscreen();
} else if (c.msRequestFullscreen) {
c.msRequestFullscreen();
} else if (c.mozRequestFullScreen) {
c.mozRequestFullScreen();
} else if (c.webkitRequestFullscreen) {
c.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}
function togglePlayPause() {
var v = document.getElementById("video");
if (v.paused) v.play(); else v.pause();
}
function onload() {
document.getElementById("video").ontimeupdate = ontimeupdate;
var c = document.getElementById("c");
c.ondblclick = toggleFullScreen;
c.onclick = function() {
// can't togglePlayPause here, choice buttons stop the video
// should use preventdefault or something
// use spacebar for now
// mind that autoplay is disabled in latest chrome, so play after click
document.getElementById("video").play();
};
document.onkeypress = function(e) {
if (e.code=='KeyF')
toggleFullScreen();
if (e.code=='KeyR')
playSegment(0);
if (e.code=='Space')
togglePlayPause();
}
document.onkeydown = function(evt) {
var v = document.getElementById("video");
if (evt.key=='ArrowLeft') {
jumpBack();
}
if (evt.key=='ArrowRight') {
jumpForward();
}
}
if (location.hash) {
var segmentId = location.hash.slice(1);
playSegment(segmentId);
}
}
function seek(ms) {
clearTimeout(timerId);
console.log('seek', ms);
momentSelected = null;
document.getElementById("video").currentTime = ms / 1000.0;
}
function choice(choiceId, text, id) {
var segmentId = findSegment(choiceId);
console.log('choice', choiceId, 'nextSegment', segmentId);
applyImpression(globalChoices[id]);
setNextSegment(segmentId, text);
momentSelected = choiceId;
addChoices(0);
}
function applyImpression(obj) {
if (!obj) {
return;
}
impressionData = obj.impressionData;
if (impressionData && impressionData.type == 'userState') {
for (const [variable, value] of Object.entries(impressionData.data.persistent)) {
console.log('persistentState set', variable, '=', value);
persistentState[variable] = value;
}
}
}
function applyPlaybackImpression(segmentId) {
let moments = momentsBySegment[segmentId];
if (!moments) {
console.log('warning - no moments');
return;
}
for (moment of moments) {
if (moment.type != 'notification:playbackImpression') {
continue;
}
applyImpression(moment);
}
}
function playSegment(segmentId) {
clearTimeout(timerId);
if (!segmentId || segmentId=="undefined")
segmentId = '1A';
console.log('playSegment', segmentId);
applyPlaybackImpression(segmentId);
location.hash = segmentId;
document.title = 'Bandersnatch - Chapter ' + segmentId;
var ms = getSegmentMs(segmentId);
seek(ms);
}
</script>
</head>
<body onload=onload()>
<div>
<div id="c">
<video id="video" loop autoplay src="Black.Mirror.Bandersnatch.2018.720p.WEB-DL.x264.DUAL.mkv">
<track default kind="captions" srclang="en" src="Black.Mirror.Bandersnatch.2018.720p.WEB-DL.x264.DUAL.en.vtt"/>
</video>
<div class="controls">
<div id="choiceCaption"></div>
<div id="progress"></div>
<div class="buttons">
<ul id="choices"></ul>
<ul id="interactionZones"></ul>
<ul id="nextSegments"></ul>
<ul id="nextSegment"></ul>
</div>
</div>
</div>
</div>
</body>
</html>