-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicDelay.py
200 lines (141 loc) · 6.86 KB
/
DynamicDelay.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
import sys
import obswebsocket, obswebsocket.requests
import time as t
from multiprocessing import Process, Pipe, Queue
import PySimpleGUI as sg
from config import RECORDING_OBS_IP, STREAMING_OBS_IP, RECORDING_OBS_PORT, STREAMING_OBS_PORT, DELAY_SCENE, REMOVE_DELAY_SCENE_TRANSITION, ADD_DELAY_SCENE_TRANSITION, AUDIO_SOURCE_1, AUDIO_SOURCE_2, AUDIO_SOURCE_3
def addDelay(record, stream, transition = 'Transition', delayScene = 'Delay', delay = 30):
"""Add delay function"""
print("Adding delay")
#Start recording with recording OBS
record.call(obswebsocket.requests.StartRecording())
#Mute Streaming OBS !!!Change names of sound sources
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_1, 0.0))
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_2, 0.0))
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_3, 0.0))
stream.call(obswebsocket.requests.SetCurrentScene(transition))
#Pause program delayed time
#(Tiene que haber un medio de video configurado de la misma para que no se vea raro)
t.sleep(delay)
#Reinicia el video ya que se reproduce de fondo
stream.call(obswebsocket.requests.SetCurrentScene(delayScene))
print("Delay added")
def removeDelay(record, stream, transition = 'removeDelayTransition'):
"""Remove delay function"""
print("Removind delay")
stream.call(obswebsocket.requests.SetCurrentScene(transition))
#Stop recording with recording OBS
record.call(obswebsocket.requests.StopRecording())
t.sleep(0.3)
#Unmute audio from Streaming OBS !!!Change names of sound sources
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_1, 1.0))
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_2, 1.0))
stream.call(obswebsocket.requests.SetVolume(AUDIO_SOURCE_3, 1.0))
escenaActual = record.call(obswebsocket.requests.GetCurrentScene()).getName()
stream.call(obswebsocket.requests.SetCurrentScene(escenaActual))
print("Delay removed")
def sceneSyncronizer(transition = 'Transition', delayScene = 'Delay', removeDelayTransition = 'removeDelayTransition'):
"""syncronize Streaming OBS with Recording OBS. You only have to change scenes in Recording OBS so they are changed in Streaming OBS unless if you are with delay"""
banned = [transition, delayScene, removeDelayTransition]
#OBS websocket recording
record = obswebsocket.obsws(RECORDING_OBS_IP, RECORDING_OBS_PORT, "secret")
record.connect()
#OBS websocket stream
stream = obswebsocket.obsws(STREAMING_OBS_IP, STREAMING_OBS_PORT, "secret")
stream.connect()
transition = ADD_DELAY_SCENE_TRANSITION
delayScene = DELAY_SCENE
removeDelayTransition = REMOVE_DELAY_SCENE_TRANSITION
banned = [transition, delayScene, removeDelayTransition]
while True:
actualRecordingScene = record.call(obswebsocket.requests.GetCurrentScene()).getName()
actualStreamScene = stream.call(obswebsocket.requests.GetCurrentScene()).getName()
if actualStreamScene not in banned and actualStreamScene != actualRecordingScene:
stream.call(obswebsocket.requests.SetCurrentScene(actualRecordingScene))
t.sleep(0.1)
def eventHandler(p_output, queue):
"""Used to comunicate with the different app threads"""
#OBS websocket recording
record = obswebsocket.obsws(RECORDING_OBS_IP, RECORDING_OBS_PORT, "secret")
record.connect()
#OBS websocket stream
stream = obswebsocket.obsws(STREAMING_OBS_IP, STREAMING_OBS_PORT, "secret")
stream.connect()
withDelay = False
while True:
event = p_output.recv()
if event == 'Exit' or event is None:
if withDelay:
removeDelay(record, stream, REMOVE_DELAY_SCENE_TRANSITION)
break
if event == 'addDelay':
values = p_output.recv()
try:
if values == '':
addDelay(record, stream, ADD_DELAY_SCENE_TRANSITION, DELAY_SCENE)
else:
delay = int(values)
addDelay(record, stream, ADD_DELAY_SCENE_TRANSITION, DELAY_SCENE, delay)
except:
sg.Popup('The delay must be an integer')
withDelay = True
if event == 'removeDelay':
removeDelay(record, stream, REMOVE_DELAY_SCENE_TRANSITION)
withDelay = False
if event == 'cancel':
break
record.disconnect()
stream.disconnect()
sys.exit()
if __name__ == '__main__':
#if I am without delay, the method of synchronizing scenes would be active
#Hacer layout de config que guarde los datos
#Crear archivo config
defaultDelay = 30
#Interface
sg.ChangeLookAndFeel('LightGreen')
layout = [[sg.Text('Dynamic Delay', size=(40, 1), justification='center')],
[sg.Text(text='Delay:'), sg.InputText()],
[sg.Button('Add delay', key='addDelay'), sg.Button('Remove delay', key = 'removeDelay', disabled=True)],
[sg.Text(text='No delay', key='message', size=(40, 1))]
]
window = sg.Window('Dynamic Delay', location=(800, 400))
window.Layout(layout).Finalize()
#Implementar con pipe para el correcto funcionamiento con 2 subprocesos
p_output, p_input = Pipe()
queue = Queue()
pEventHandel = Process(target=eventHandler, args=((p_output),(queue)))
pEventHandel.daemon = True
pEventHandel.start()
pSync = Process(target=sceneSyncronizer)
pSync.daemon = True
pSync.start()
while True:
event, values = window.Read()
if not queue.empty():
msg = queue.get()
if msg == 'endingDelay':
window.FindElement('message').Update('')
if event == 'Exit' or event is None:
p_input.send(event)
pEventHandel.join()
sys.exit()
break
if event == 'addDelay':
p_input.send(event)
p_input.send(values[0])
window.FindElement('removeDelay').Update(disabled=False)
window.FindElement('addDelay').Update(disabled=True)
if values[0] == '':
delayAdded = str(defaultDelay)
else:
delayAdded = values[0]
window.FindElement('message').Update('You have ' + delayAdded + ' seconds delay')
if event == 'removeDelay':
p_input.send(event)
window.FindElement('removeDelay').Update(disabled=True)
window.FindElement('addDelay').Update(disabled=False)
window.FindElement('message').Update('No tienes delay')
if event == 'cancel':
p_input.send(event)
sys.exit()