-
Notifications
You must be signed in to change notification settings - Fork 13
/
recorder.ts
94 lines (77 loc) · 1.92 KB
/
recorder.ts
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
import { Recorder as ScreenRecorder, RecordingOptions as Recop, GIFCreator, GIFOptions } from '@arcsine/screen-recorder';
import { Config } from './config';
import { RecordingOptions } from './types';
import { ChildProcess } from 'child_process';
export class Recorder {
private proc: {
proc: ChildProcess,
stop: (now?: boolean) => void,
finish: Promise<Recop>
};
get active() {
return !!this.proc;
}
get finish() {
return this.proc.finish;
}
dispose() {
this.stop();
}
async postProcess(opts: RecordingOptions) {
try {
await this.proc.finish;
if (opts.animatedGif) {
const result = await GIFCreator.generate({
...opts,
scale: opts.gifScale || 1
});
if (result) {
const animated = await result.finish;
if (!opts.audio) { // Only default to GIF is not audio
opts.file = animated;
}
}
}
return opts;
} finally {
this.proc.stop(true);
delete (this as any)['proc'];
}
}
async run(override: Partial<RecordingOptions> = {}) {
const binary = (await Config.getFFmpegBinary())!;
const defs = Config.getRecordingDefaults();
const opts = {
...defs,
file: await Config.getFilename(),
...override,
ffmpeg: {
binary,
flags: defs.flags
}
};
if (this.proc) {
throw new Error('Recording already in progress');
}
this.proc = await ScreenRecorder.recordActiveWindow(opts);
return {
output: async () => {
const newOpts: RecordingOptions = (await this.proc.finish) as any;
return this.postProcess(newOpts);
}
};
}
get running() {
return !!this.proc;
}
stop(force = false) {
if (!this.running) {
throw new Error('info:No recording running');
}
try {
this.proc.stop(force);
} catch {
this.proc.stop(true);
}
}
}