-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshitrate.py
349 lines (228 loc) · 12.1 KB
/
shitrate.py
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
import platform
import ctypes as ct
import ctypes.util
from types import SimpleNamespace
import obspython as obs # studio
###########################################################################
if platform.system() == "Linux":
libobs = ct.CDLL(ct.util.find_library("obs"))
else:
libobs = ct.CDLL("obs")
def wrap(lib, funcname, restype, argtypes):
"""Wraps a C function from the given lib into python
"""
func = getattr(lib, funcname)
func.restype = restype
func.argtypes = argtypes
return func
############################################################################
class ctSource(ct.Structure):
pass
class ctVolmeter(ct.Structure):
pass
volmeter_callback_t = ct.CFUNCTYPE(None, ct.c_void_p, ct.POINTER(ct.c_float),
ct.POINTER(ct.c_float), ct.POINTER(ct.c_float))
obs.obs_volmeter_create = wrap(libobs,
"obs_volmeter_create",
restype=ct.POINTER(ctVolmeter),
argtypes=[ct.c_int])
obs.obs_volmeter_destroy = wrap(libobs,
"obs_volmeter_destroy",
restype=None,
argtypes=[ct.POINTER(ctVolmeter)])
obs.obs_volmeter_add_callback = wrap(libobs,
"obs_volmeter_add_callback",
restype=None,
argtypes=[ct.POINTER(ctVolmeter), volmeter_callback_t, ct.c_void_p])
obs.obs_volmeter_remove_callback = wrap(libobs,
"obs_volmeter_remove_callback",
restype=None,
argtypes=[ct.POINTER(ctVolmeter), volmeter_callback_t, ct.c_void_p])
_obs_volmeter_attach_source = wrap(libobs,
"obs_volmeter_attach_source",
restype=ct.c_bool,
argtypes=[ct.POINTER(ctVolmeter), ct.POINTER(ctSource)])
obs.obs_volmeter_attach_source = lambda volmeter, source : _obs_volmeter_attach_source(volmeter,
ct.cast(int(source), ct.POINTER(ctSource)))
obs.obs_volmeter_detach_source = wrap(libobs,
"obs_volmeter_detach_source",
restype=None,
argtypes=[ct.POINTER(ctVolmeter)])
obs.OBS_FADER_LOG = 2
##########################################################################################
class Source:
def __init__(self, name):
self.name = name
self.volmeter = obs.obs_volmeter_create(obs.OBS_FADER_LOG)
obs.obs_volmeter_add_callback(self.volmeter, volmeter_callback, None)
G.sources.append(self)
def attach_source(self):
obsSource = obs.obs_get_source_by_name(self.name)
obs.obs_volmeter_detach_source(self.volmeter)
if not obs.obs_volmeter_attach_source(self.volmeter, obsSource):
raise
obs.obs_source_release(obsSource)
#called by obs after processing volume data
@volmeter_callback_t
def volmeter_callback(data, mag, peak, input):
try: # PENWY : try ... except so that if there is an error happening, it doesn't spam that error in log every volume tick but raises it and stops instead
if G.outputActive:
G.noises.append(float(peak[0]))
except:
for source in G.sources:
obs.obs_volmeter_destroy(source.volmeter)
raise
def update_bitrate():
try:
# getting loudest noise from all sources
loudestNoise = max(G.noises)
G.noises = [-999]
# this is a fucked up piecewise function
# checkitout: desmos.com/calculator/umastzlhoj
bitrate = (G.maxBitrate / pow( G.maxVolume, G.dampening )) * pow(abs( loudestNoise ), G.dampening)
d = (G.maxVolume - 40)
if abs( loudestNoise ) < d:
bitrate -= pow(abs( loudestNoise + d ), 4 * (1.1 - G.dampening))
# keeping bitrate within bounds
bitrate = min(G.maxBitrate, max( round(bitrate, 0), G.minBitrate ))
# print("\nvolume:" + str(loudestNoise))
# print("bitrate:" + str(bitrate))
if G.outputType & 1: # PENWY : if recording is active
output = obs.obs_frontend_get_recording_output()
encoder = obs.obs_output_get_video_encoder(output)
settings = obs.obs_data_create()
obs.obs_data_set_int(settings, "bitrate", int(bitrate))
obs.obs_encoder_update(encoder, settings)
obs.obs_data_release(settings)
obs.obs_output_release(output)
if G.outputType & 2: # PENWY : if streaming is active
output = obs.obs_frontend_get_streaming_output()
encoder = obs.obs_output_get_video_encoder(output)
settings = obs.obs_data_create()
obs.obs_data_set_int(settings, "bitrate", int(bitrate))
obs.obs_encoder_update(encoder, settings)
obs.obs_data_release(settings)
obs.obs_output_release(output)
except:
obs.remove_current_callback()
raise
def on_event(event):
if event == obs.OBS_FRONTEND_EVENT_RECORDING_STARTED:
G.outputType |= 1
if event == obs.OBS_FRONTEND_EVENT_STREAMING_STARTED:
G.outputType |= 2
if event == obs.OBS_FRONTEND_EVENT_RECORDING_STOPPING:
G.outputType &= ~1
if event == obs.OBS_FRONTEND_EVENT_STREAMING_STOPPING:
G.outputType &= ~2
if not G.outputActive and G.outputType:
# print("\nRecording start\n")
for source in G.sources:
source.attach_source()
obs.timer_add(G.callback, G.bitrate_tick)
G.outputActive = True
if G.outputActive and not G.outputType:
# print("\nRecording end\n")
obs.timer_remove(G.callback)
G.outputActive = False
# declaring global variables
G = SimpleNamespace()
G.sources = []
G.bitrate_tick = 10 # PENWY : interval between each bitrate change
G.callback = update_bitrate
G.maxVolume = 60 # in dB
G.maxBitrate = 5000 # in kbps
G.minBitrate = 10 # in kbps
G.dampening = 1
G.lock = False
G.noises = [-999] # volume (default at 999)
G.outputType = 0 # ( 0 = None, 1 = recording, 2 = streaming, 3 = both)
G.outputActive = False
G.settings = None # to set OBS saved settings
def script_load(settings):
obs.obs_frontend_add_event_callback(on_event)
savedSourceName = obs.obs_data_get_string(settings, "source_name0")
if savedSourceName is not None:
Source(obs.obs_data_get_string(settings, "source_name0"))
else:
Source("Desktop Audio")
def script_unload():
obs.timer_remove(G.callback)
obs.obs_frontend_remove_event_callback(on_event)
for source in G.sources:
if source.volmeter is not None: # PENWY : checking if a volmeter has actually been created before destroying it
obs.obs_volmeter_remove_callback(source.volmeter, volmeter_callback, None)
obs.obs_volmeter_destroy(source.volmeter)
##########################################################################################
# Description displayed in the Scripts dialog window
def script_description():
return """<center><h2>Accurately Shitty Streams!!</h2></center>
<p>Decreases the bitrate of the stream as audio levels increase!!</p>
<p>By satalight (:</p>
<p>Check <a href="https://github.com/satasatalight/obs-shitrate">the repo</a> for more information.</p>"""
# Called to display the properties GUI
def script_properties():
props = obs.obs_properties_create()
sourceGroup = obs.obs_properties_create()
obs.obs_properties_add_group (props, "source_group", "Audio Sources:", obs.OBS_GROUP_NORMAL, sourceGroup)
obs.obs_properties_add_button (sourceGroup, "add_button", "Add Source", add_source_element)
obs.obs_properties_add_button (sourceGroup, "remove_button", "Remove Source", remove_source_element)
sourceList = obs.obs_properties_add_list(sourceGroup, "source_name0", "Source #1:", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
obs.obs_properties_add_int (props, "max_volume", "Maximum Volume (in dB):", 1, 99, 10)
obs.obs_properties_add_int (props, "max_bitrate", "Maximum Bitrate (in kbps):", 100, 99999, 100)
obs.obs_properties_add_float_slider (props, "damp_scale", "Dampening:", 0.2, 1, 0.01)
populate_list_property_with_source_names(sourceList)
return props
def add_source_element(props, prop, *args, **kwargs):
source = Source("Desktop Audio")
index = len(G.sources)
sourceGroupProperties = obs.obs_property_group_content( obs.obs_properties_get(props, "source_group") )
sourceList = obs.obs_properties_add_list(sourceGroupProperties, "source_name%d" % (index - 1), "Source #%d:" % index,
obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
savedSourceName = obs.obs_data_get_string(G.settings, "source_name%d" % (index - 1))
if savedSourceName is not None:
source.name = savedSourceName
if G.outputActive:
source.attach_source()
populate_list_property_with_source_names(sourceList)
# print(G.sources)
return True # return True to update props screen
def remove_source_element(props, prop, *args, **kwargs):
index = len(G.sources) - 1
if index > 0:
sourceGroupProperties = obs.obs_property_group_content( obs.obs_properties_get(props, "source_group") )
source = G.sources[index]
obs.obs_properties_remove_by_name (sourceGroupProperties, "source_name%d" % index)
obs.obs_volmeter_destroy (source.volmeter)
G.sources.remove (source)
# print(G.sources)
return True
# Adds list of media sources (taken from https://github.com/obsproject/obs-studio/wiki/Scripting-Tutorial-Source-Shake)
def populate_list_property_with_source_names(list_property):
obsSources = obs.obs_enum_sources()
obs.obs_property_list_clear(list_property)
for obsSource in obsSources:
name = obs.obs_source_get_name(obsSource)
obs.obs_property_list_add_string(list_property, name, name)
obs.source_list_release(obsSources)
# Called to set default values of data settings
def script_defaults(settings):
obs.obs_data_set_default_int (settings, "max_volume", 60)
obs.obs_data_set_default_int (settings, "max_bitrate", 5000)
obs.obs_data_set_default_double (settings, "damp_scale", 0.7)
obs.obs_data_set_default_string (settings, "source_name0", "Desktop Audio")
# Called after change of settings including once after script load
def script_update(settings):
# print(obs.obs_data_get_json(settings))
G.settings = settings
G.maxVolume = obs.obs_data_get_int(settings, "max_volume")
G.maxBitrate = obs.obs_data_get_int(settings, "max_bitrate")
G.dampening = 1.2 - obs.obs_data_get_double(settings, "damp_scale")
for i in range(len(G.sources)):
sourceName = obs.obs_data_get_string(settings, "source_name" + str(i))
# print("source_name%d: %s" % (i, sourceName))
if G.sources[i].name != sourceName and sourceName is not None and sourceName is not "":
# print("Setting Source " + str(i) + " from " + G.sources[i].name + " to " + sourceName)
G.sources[i].name = sourceName
if G.outputActive:
G.sources[i].attach_source()