-
Notifications
You must be signed in to change notification settings - Fork 1
/
obsConnector.js
91 lines (82 loc) · 2.63 KB
/
obsConnector.js
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
const OBSWebSocket = require('obs-websocket-js');
const { uploadVideo } = require('./thetaVideoManager');
const log = require('./log');
let obs;
let obsConnected = false;
let recordingFolder;
let lastVideoName = '';
let stateChangeListener;
let _onVideoSaved = _ => _;
const init = async (_stateChangeListener) => {
log.info(`[OC] OBS Connector - initialization`);
await connect(false);
stateChangeListener = _stateChangeListener;
};
const connect = async (reset) => {
if (obsConnected && !reset) return;
if (obsConnected) {
obs.disconnect();
}
obs = new OBSWebSocket();
try {
log.info('[OC] stablishing connection to OBS WebSocket server');
await obs.connect({ address: process.env.OBS_WEBSOCKET_ADDRESS, password: process.env.OBS_WEBSOCKET_PASSWORD });
const res = await obs.send('GetRecordingFolder');
recordingFolder = res['rec-folder'].replace(/\\/g, '/');
log.info('[OC] recoding folder:', recordingFolder);
obsConnected = true;
stateChangeListener && stateChangeListener();
} catch(error) {
console.error(error);
log.error('[OC] not connected');
return;
}
log.info('[OC] connected');
obs.on('RecordingStopped', async ({ recordingFilename }) => {
log.info(`[OC] recordingFilename: ${ recordingFilename }`);
recordingFilename = recordingFilename.replace(recordingFolder, process.env.OBS_RECORDING_PATH);
log.info('[OC] updated to', recordingFilename);
const video = await uploadVideo(recordingFilename, lastVideoName);
console.log('video saved => ', video);
_onVideoSaved(video.id);
});
obs.on('ConnectionClosed', (data) => {
log.warn('[OC] connection closed');
obsConnected = false;
stateChangeListener();
});
};
const startRecording = async (filename) => {
if (!obsConnected) return;
try {
await obs.send('SetFilenameFormatting', {'filename-formatting': filename});
lastVideoName = filename;
await obs.send('StartRecording');
} catch(error) {
console.error(error);
}
};
const stopRecording = async () => {
if (!obsConnected) return;
await obs.send('StopRecording');
await obs.send('SetFilenameFormatting', {'filename-formatting': '%CCYY-%MM-%DD %hh-%mm-%ss'});
};
const setScene = async (sceneName) => {
if (!obsConnected) return;
await obs.send('SetCurrentScene', {'scene-name': sceneName});
}
const test = async () => {
await init();
await startRecording('testing');
await new Promise((resolve) => setTimeout(resolve, 5*60*1000));
await stopRecording();
};
module.exports = {
init,
connect,
startRecording,
stopRecording,
setScene,
onVideoSaved: (cb) => _onVideoSaved = cb,
isConnected: () => obsConnected
};