-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsctl.js
126 lines (109 loc) · 3.02 KB
/
obsctl.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
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
const maxApi = require('max-api');
const { default: OBSWebSocket } = require('obs-websocket-js');
const obs = new OBSWebSocket();
maxApi.post("hello n4m-obs");
let connected = false;
const port = 4455;
maxApi.addHandler('connect', () => {
// To use LAN, or if password enabled, change to something like below, replacing with your server IP and password
//obs.connect(`ws://192.168.1.17:4455`, 'your_obs_websocket_password').then((info) => {
obs.connect(`ws://localhost:${port}`, 'password').then((info) => {
maxApi.post('Connected and identified', info);
}, () => {
maxApi.post('Error Connecting, check your port and password (if enabled)');
});
});
maxApi.addHandler('set_scene', (sceneName) => {
if(connected) {
obs.call('SetCurrentProgramScene', {
'sceneName': sceneName
});
}
else {
maxApi.post("not connected to OBS");
}
});
maxApi.addHandler('set_recording', (state) => {
if(connected) {
if(state === "start") {
obs.call('StartRecord')
.catch(err => {
maxApi.post('set_recording error');
maxApi.post(err);
});
}
else if(state === "stop") {
obs.call('StopRecord')
.catch(err => {
maxApi.post('set_recording error');
maxApi.post(err);
});
}
else if(state === "pause") {
obs.call('PauseRecord')
.catch(err => {
maxApi.post('set_recording error');
maxApi.post(err);
});
}
else if(state === "resume") {
obs.call('ResumeRecord')
.catch(err => {
maxApi.post('set_recording error');
maxApi.post(err);
});
}
}
else {
maxApi.post("not connected to OBS");
}
});
function outputSceneList(scenes)
{
maxApi.outlet('scenelist');
scenes.forEach(scene => {
maxApi.outlet("scene", scene.sceneName);
});
}
// Declare some events to listen for.
obs.on('ConnectionOpened', () => {
maxApi.post('Connection Opened');
});
obs.on('Identified', () => {
maxApi.post('Identified, good to go!')
obs.call('GetSceneList').then((data) => {
connected = true;
maxApi.post(`Current Scene is ${data.currentScene}`);
maxApi.post(`${data.scenes.length} Available Scenes`);
maxApi.post(data.scenes);
outputSceneList(data.scenes);
});
});
obs.on('RecordStateChanged', data => {
if(data.outputState == 'OBS_WEBSOCKET_OUTPUT_STARTED') {
maxApi.post(`recording started to ${data.outputPath}`)
maxApi.outlet('start', data.outputPath);
}
else if(data.outputState == 'OBS_WEBSOCKET_OUTPUT_STOPPED') {
maxApi.post(`recording stopped to ${data.outputPath}`)
maxApi.outlet('stop', data.outputPath);
}
else if(data.outputState == 'OBS_WEBSOCKET_OUTPUT_PAUSED') {
maxApi.post('recording paused');
}
else if(data.outputState == 'OBS_WEBSOCKET_OUTPUT_RESUMED') {
maxApi.post('recording resumed');
}
});
obs.on('CurrentProgramSceneChanged', data => {
maxApi.post(`New Active Scene ${data.sceneName}`);
});
obs.on('SceneListChanged', data => {
maxApi.post('scene list changed');
maxApi.post(data.scenes);
outputSceneList(data.scenes);
});
// You must add this handler to avoid uncaught exceptions.
obs.on('error', err => {
maxApi.post('socket error:', err);
});