-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathautosplitter.lua
206 lines (175 loc) · 5.41 KB
/
autosplitter.lua
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
obs = obslua
local enabled = false
local interval = 60
function script_description()
return "AutoSplitter\nSplit recordings at a specific interval"
end
function getInterval(settings)
local seconds = obs.obs_data_get_int(settings, "interval_s")
local minutes = obs.obs_data_get_int(settings, "interval_m")
local hours = obs.obs_data_get_int(settings, "interval_h")
return hours, minutes, seconds
end
function setInterval(settings, hours, minutes, seconds)
obs.obs_data_set_int(settings, "interval_s", seconds)
obs.obs_data_set_int(settings, "interval_m", minutes)
obs.obs_data_set_int(settings, "interval_h", hours)
end
function toSeconds(hours, minutes, seconds)
return seconds + (minutes + hours * 60) * 60
end
function fromSeconds(interval)
local seconds = interval % 60
local minutes = math.floor(interval / 60) % 60
local hours = math.floor(math.floor(interval / 60 / 60))
return hours, minutes, seconds
end
local cancelCheck = function()
end
local cancelRestart = function()
end
function script_update(settings)
enabled = obs.obs_data_get_bool(settings, "enabled")
interval = toSeconds(getInterval(settings))
if interval <= 0 then
interval = 1
end
cancelCheck()
cancelRestart()
if enabled then
cancelCheck = onRecordingChanged(
function(recording)
if recording then
cancelRestart = timer(recording_restart, interval * 1000)
else
cancelRestart()
end
end
)
end
end
function script_defaults(settings)
obs.obs_data_set_default_bool(settings, "enabled", enabled)
local hours, minutes, seconds = fromSeconds(interval)
obs.obs_data_set_default_int(settings, "interval_s", seconds)
obs.obs_data_set_default_int(settings, "interval_m", minutes)
obs.obs_data_set_default_int(settings, "interval_h", hours)
end
function script_properties()
local props = obs.obs_properties_create()
obs.obs_properties_add_bool(props, "enabled", "Enabled")
local prop_interval_s = obs.obs_properties_add_int(props, "interval_s", "Seconds", -1, 60, 1)
local prop_interval_m = obs.obs_properties_add_int(props, "interval_m", "Minutes", -1, 60, 1)
local prop_interval_h = obs.obs_properties_add_int(props, "interval_h", "Hours", 0, 240, 1)
function validate(props, prop, settings)
local hours, minutes, seconds = getInterval(settings)
local interval = toSeconds(hours, minutes, seconds)
if interval <= 0 then
interval = 1
end
local newHours, newMinutes, newSecods = fromSeconds(interval)
if hours == newHours and
minutes == newMinutes and
seconds == newSecods then
return false
else
setInterval(settings, newHours, newMinutes, newSecods)
return true
end
end
obs.obs_property_set_modified_callback(prop_interval_s, validate)
obs.obs_property_set_modified_callback(prop_interval_m, validate)
obs.obs_property_set_modified_callback(prop_interval_h, validate)
return props
end
function timer(func, millis)
local cancelled = false
obs.timer_add(
function()
if not cancelled then
func()
else
obs.remove_current_callback()
end
end,
millis
)
return function()
cancelled = true
end
end
function delay(func, millis)
return timer(
function()
obs.remove_current_callback()
func()
end,
millis
)
end
function delayUntil(func, condition, millis)
local cancel
cancel = timer(
function()
if condition() then
cancel()
func()
end
end,
millis
)
return cancel
end
function timerWhile(func, condition, millis)
local cancel
cancel = timer(
function()
if condition() then
func()
else
cancel()
end
end,
millis
)
return cancel
end
function recording_stopped(func)
delayUntil(
func,
function()
return not obs.obs_frontend_recording_active()
end,
10
)
end
function recording_restart()
if obs.obs_frontend_recording_active() and not obs.obs_frontend_recording_paused() then
obs.obs_frontend_recording_stop()
recording_stopped(
function()
timerWhile(
function()
obs.obs_frontend_recording_start()
end,
function()
return not obs.obs_frontend_recording_active()
end,
10
)
end
)
end
end
function onRecordingChanged(func)
local recording = not obs.obs_frontend_recording_active()
return timer(
function()
if recording ~= obs.obs_frontend_recording_active() then
recording = not recording
func(recording)
end
end,
200
)
end