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
|
import Button from '@material-ui/core/Button';
import Ajv from 'ajv';
import { useEffect, useState } from 'react';
import Timecode from 'timecode-boss';
import timeline, { delaySlide, loopSlide, slide, speedChangeSlide } from '../timeline';
import * as timelineSchema from '../timeline.schema.json';
import ExitToAppRoundedIcon from '@material-ui/icons/ExitToAppRounded';
import PlayArrowRoundedIcon from '@material-ui/icons/PlayArrowRounded';
import SettingsRemoteRoundedIcon from '@material-ui/icons/SettingsRemoteRounded';
import SettingsRoundedIcon from '@material-ui/icons/SettingsRounded';
import CodeRoundedIcon from '@material-ui/icons/CodeRounded';
import MovieRoundedIcon from '@material-ui/icons/MovieRounded';
export class TimedVideoPlayer {
slide: number;
timeline: timeline;
precision: number;
player: HTMLVideoElement;
video: string;
registeredEventListeners: boolean;
frame: number;
framerate: number;
constructor() {
this.slide = -1;
this.precision = 3;
this.frame = 0;
this.framerate = 0;
this.registeredEventListeners = false;
}
frameToTimestampString(frame: number, trim: boolean = true) {
var timecodeString = new Timecode(frame, this.framerate).toString();
if (trim) timecodeString = timecodeString.replace(/^(00:)+/, '');
timecodeString = timecodeString.replace(';', '.')
.replace(/(:)(\d+?)$/, '.$2')
+ 'f';
if (timecodeString == '00f') timecodeString = '0';
return timecodeString;
}
timestampToFrame(timestamp: number): number {
return Math.ceil((timestamp * 1e3) / (1e3 / this.framerate));
}
frameToTimestamp(frame: number): number {
return frame / this.framerate;
}
registerPlayer(player: HTMLVideoElement) {
this.player = player;
if (this.video) this.player.src = this.video;
this.registerEventListeners();
}
jumpToFrame(frame: number) {
this.player.currentTime = this.frameToTimestamp(frame);
this.frame = frame;
}
jumpToSlide(slide: slide) {
this.jumpToFrame(slide.frame);
this.player.playbackRate = this.getPlaybackSpeed(this.slide);
this.player.pause();
}
getPlaybackSpeed(slide: number) {
for (var i = slide; i > -1; i--) {
var previousSlide = this.timeline.slides[i];
if (previousSlide.type != 'speedChange') {
continue;
}
return this.framerate / (previousSlide as speedChangeSlide).newFramerate;
}
return 1;
}
handleSlide(slide: slide) {
switch (slide.type) {
case 'loop': {
this.jumpToFrame((slide as loopSlide).beginFrame);
break;
}
case 'delay': {
this.player.playbackRate = 0;
this.slide++;
var event = new CustomEvent('TimedVideoPlayerSlide', { detail: this.slide });
this.dispatchEvent(event);
setTimeout(() => {
this.player.playbackRate = this.getPlaybackSpeed(this.slide - 1);
}, (slide as delaySlide).delay);
break;
}
case 'speedChange': {
this.slide++;
var event = new CustomEvent('TimedVideoPlayerSlide', { detail: this.slide });
this.dispatchEvent(event);
this.player.playbackRate = this.framerate / (slide as speedChangeSlide).newFramerate;
break;
}
default: {
this.jumpToSlide(slide);
break;
}
}
}
addEventListener(name: string, callback: (...args: any[]) => void) {
return document.addEventListener(name, callback, false);
}
dispatchEvent(event: CustomEvent) {
return document.dispatchEvent(event);
}
registerEventListeners() {
if (
!this.video
|| !this.player
|| !this.timeline
|| this.registeredEventListeners
) {
return;
}
setInterval(() => {
if (this.player.paused) return;
var lastFrame = this.frame;
this.frame = this.timestampToFrame(this.player.currentTime);
var event = new CustomEvent('TimedVideoPlayerOnFrame', { detail: this.frame });
this.dispatchEvent(event);
var slide = this.timeline.slides[this.slide];
if (!slide) return;
if (this.frame >= slide.frame) {
this.handleSlide(slide);
}
}, 1e3 / (this.precision * this.framerate));
this.registeredEventListeners = true;
}
loadVideo(base64Video: string) {
this.video = base64Video;
if (this.player) this.player.src = this.video;
this.registerEventListeners();
}
loadSlides(jsonString: string) {
try {
var timeline = JSON.parse(jsonString);
} catch (e) {
console.log('invalid json object!' + e);
return;
}
var ajv = new Ajv({ allErrors: true });
var validate = ajv.compile(timelineSchema);
if (!validate(timeline)) {
console.log('schema not passed!');
return;
}
this.timeline = timeline as timeline;
this.framerate = this.timeline.framerate;
this.timeline.slides[-1] = {
frame: 0,
type: 'default',
clickThroughBehaviour: 'ImmediatelySkip',
};
this.registerEventListeners();
}
skip() {
var slide = this.timeline.slides[this.slide - 1];
if (slide.clickThroughBehaviour == 'ImmediatelySkip') this.jumpToSlide(slide);
}
next() {
if (!this.registeredEventListeners) return;
this.slide++;
var event = new CustomEvent('TimedVideoPlayerSlide', { detail: this.slide });
this.dispatchEvent(event);
var slide = this.timeline.slides[this.slide];
if (!this.player.paused && this.frame < slide?.frame) {
this.skip();
}
this.player.play();
}
previous() {
if (!this.registeredEventListeners) return;
this.slide = Math.max(this.slide - 1, -1);
var event = new CustomEvent('TimedVideoPlayerSlide', { detail: this.slide });
this.dispatchEvent(event);
var slide = this.timeline.slides[this.slide];
if (!slide) return;
this.jumpToSlide(slide);
}
}
export default function Present() {
var [dummy, setDummy] = useState(false);
var rerender = () => setDummy(!dummy);
var [player, setPlayer] = useState(new TimedVideoPlayer());
useEffect(() => {
setInterval(() => {
document.getElementById('time').innerText = new Date().toLocaleTimeString();
}, 500);
}, []);
useEffect(() => {
var videoEL = document.getElementById('player') as HTMLVideoElement;
player.registerPlayer(videoEL);
}, []);
return <div className='presentation posfix a0 h100vh'>
<div className='slideWrapper abscenterv posrel'>
<div className='slide posrel'>
<div className='innner posabs a0'>
<video id='player' className='fullwidth' />
</div>
</div>
</div>
<div className='fullscreenControls posabs a0'>
<div
className='control previous'
onClick={() => {
player.previous();
rerender();
}}
/>
<div
className='control menu'
onClick={() => {
document.getElementById('menu').classList.add('active');
}}
/>
<div
className='control next'
onClick={() => {
player.next();
rerender();
}}
/>
</div>
<div className='menu posabs a0' id='menu'>
<div
className='background posabs a0'
onClick={() => {
document.getElementById('menu').classList.remove('active');
}}
/>
<div className='info sidebyside posabs h0 b0'>
<div className='timetitle floatb'>
<h3 className='time numbers nobr' id='time'>14:00:41</h3>
<h1 className='title nobr'>{player.timeline?.name || '???'}</h1>
</div>
<div className='buttons floatb'>
<div className='inner center'>
{false && <Button
variant='contained'
color='default'
className='bg-err'
startIcon={<ExitToAppRoundedIcon />}
children='Stop presentation'
/>}
<Button
variant='contained'
color='default'
startIcon={<PlayArrowRoundedIcon />}
children='Resume presentation'
onClick={() => {
document.getElementById('menu').classList.remove('active');
}}
/>
<input
type='file'
id='vidUpload'
accept='video/*'
className='dispnone'
onChange={event => {
var file = event.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.addEventListener('load', ev => {
player.loadVideo(ev.target.result as string);
});
reader.readAsDataURL(file);
}}
/>
<input
type='file'
id='jsonUpload'
accept='application/json'
className='dispnone'
onChange={event => {
var file = event.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.addEventListener('load', ev => {
player.loadSlides(ev.target.result as string);
rerender();
});
reader.readAsText(file);
}}
/>
<Button
variant='contained'
color='default'
children='Load video'
startIcon={<MovieRoundedIcon />}
onClick={() => document.getElementById('vidUpload').click()}
/>
<Button
variant='contained'
color='default'
children='Load json'
startIcon={<CodeRoundedIcon />}
onClick={() => document.getElementById('jsonUpload').click()}
/>
{false && <Button
variant='contained'
color='default'
startIcon={<SettingsRemoteRoundedIcon />}
children='Connect remote'
/>}
{false && <Button
variant='contained'
color='default'
startIcon={<SettingsRoundedIcon />}
children='Settings'
/>}
</div>
</div>
<div className='slide posrel floatb'>
<h3 className='time numbers nobr posrel'>
slide {player.slide + 1}/{player.timeline?.slides.length || 0}
</h3>
</div>
</div>
</div>
</div>;
}
|