Repository navigation
Expand file tree
/
Copy pathrecord.js
More file actions
1195 lines (1072 loc) · 42.4 KB
/
Copy pathrecord.js
File metadata and controls
1195 lines (1072 loc) · 42.4 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Records a walkthrough video of filling a split along with its subtitles, with no dependencies:
// builds the application, serves the build, drives Google Chrome via the DevTools protocol,
// encodes the screencast by ffmpeg and adds the captions as a subtitles track and file,
// optionally also as a bar below the page. The captions are voiced by Piper when the locale
// has a voice, and the clicks and key presses are heard too. Needs a running Blobs backend,
// Chrome, ffmpeg and Piper for the voice.
// Run with `npm run demo -- --locale ru`, see the options below.
import { spawn, spawnSync } from 'node:child_process'
import {
createReadStream,
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync
} from 'node:fs'
import { createServer } from 'node:http'
import { homedir, tmpdir } from 'node:os'
import { extname, join, resolve } from 'node:path'
import { performance } from 'node:perf_hooks'
import { setTimeout as sleep } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'
import { parseArgs } from 'node:util'
import content from './content.js'
const ROOT = fileURLToPath(new URL('../..', import.meta.url))
const { values: options } = parseArgs({
options: {
locale: { type: 'string', default: 'ru' },
backend: { type: 'string', default: 'ws://localhost:3000' },
out: { type: 'string', default: 'videos' },
chrome: { type: 'string', default: process.env.CHROME || 'google-chrome' },
// Slows down (> 1) or speeds up (< 1) all the pauses and movements
pace: { type: 'string', default: '1' },
// Captions are always saved as subtitles, this option also renders them in a bar below
// the page, needs ffmpeg built with libass
captions: { type: 'boolean', default: false },
// Directory with the Piper binary in bin/piper and the voice models in voices
piper: { type: 'string', default: join(homedir(), '.cache/split-demo/piper') },
// Records the video without the voice
mute: { type: 'boolean', default: false },
// Voice model to try instead of the locale one, e.g. ru_RU-denis-medium, along with its speed
// where values below 1 speed it up
voice: { type: 'string' },
'length-scale': { type: 'string', default: '1' },
// Records the video without the sounds of the clicks and key presses
'no-sounds': { type: 'boolean', default: false },
// Prints the phonemes of the captions speech to check the pronunciation, records nothing
phonemes: { type: 'boolean', default: false },
// Saves an example of the typing and clicking sounds, records nothing
'sounds-sample': { type: 'boolean', default: false },
// Shows the browser window, for debugging of the scenario
show: { type: 'boolean', default: false }
}
})
// The page is laid out in CSS pixels of a 720p screen and rendered with the scale, so the video
// is Full HD while the application is as large in it as in a 720p one
const WIDTH = 1280
const HEIGHT = 720
const SCALE = 1.5
const FPS = 30
// The captions bar is added below the page, so it never covers the application
const BAR_HEIGHT = options.captions ? 96 : 0
const PAGE_HEIGHT = HEIGHT - BAR_HEIGHT
// Sizes of the video frames in device pixels
const VIDEO_WIDTH = WIDTH * SCALE
const VIDEO_HEIGHT = HEIGHT * SCALE
const VIDEO_PAGE_HEIGHT = PAGE_HEIGHT * SCALE
const BAR_COLOR = '5c6bc0'
const CAPTION_FONT = 'Noto Sans'
const PACE = Number(options.pace)
// Delay after every typed character in milliseconds, varied between the bounds like a person types
const TYPING_DELAY = [80, 140]
const locale = options.locale
const data = content[locale]
if (!data) throw new Error(`No demo content for the "${locale}" locale`)
const messages = JSON.parse(readFileSync(join(ROOT, 'src/locales', `${locale}.json`), 'utf8'))
function t(key) {
if (!(key in messages)) throw new Error(`No "${key}" message in the "${locale}" locale`)
return messages[key]
}
const wait = (ms) => sleep(ms * PACE)
const captionText = (caption) => (typeof caption === 'string' ? caption : caption.text)
// Spells out the words the voice reads poorly, longer ones first, e.g. a link before its domain
const pronunciations = Object.entries(data.pronunciations || {})
.sort(([a], [b]) => b.length - a.length)
.map(([word, speech]) => {
const escaped = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
return [new RegExp(`(?<![\\p{L}\\d])${escaped}(?![\\p{L}\\d])`, 'giu'), speech]
})
function captionSpeech(caption) {
if (typeof caption !== 'string' && caption.speech) return caption.speech
return pronunciations.reduce(
(speech, [pattern, replacement]) => speech.replace(pattern, replacement),
captionText(caption)
)
}
// Build and serving
function build(dir) {
console.log('Building the application...')
const result = spawnSync(
join(ROOT, 'node_modules/.bin/vite'),
['build', '--outDir', dir, '--emptyOutDir'],
{
cwd: ROOT,
// Variables from the environment take precedence over the .env files
env: { ...process.env, VITE_BACKEND_URL: options.backend },
stdio: ['ignore', 'ignore', 'inherit']
}
)
if (result.status !== 0) throw new Error('Unable to build the application')
}
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.json': 'application/json',
'.webmanifest': 'application/manifest+json',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
}
async function serve(dir) {
const server = createServer((req, res) => {
const path = decodeURIComponent(new URL(req.url, 'http://localhost').pathname)
let file = resolve(join(dir, path))
if (!file.startsWith(dir) || !existsSync(file) || path.endsWith('/')) {
file = join(dir, 'index.html')
}
res.writeHead(200, { 'Content-Type': MIME_TYPES[extname(file)] || 'application/octet-stream' })
createReadStream(file).pipe(res)
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
return server
}
// Voice
function synthesize(text, { model, speaker = 0, lengthScale = 1 }) {
const modelFile = join(options.piper, 'voices', `${model}.onnx`)
const result = spawnSync(
join(options.piper, 'bin/piper/piper'),
[['--model', modelFile, '--speaker', speaker, '--length_scale', lengthScale], '--output_raw']
.flat()
.map(String),
{ input: text, maxBuffer: 1 << 28, stdio: ['pipe', 'pipe', 'ignore'] }
)
if (result.status !== 0) throw new Error(`Unable to synthesize "${text}"`)
return result.stdout
}
// eSpeak voices phonemizing the captions of the locales
const ESPEAK_VOICES = { en: 'en-us', ru: 'ru', es: 'es', pt: 'pt-br', de: 'de', fr: 'fr-fr' }
// Prints the speech of every caption along with its phonemes, where ˈ marks the stressed vowel
function printPhonemes() {
const dir = join(options.piper, 'bin/piper')
for (const caption of Object.values(data.captions)) {
const speech = captionSpeech(caption)
const result = spawnSync(
join(dir, 'espeak-ng'),
[`--path=${dir}`, '-v', ESPEAK_VOICES[locale], '-q', '--ipa', speech],
{ env: { ...process.env, LD_LIBRARY_PATH: dir }, encoding: 'utf8' }
)
if (result.status !== 0) throw new Error('Unable to run eSpeak')
console.log(
`${captionText(caption)}\n ${speech}\n ${result.stdout.trim().replace(/\n/g, ' ')}\n`
)
}
}
const readVoiceRate = ({ model }) =>
JSON.parse(readFileSync(join(options.piper, 'voices', `${model}.onnx.json`), 'utf8')).audio
.sample_rate
// Loudness normalization of the audio track, the limiter keeps its peaks below -1.5 dB
const AUDIO_FILTERS = 'loudnorm=I=-16:TP=-1.5,alimiter=limit=0.84:level=false'
// Speeches of the captions placed at the times they're shown
class Narration {
#clips = []
constructor(voice) {
this.rate = readVoiceRate(voice)
this.speeches = Object.fromEntries(
Object.entries(data.captions).map(([key, caption]) => [
key,
synthesize(captionSpeech(caption), voice)
])
)
}
// Places the speech at the time and returns its duration in seconds
add(key, time) {
const speech = this.speeches[key]
this.#clips.push({ time, speech })
return speech.length / 2 / this.rate
}
// Adds the speeches to the samples ranging from -1 to 1
mix(samples) {
for (const { time, speech } of this.#clips) {
const start = Math.round(time * this.rate)
const length = Math.min(speech.length / 2, samples.length - start)
for (let i = 0; i < length; i++) samples[start + i] += speech.readInt16LE(i * 2) / 32768
}
}
}
// Recorded sounds of the key presses and mouse clicks, see the sources in sounds/README.md.
// Every event takes a random variant with a slightly varied volume, so the typing doesn't sound
// mechanical, the levels are the peaks of the sounds
const SOUNDS = {
key: { variants: ['key-1', 'key-2', 'key-3', 'key-4', 'key-5', 'key-6'], level: 0.07 },
press: { variants: ['mouse-press'], level: 0.15 },
release: { variants: ['mouse-release'], level: 0.1 }
}
const SOUNDS_DIR = fileURLToPath(new URL('sounds', import.meta.url))
// Decodes the sound into samples of the rate with the peak of 1
function loadSound(name, rate) {
const result = spawnSync(
'ffmpeg',
['-loglevel', 'error', '-i', join(SOUNDS_DIR, `${name}.ogg`)]
.concat(['-ac', '1', '-ar', rate, '-f', 'f32le', '-'])
.map(String),
{ maxBuffer: 1 << 24 }
)
if (result.status !== 0) throw new Error(`Unable to load the "${name}" sound`)
const { buffer, byteOffset, length } = result.stdout
const samples = new Float32Array(buffer.slice(byteOffset, byteOffset + length))
const peak = samples.reduce((max, value) => Math.max(max, Math.abs(value)), 0)
return samples.map((value) => value / peak)
}
class Sounds {
#events = []
#seed = 1
#loaded = new Map()
add(kind, time = recorder?.time()) {
if (time !== undefined) this.#events.push({ kind, time })
}
// Seeded, so the same scenario sounds the same
#random() {
this.#seed = (this.#seed * 1103515245 + 12345) % 2147483648
return this.#seed / 2147483648
}
#load(name, rate) {
if (!this.#loaded.has(name)) this.#loaded.set(name, loadSound(name, rate))
return this.#loaded.get(name)
}
// Adds the sounds to the samples ranging from -1 to 1
mix(samples, rate) {
for (const { kind, time } of this.#events) {
const { variants, level } = SOUNDS[kind]
const sound = this.#load(variants[Math.floor(this.#random() * variants.length)], rate)
const gain = level * (0.85 + 0.3 * this.#random())
const start = Math.round(time * rate)
const length = Math.min(sound.length, samples.length - start)
for (let i = 0; i < length; i++) samples[start + i] += sound[i] * gain
}
}
}
// Saves an example of a caption spoken by the locale voice followed by typing and clicking,
// mixed and normalized as in the video, to tune the sounds without recording one
function saveSoundsSample(file) {
const sample = new Sounds()
const speech = data.voice && synthesize(captionSpeech(data.captions.greeting), data.voice)
const rate = data.voice ? readVoiceRate(data.voice) : SOUNDS_RATE
let time = 0.3 + (speech ? speech.length / 2 / rate + 0.5 : 0)
for (const word of ['Weekend', 'at', 'the', 'lake', 'trip']) {
for (let i = 0; i < word.length + 1; i++) {
sample.add('key', time)
time += (TYPING_DELAY[0] + Math.random() * (TYPING_DELAY[1] - TYPING_DELAY[0])) / 1000
}
}
for (let i = 0; i < 3; i++) {
time += 0.8
sample.add('press', time)
sample.add('release', time + 0.08)
}
const samples = new Float32Array(Math.ceil((time + 1) * rate))
if (speech) {
const start = Math.round(0.3 * rate)
for (let i = 0; i < speech.length / 2; i++)
samples[start + i] = speech.readInt16LE(i * 2) / 32768
}
sample.mix(samples, rate)
const pcm = Buffer.from(Int16Array.from(samples, (value) => Math.round(value * 32767)).buffer)
const result = spawnSync(
'ffmpeg',
[
['-loglevel', 'error', '-y', '-f', 's16le', '-ar', rate, '-ac', '1', '-i', '-'],
['-af', AUDIO_FILTERS, file]
]
.flat()
.map(String),
{ input: pcm }
)
if (result.status !== 0) throw new Error('Unable to save the sounds sample')
console.log(`Saved ${file}`)
}
// Sample rate of the audio track when there's no voice to take it from
const SOUNDS_RATE = 22050
// Saves the voice and the sounds as raw 16-bit mono samples, returns their rate
function saveAudio(file, duration) {
const rate = narration?.rate || SOUNDS_RATE
const samples = new Float32Array(Math.ceil(duration * rate))
narration?.mix(samples)
if (!options['no-sounds']) sounds.mix(samples, rate)
const pcm = Buffer.alloc(samples.length * 2)
samples.forEach((value, index) => {
pcm.writeInt16LE(Math.max(-32768, Math.min(32767, Math.round(value * 32767))), index * 2)
})
writeFileSync(file, pcm)
return rate
}
// Chrome and the DevTools protocol
async function launchChrome(profileDir) {
const args = [
`--user-data-dir=${profileDir}`,
'--remote-debugging-port=0',
`--window-size=${WIDTH},${PAGE_HEIGHT}`,
`--lang=${locale}`,
'--no-first-run',
'--no-default-browser-check',
'--disable-extensions',
'--hide-scrollbars',
'--mute-audio',
'--force-color-profile=srgb',
'about:blank'
]
if (!options.show) args.unshift('--headless=new')
const chrome = spawn(options.chrome, args, { stdio: ['ignore', 'ignore', 'pipe'] })
const browserUrl = await new Promise((resolve, reject) => {
let output = ''
chrome.on('error', reject)
chrome.on('exit', () => reject(new Error(`Chrome exited unexpectedly:\n${output}`)))
chrome.stderr.on('data', (chunk) => {
output += chunk
const matched = output.match(/DevTools listening on (ws:\/\/\S+)/)
if (matched) resolve(matched[1])
})
})
const { host } = new URL(browserUrl)
const targets = await (await fetch(`http://${host}/json/list`)).json()
const target = targets.find(({ type }) => type === 'page')
return { chrome, pageUrl: target.webSocketDebuggerUrl }
}
class Connection {
#ws
#nextId = 1
#pending = new Map()
#listeners = new Map()
static async open(url) {
const connection = new Connection()
connection.#ws = new WebSocket(url)
connection.#ws.addEventListener('message', (event) => connection.#receive(event.data))
await new Promise((resolve, reject) => {
connection.#ws.addEventListener('open', resolve, { once: true })
connection.#ws.addEventListener('error', reject, { once: true })
})
return connection
}
#receive(raw) {
const message = JSON.parse(raw)
if (message.id) {
const { resolve, reject } = this.#pending.get(message.id)
this.#pending.delete(message.id)
if (message.error) reject(new Error(`${message.error.message} ${message.error.data || ''}`))
else resolve(message.result)
} else {
this.#listeners.get(message.method)?.(message.params)
}
}
send(method, params = {}) {
const id = this.#nextId++
this.#ws.send(JSON.stringify({ id, method, params }))
return new Promise((resolve, reject) => this.#pending.set(id, { resolve, reject }))
}
on(method, listener) {
this.#listeners.set(method, listener)
}
close() {
this.#ws.close()
}
}
// Video recording
class Recorder {
#cdp
#ffmpeg
#closed
#start
#frame
#written = 0
constructor(cdp, file) {
this.#cdp = cdp
this.#ffmpeg = spawn(
'ffmpeg',
[
['-y', '-loglevel', 'error'],
['-f', 'image2pipe', '-c:v', 'mjpeg', '-framerate', FPS, '-i', 'pipe:0'],
['-vf', `scale=${VIDEO_WIDTH}:${VIDEO_PAGE_HEIGHT},format=yuv420p`],
// Nearly lossless intermediate video, it's encoded again along with the captions
['-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '8', file]
]
.flat()
.map(String),
{ stdio: ['pipe', 'inherit', 'inherit'] }
)
this.#closed = new Promise((resolve) => this.#ffmpeg.on('close', resolve))
}
// Seconds since the start of the recording
time() {
return (performance.now() - this.#start) / 1000
}
async start() {
this.#cdp.on('Page.screencastFrame', ({ data, sessionId }) => {
this.#cdp.send('Page.screencastFrameAck', { sessionId }).catch(() => {})
this.#flush()
this.#frame = Buffer.from(data, 'base64')
})
this.#start = performance.now()
await this.#cdp.send('Page.startScreencast', {
format: 'jpeg',
quality: 95,
maxWidth: VIDEO_WIDTH,
maxHeight: VIDEO_PAGE_HEIGHT
})
}
// Chrome sends frames only when the page changes, so the last frame is
// repeated until the next one to keep the video frame rate constant
#flush() {
if (!this.#frame) return
const due = Math.round(this.time() * FPS)
for (; this.#written < due; this.#written++) this.#ffmpeg.stdin.write(this.#frame)
}
async stop() {
await this.#cdp.send('Page.stopScreencast')
this.#flush()
this.#ffmpeg.stdin.end()
await this.#closed
}
}
// ISO 639-2 codes of the locales for the subtitles track
const LANGUAGES = { en: 'eng', ru: 'rus', es: 'spa', pt: 'por', de: 'deu', fr: 'fra' }
// Encodes the final video with the subtitles track, the optional captions bar and audio
function compose({ source, subtitlesFile, barFile, audioFile, audioRate, file }) {
console.log('Encoding the video...')
const filters = [`pad=${VIDEO_WIDTH}:${VIDEO_HEIGHT}:0:0:color=0x${BAR_COLOR}`]
if (BAR_HEIGHT) filters.push(`ass=${barFile}`)
const audioInput = audioFile ? ['-f', 's16le', '-ar', audioRate, '-ac', '1', '-i', audioFile] : []
const audioOutput = audioFile
? ['-map', '2:a', '-af', AUDIO_FILTERS, '-c:a', 'aac', '-b:a', '160k']
: []
const result = spawnSync(
'ffmpeg',
[
['-y', '-loglevel', 'error', '-i', source, '-i', subtitlesFile, ...audioInput],
['-map', '0:v', '-map', '1:s', ...audioOutput, '-vf', filters.join(',')],
['-c:v', 'libx264', '-preset', 'slow', '-crf', '18', '-tune', 'animation'],
['-c:s', 'mov_text', '-metadata:s:s:0', `language=${LANGUAGES[locale] || 'und'}`],
['-metadata:s:a:0', `language=${LANGUAGES[locale] || 'und'}`],
['-pix_fmt', 'yuv420p', '-movflags', '+faststart', file]
]
.flat()
.map(String),
{ stdio: 'inherit' }
)
if (result.status !== 0) throw new Error('Unable to encode the video')
}
class Subtitles {
#cues = []
add(start, text) {
this.end(start)
this.#cues.push({ start, end: undefined, text })
}
end(time) {
const last = this.#cues.at(-1)
if (last && last.end === undefined) last.end = time
}
save(file) {
const stamp = (seconds) => new Date(seconds * 1000).toISOString().slice(11, 23)
const cues = this.#cues.map(
({ start, end, text }, index) => `${index + 1}\n${stamp(start)} --> ${stamp(end)}\n${text}`
)
writeFileSync(file, `WEBVTT\n\n${cues.join('\n\n')}\n`)
}
// Captions for the bar in the ASS format rendered by ffmpeg, it scales them to the video
saveBar(file) {
const stamp = (seconds) => new Date(seconds * 1000).toISOString().slice(12, 22)
const position = `\\an5\\pos(${WIDTH / 2},${PAGE_HEIGHT + BAR_HEIGHT / 2})\\fad(250,250)`
const events = this.#cues.map(
({ start, end, text }) =>
`Dialogue: 0,${stamp(start)},${stamp(end)},Caption,,0,0,0,,{${position}}` +
text.replace(/[{}\\]/g, '')
)
writeFileSync(
file,
[
'[Script Info]',
'ScriptType: v4.00+',
`PlayResX: ${WIDTH}`,
`PlayResY: ${HEIGHT}`,
'WrapStyle: 0',
'',
'[V4+ Styles]',
'Format: Name, Fontname, Fontsize, PrimaryColour, Bold, BorderStyle, Outline, Shadow, ' +
'Alignment, MarginL, MarginR, MarginV',
`Style: Caption,${CAPTION_FONT},28,&H00FFFFFF,0,1,0,0,5,80,80,0`,
'',
'[Events]',
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
...events,
''
].join('\n')
)
}
}
// Script evaluated in the page before the application: sets the locale, adds a visible mouse
// cursor, the title card shown at the start and the end, and the chat message showing the copied
// text, along with the helpers locating the application elements by their texts
const PAGE_SCRIPT = `(() => {
try {
localStorage.setItem('split:locale', ${JSON.stringify(JSON.stringify(locale))})
} catch {}
const style = document.createElement('style')
style.textContent = \`
#demo-cursor {
position: fixed; left: 0; top: 0; z-index: 2147483646; pointer-events: none;
width: 26px; height: 26px; transform: translate(${WIDTH / 2}px, ${PAGE_HEIGHT / 2}px);
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
}
#demo-cursor::before {
content: ''; position: absolute; left: -16px; top: -16px; width: 32px; height: 32px;
border-radius: 50%; background: rgba(255, 193, 7, 0.55); transform: scale(0); opacity: 0;
}
#demo-cursor.pressed::before { animation: demo-press 0.45s ease-out; }
@keyframes demo-press {
0% { transform: scale(0.3); opacity: 1; }
100% { transform: scale(1.4); opacity: 0; }
}
/* The pulsing badges hinting at the menu items only distract in the video */
.hint-badge { display: none !important; }
#demo-card {
position: fixed; inset: 0; z-index: 2147483647; pointer-events: none;
display: flex; flex-direction: column; align-items: center; justify-content: center;
background: #5c6bc0; color: #fff; font-family: Roboto, sans-serif; text-align: center;
opacity: 0; transition: opacity 0.8s ease;
}
#demo-card::before {
content: ''; position: absolute; inset: 0; background: rgba(255, 255, 255, 0.07);
mask: url('/pattern.svg') 0 0 / 480px 480px repeat;
}
#demo-card.shown { opacity: 1; }
#demo-card.instant { transition: none; }
#demo-card > * { position: relative; }
#demo-card.shown > * { animation: demo-rise 0.9s ease both; }
#demo-card.shown > :nth-child(2) { animation-delay: 0.15s; }
#demo-card.shown > :nth-child(3) { animation-delay: 0.3s; }
#demo-card.shown > :nth-child(4) { animation-delay: 0.45s; }
#demo-card img { width: 112px; height: 112px; margin-bottom: 20px; }
#demo-card .title { font-size: 72px; font-weight: 700; line-height: 1.1; }
#demo-card .subtitle { font-size: 30px; margin-top: 12px; }
#demo-card .link { font-size: 22px; margin-top: 40px; opacity: 0.8; }
@keyframes demo-rise {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: none; }
}
#demo-chat {
position: fixed; right: 40px; bottom: 12px; z-index: 2147483645; pointer-events: none;
width: max-content; min-width: 400px; max-width: calc(100vw - 80px);
border-radius: 16px; overflow: hidden; font-family: Roboto, sans-serif;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3); opacity: 0; transform: translateY(24px);
transition: opacity 0.4s ease, transform 0.4s ease;
}
#demo-chat.shown { opacity: 1; transform: none; }
#demo-chat .head {
padding: 12px 18px; background: #5c6bc0; color: #fff; font-size: 16px; font-weight: 500;
}
#demo-chat .body { padding: 18px; background: #eceff8; }
#demo-chat .message {
margin-left: 40px; padding: 10px 14px; border-radius: 14px 14px 4px 14px;
background: #fff; color: #212121; font-size: 15px; line-height: 1.5;
white-space: pre; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}
\`
const cursor = document.createElement('div')
cursor.id = 'demo-cursor'
cursor.innerHTML = '<svg width="26" height="26" viewBox="0 0 26 26">' +
'<path d="M3 2 L3 21 L8 16.5 L11.5 24 L15 22.5 L11.5 15 L18 15 Z" ' +
'fill="#fff" stroke="#111" stroke-width="1.6" stroke-linejoin="round"/></svg>'
const card = document.createElement('div')
card.id = 'demo-card'
card.innerHTML = '<img src="/favicon.svg" alt=""><div class="title">Split</div>' +
'<div class="subtitle"></div><div class="link">split.mayfleet.com</div>'
const chat = document.createElement('div')
chat.id = 'demo-chat'
chat.innerHTML = '<div class="head"></div><div class="body"><div class="message"></div></div>'
chat.querySelector('.head').textContent = ${JSON.stringify(data.title)}
document.addEventListener('DOMContentLoaded', () => {
// A fresh profile may check the spelling with a dictionary of another language
document.documentElement.spellcheck = false
document.head.append(style)
document.body.append(cursor, card, chat)
})
// The application copies the text by the clipboard API or by the copy command
// as a fallback, so both are watched to show the copied text in the chat
let copied = ''
const writeText = navigator.clipboard?.writeText?.bind(navigator.clipboard)
if (writeText) {
navigator.clipboard.writeText = (value) => {
copied = value
return writeText(value)
}
}
window.addEventListener('copy', () => {
copied = document.getSelection()?.toString() || copied
}, true)
window.addEventListener('mousemove', (event) => {
cursor.style.transform = \`translate(\${event.clientX - 3}px, \${event.clientY - 2}px)\`
}, true)
window.addEventListener('mousedown', () => {
cursor.classList.remove('pressed')
void cursor.offsetWidth
cursor.classList.add('pressed')
}, true)
const text = (el) => (el ? el.textContent.replace(/\\s+/g, ' ').trim() : '')
const all = (selector, root = document) =>
root ? [...root.querySelectorAll(selector)].filter((el) => el.getClientRects().length) : []
const leaving = (el) => el.classList.contains('fadeOut')
let target
window.__demo = {
// Shows the title card with the subtitle or hides it when there's none
card(subtitle, instant) {
card.classList.toggle('instant', !!instant)
if (subtitle) card.querySelector('.subtitle').textContent = subtitle
card.classList.toggle('shown', !!subtitle)
},
copied: () => copied,
// Shows the copied text in the chat, one line per settlement, and tells whether it fits
chat(shown) {
const message = chat.querySelector('.message')
message.textContent = copied
chat.classList.toggle('shown', shown)
return message.scrollWidth <= message.clientWidth
},
ready: () => !!document.querySelector('.app.ready section'),
synced: () => !document.querySelector('.header .q-badge.bg-negative'),
dialog: () => all('.backdrop').filter((el) => !leaving(el)).pop()?.querySelector('.q-card'),
menu: () => all('.q-menu').length > 0,
header: () => document.querySelector('.header .cursor-pointer'),
menuButton: () => document.querySelector('.btn-menu'),
section: (title) => all('section').find((el) => text(el.querySelector('.text-h6')) === title),
// The first button next to the section title, e.g. adding a new item
headerButton: (title) => __demo.section(title)?.querySelector('.q-btn'),
remove: (title) => __demo.section(title)?.querySelector('.q-btn.text-negative'),
// Checkbox of the section row having a cell with the value
select: (title, value) =>
all('tbody tr', __demo.section(title))
.find((row) => [...row.children].some((cell) => text(cell) === value))
?.querySelector('.q-checkbox'),
field: (label) =>
all('.q-field', __demo.dialog()).find((el) => text(el.querySelector('.q-field__label')) === label),
button: (label) => all('.q-btn', __demo.dialog()).find((el) => text(el) === label),
checkbox: (label) =>
all('.q-checkbox', __demo.dialog()).find(
(el) => text(el) === label || text(el).startsWith(label + ' (')
),
thumb: (label) => __demo.checkbox(label)?.parentElement.querySelector('.q-slider__thumb'),
// Point of the participation slider track matching the rate
ratePoint(label, rate) {
const slider = __demo.checkbox(label)?.parentElement.querySelector('[role=slider]')
const track = slider?.querySelector('.q-slider__track')?.getBoundingClientRect()
if (!track) return null
const { ariaValueMin: min, ariaValueMax: max } = slider
return {
x: track.left + (track.width * (rate - min)) / (max - min),
y: track.top + track.height / 2
}
},
rate: (label) =>
Number(__demo.checkbox(label)?.parentElement.querySelector('[role=slider]')?.ariaValueNow),
option: (label) => all('.q-menu .q-item').find((el) => text(el) === label),
day: (day) =>
all('.q-menu .q-date__calendar-item--in .q-btn').find((el) => text(el) === String(day)),
row: (title, sender, receiver) => {
const row = all('tbody tr', __demo.section(title)).find(
(el) => text(el.children[1]) === sender && text(el.children[2]) === receiver
)
return row?.children[3]
},
// Scrolls the element to the top of the page, right below the header
focus(el) {
const top = el.getBoundingClientRect().top
if (Math.abs(top - 64) < 4) return false
el.style.scrollMarginTop = '64px'
el.scrollIntoView({ behavior: 'smooth', block: 'start' })
return true
},
// Remembers the element to act on and scrolls it into the view when it's
// hidden behind the header
target(el) {
target = el
if (!el) return false
const rect = el.getBoundingClientRect()
const top = el.closest('.backdrop, .q-menu') ? 0 : 70
if (rect.top < top || rect.bottom > innerHeight - 10) {
el.scrollIntoView({ behavior: 'smooth', block: 'center' })
return 'scrolled'
}
return 'visible'
},
rect() {
const rect = target.getBoundingClientRect()
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }
}
}
})()`
// Scenario actions
let cdp
let recorder
let narration
const subtitles = new Subtitles()
const sounds = new Sounds()
const mouse = { x: WIDTH / 2, y: PAGE_HEIGHT / 2 }
async function evaluate(expression) {
const { result, exceptionDetails } = await cdp.send('Runtime.evaluate', {
expression,
returnByValue: true,
awaitPromise: true
})
if (exceptionDetails) throw new Error(`${expression}: ${exceptionDetails.exception?.description}`)
return result.value
}
async function waitFor(expression, timeout = 10000) {
const deadline = Date.now() + timeout
while (!(await evaluate(expression))) {
if (Date.now() > deadline) throw new Error(`Timed out waiting for ${expression}`)
await sleep(100)
}
}
// Locates the element by the expression evaluated in the page and scrolls to it if needed
async function locate(expression) {
await waitFor(expression)
const state = await evaluate(`__demo.target(${expression})`)
if (state === 'scrolled') await wait(900)
return evaluate('__demo.rect()')
}
// Moves the mouse smoothly, holding its button down when dragging
async function move(x, y, { dragging = false, slowdown = 1 } = {}) {
const distance = Math.hypot(x - mouse.x, y - mouse.y)
const duration = Math.min(Math.max(distance * 1.1, 250), 900) * PACE * slowdown
const steps = Math.max(Math.round(duration / 16), 1)
const from = { ...mouse }
for (let step = 1; step <= steps; step++) {
const progress = step / steps
const eased = progress < 0.5 ? 2 * progress ** 2 : 1 - (-2 * progress + 2) ** 2 / 2
mouse.x = from.x + (x - from.x) * eased
mouse.y = from.y + (y - from.y) * eased
await cdp.send('Input.dispatchMouseEvent', {
type: 'mouseMoved',
x: mouse.x,
y: mouse.y,
...(dragging && { button: 'left', buttons: 1 })
})
await sleep(16)
}
}
async function hover(expression) {
const { x, y } = await locate(expression)
await move(x, y)
}
async function click(expression) {
await hover(expression)
await wait(150)
const event = { x: mouse.x, y: mouse.y, button: 'left', clickCount: 1 }
sounds.add('press')
await cdp.send('Input.dispatchMouseEvent', { type: 'mousePressed', ...event })
await sleep(80)
sounds.add('release')
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseReleased', ...event })
await wait(350)
}
async function type(value) {
for (const char of value) {
sounds.add('key')
await cdp.send('Input.insertText', { text: char })
await wait(TYPING_DELAY[0] + Math.random() * (TYPING_DELAY[1] - TYPING_DELAY[0]))
}
await wait(300)
}
const KEYS = {
ArrowLeft: { code: 'ArrowLeft', windowsVirtualKeyCode: 37 },
ArrowRight: { code: 'ArrowRight', windowsVirtualKeyCode: 39 },
Escape: { code: 'Escape', windowsVirtualKeyCode: 27 }
}
async function press(key, times = 1) {
for (let i = 0; i < times; i++) {
const event = { key, ...KEYS[key] }
sounds.add('key')
await cdp.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', ...event })
await cdp.send('Input.dispatchKeyEvent', { type: 'keyUp', ...event })
await wait(140)
}
}
const str = (value) => JSON.stringify(value)
// Brings the section to the top, so its new items are seen well
async function focusSection(title) {
const expression = `__demo.section(${str(title)})`
await waitFor(expression)
if (await evaluate(`__demo.focus(${expression})`)) await wait(1000)
}
async function openDialog(expression) {
await click(expression)
await waitFor('__demo.dialog()')
// Lets the dialog slide in
await wait(500)
}
async function fill(label, value) {
await click(`__demo.field(${str(label)})`)
await type(value)
}
async function select(label, option) {
await click(`__demo.field(${str(label)})`)
await click(`__demo.option(${str(option)})`)
await waitFor('!__demo.menu()')
}
// Drags the participation slider to the rate, the keyboard corrects it in case of rounding
async function setRate(label, rate) {
await hover(`__demo.thumb(${str(label)})`)
await wait(200)
const event = { button: 'left', clickCount: 1 }
sounds.add('press')
await cdp.send('Input.dispatchMouseEvent', { type: 'mousePressed', ...mouse, ...event })
await wait(150)
const point = await evaluate(`__demo.ratePoint(${str(label)}, ${rate})`)
await move(point.x, mouse.y, { dragging: true, slowdown: 2.5 })
await wait(250)
sounds.add('release')
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseReleased', ...mouse, ...event })
for (let attempt = 0; ; attempt++) {
const current = await evaluate(`__demo.rate(${str(label)})`)
if (current === rate) break
if (Number.isNaN(current) || attempt > 50) throw new Error(`Unable to set ${label} rate`)
if (attempt === 0) console.warn(`The slider is dragged to ${current} instead of ${rate}`)
await press(current < rate ? 'ArrowRight' : 'ArrowLeft')
}
await wait(600)
}
async function submit(label = t('save')) {
await click(`__demo.button(${str(label)})`)
await waitFor('!__demo.dialog()')
await wait(500)
}
// Selects the section row having a cell with the value and removes it after the confirmation
async function removeItem(title, value) {
await click(`__demo.select(${str(title)}, ${str(value)})`)
await openDialog(`__demo.remove(${str(title)})`)
await submit(t('confirm'))
}
async function addExpense({ description, category, payer, amount, dated }) {
await openDialog(`__demo.headerButton(${str(t('expense.plural'))})`)
if (dated) {
await click(`__demo.field(${str(t('date'))})`)
await click(`__demo.day(${new Date().getDate()})`)
await waitFor('!__demo.menu()')
}
await fill(t('expense.description'), description)
if (category) await select(t('category'), category)
await select(t('expense.payer'), payer.name)
await fill(t('amount'), amount)
await submit()
}
let captionEnd = 0
// Waits until the current caption is spoken or shown long enough to be read
async function captionShown() {
const delay = captionEnd - recorder.time()
if (delay > 0) await sleep(delay * 1000)
}
// Shows the caption once the previous one is done, or ends the last one
async function caption(key) {
await captionShown()
const time = recorder.time()
if (!key) {
subtitles.end(time)
await wait(400)
return
}
const text = captionText(data.captions[key])
subtitles.add(time, text)
captionEnd = narration
? time + narration.add(key, time) + 0.6
: time + (Math.max(2500, text.length * 55) * PACE) / 1000
await wait(1200)
}
// Shows the title card with the subtitle, hides it when there's none
async function card(subtitle) {
await evaluate(`__demo.card(${str(subtitle || '')})`)
await wait(900)
}
// Scenario
async function scenario() {
const { anna, ben, clara, tim, oscar } = data.participants
const { rent, groceries, wine, taxi } = data.expenses
// The recording starts with the title card shown
await caption('greeting')
await caption('about')
await caption('demo')
await captionShown()
await card(null)
await caption('title')
await openDialog('__demo.header()')