-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
371 lines (316 loc) · 13.5 KB
/
app.py
File metadata and controls
371 lines (316 loc) · 13.5 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import time
import json
import os
import csv
import io
import urllib.request
import urllib.parse
from flask import Flask, render_template, request, jsonify, make_response
from flask_socketio import SocketIO
import obsws_python as obs
import threading
app = Flask(__name__)
socketio = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*")
triggered_webhooks = set()
OBS_CONFIG_FILE = "obs_config.json"
obs_client = None
obs_connected = False
obs_error = ""
def load_obs_config():
if os.path.exists(OBS_CONFIG_FILE):
with open(OBS_CONFIG_FILE, 'r') as f:
return json.load(f)
return {"host": "localhost", "port": 4455, "password": "", "webhook_url": ""}
def connect_obs():
global obs_client, obs_connected, obs_error
config = load_obs_config()
try:
obs_client = obs.ReqClient(host=config['host'], port=config['port'], password=config['password'])
obs_connected = True
obs_error = ""
print(f"✅ Connected to OBS at {config['host']}:{config['port']}!")
except Exception as e:
obs_client = None
obs_connected = False
obs_error = str(e)
print(f"❌ OBS Connection Error: {e}")
connect_obs()
is_playing = False
start_time = 0
active_rundown = []
show_mode = "playback"
force_timeline_update = False
time_offset = 0.0
is_paused = False
pause_timestamp = 0.0
COMPILATIONS_FILE = 'compilations.json'
def load_compilations():
if os.path.exists(COMPILATIONS_FILE):
with open(COMPILATIONS_FILE, 'r') as f:
return json.load(f)
return {}
# --- FUNZIONE IOT PER LUCI TALLY FISICHE ---
def trigger_physical_tally(scene_name):
config = load_obs_config()
webhook_url = config.get("webhook_url", "").strip()
if webhook_url:
try:
# Invia una chiamata POST al dispositivo IoT (es. ESP32, Raspberry)
url = f"{webhook_url}?scene={urllib.parse.quote(scene_name)}"
req = urllib.request.Request(url, method="POST")
urllib.request.urlopen(req, timeout=0.5)
print(f"💡 Tally Hardware acceso per: {scene_name}")
except Exception as e:
print(f"⚠️ Webhook Tally Error: {e}")
# --- ROUTING DELLE PAGINE ---
@app.route('/')
def index(): return render_template('index.html')
@app.route('/camera')
def camera_select(): return render_template('camera.html', scenes=get_obs_scenes())
@app.route('/talent')
def talent_monitor(): return render_template('talent.html')
# --- API E CSV EXPORT ---
@app.route('/api/scenes')
def api_scenes(): return jsonify(get_obs_scenes())
@app.route('/api/transitions')
def api_transitions():
if not obs_client: return jsonify(["Cut", "Fade"])
try:
resp = obs_client.get_scene_transition_list()
return jsonify([t['transitionName'] for t in resp.transitions])
except:
return jsonify(["Cut", "Fade"])
@app.route('/api/compilations', methods=['GET', 'POST'])
def api_compilations():
comps = load_compilations()
if request.method == 'POST':
data = request.json
name = data.get('name')
if not name: return jsonify({"error": "Missing name"}), 400
comps[name] = data.get('rundown')
with open(COMPILATIONS_FILE, 'w') as f:
json.dump(comps, f)
return jsonify({"status": "success", "compilations": list(comps.keys())})
return jsonify(comps)
@app.route('/api/export_csv/<project_name>')
def export_csv(project_name):
comps = load_compilations()
rundown = comps.get(project_name, [])
si = io.StringIO()
cw = csv.writer(si)
cw.writerow(['Shot Number', 'Start Time (s)', 'End Time (s)', 'Camera/Scene', 'Movement', 'Transition', 'Duration', 'Director Note'])
for i, clip in enumerate(rundown):
cw.writerow([
i + 1,
clip.get('start', 0),
clip.get('end', 0),
clip.get('scene', ''),
clip.get('movement', 'Static'),
clip.get('transition', 'Cut'),
clip.get('transition_duration', 0),
clip.get('note', '')
])
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = f"attachment; filename={project_name}_rundown.csv"
output.headers["Content-type"] = "text/csv"
return output
def get_obs_scenes():
if not obs_client: return ["Camera 1", "Camera 2", "Camera 3"]
try:
return [scene['sceneName'] for scene in obs_client.get_scene_list().scenes]
except:
return []
@socketio.on('req_obs_status')
def handle_obs_status():
config = load_obs_config()
socketio.emit('obs_status', {'connected': obs_connected, 'error': obs_error, 'host': config['host'], 'port': config['port'], 'webhook_url': config.get('webhook_url', '')}, room=request.sid)
@socketio.on('save_obs_config')
def handle_save_obs(data):
with open(OBS_CONFIG_FILE, 'w') as f:
json.dump({
"host": data.get('host', 'localhost'),
"port": int(data.get('port', 4455)),
"password": data.get('password', ''),
"webhook_url": data.get('webhook_url', '')
}, f)
connect_obs()
socketio.emit('obs_status', {'connected': obs_connected, 'error': obs_error, 'host': data.get('host', 'localhost'), 'port': int(data.get('port', 4455)), 'webhook_url': data.get('webhook_url', '')})
@socketio.on('req_scenes_refresh')
def handle_scenes_refresh():
socketio.emit('scenes_list', get_obs_scenes(), room=request.sid)
try:
if obs_client:
resp = obs_client.get_scene_transition_list()
socketio.emit('transitions_list', [t['transitionName'] for t in resp.transitions], room=request.sid)
except: pass
def run_show():
global is_playing, start_time, active_rundown, show_mode, force_timeline_update
global time_offset, is_paused, pause_timestamp, triggered_webhooks
rundown = sorted(active_rundown, key=lambda x: x['start'])
last_triggered_start = None
while is_playing:
if is_paused: elapsed = (pause_timestamp - start_time) + time_offset
else: elapsed = (time.time() - start_time) + time_offset
if elapsed < 0: elapsed = 0.0
socketio.emit('timecode', {'time': elapsed})
if show_mode == 'playback' and not is_paused:
target_shot = None
for item in rundown:
if elapsed >= item["start"]:
item_type = item.get("itemType", item.get("type", "shot"))
# Esecuzione Webhook
if item_type == "webhook":
wh_id = f"{item['start']}_{item.get('url')}"
if wh_id not in triggered_webhooks:
triggered_webhooks.add(wh_id)
trigger_custom_webhook(item)
# Aggiornamento Telecamera
if item_type == "shot":
target_shot = item
else: break
if target_shot and (force_timeline_update or last_triggered_start != target_shot["start"]):
last_triggered_start = target_shot["start"]
force_timeline_update = False
if obs_client:
t_name = target_shot.get("transition", "").strip()
if t_name and t_name.lower() not in ["cut", "taglio"]:
try:
obs_client.set_current_scene_transition(t_name)
obs_client.set_current_scene_transition_duration(int(target_shot.get("transition_duration", 300)))
except: pass
try:
obs_client.set_current_program_scene(target_shot["scene"])
trigger_physical_tally(target_shot["scene"])
except: pass
if show_mode == 'playback' and len(rundown) > 0 and not is_paused:
if elapsed > rundown[-1].get("end", rundown[-1]["start"] + 10):
is_playing = False
socketio.emit('status', {'msg': 'Show Ended'})
if obs_client:
try:
status = obs_client.get_record_status()
if status.output_active:
obs_client.stop_record()
except: pass
break
socketio.sleep(0.05)
def trigger_custom_webhook(item):
def _send():
try:
url = item.get('url', '')
method = item.get('method', 'POST')
payload = item.get('payload', '')
if not url: return
print(f"🌐 Triggering Webhook: {method} {url}")
req = urllib.request.Request(url, method=method)
if method == 'POST' and payload:
req.add_header('Content-Type', 'application/json')
req.data = payload.encode('utf-8')
urllib.request.urlopen(req, timeout=2.0)
except Exception as e:
print(f"⚠️ Custom Webhook Error: {e}")
threading.Thread(target=_send).start()
@socketio.on('start_show')
def handle_start(data):
global is_playing, start_time, active_rundown, show_mode, force_timeline_update, time_offset, is_paused, triggered_webhooks
if not is_playing:
active_rundown = data.get('rundown', [])
show_mode = data.get('mode', 'playback')
obs_audio_source = data.get('obs_audio_source', '').strip()
auto_record = data.get('auto_record', True)
force_timeline_update = True
time_offset = 0.0
is_paused = False
triggered_webhooks = set()
socketio.emit('hold_state', {'held': False})
if obs_client:
# 1. Avvia base musicale
if obs_audio_source:
try: obs_client.trigger_media_input_action(obs_audio_source, 'OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART')
except Exception as e: print(f"Audio Start Error: {e}")
# 2. Avvia registrazione in sicurezza
if auto_record:
try:
status = obs_client.get_record_status()
if not status.output_active:
obs_client.start_record()
print("⏺️ OBS Master Recording Started automatically!")
except Exception as e:
print(f"⚠️ OBS Record Start Warning: {e}")
socketio.emit('show_started', {'mode': show_mode})
if show_mode == 'playback': socketio.emit('rundown_updated', active_rundown)
is_playing = True
start_time = time.time()
socketio.start_background_task(run_show)
socketio.emit('status', {'msg': f'LIVE ({show_mode.upper()})'})
@socketio.on('change_mode')
def handle_change_mode(data):
global show_mode, force_timeline_update
show_mode = data.get('mode', 'playback')
if show_mode == 'playback': force_timeline_update = True
socketio.emit('mode_changed', {'mode': show_mode})
socketio.emit('status', {'msg': f'LIVE ({show_mode.upper()})'})
@socketio.on('stop_show')
def handle_stop():
global is_playing, is_paused
is_playing = False
is_paused = False
# Stoppa registrazione in sicurezza
if obs_client:
try:
status = obs_client.get_record_status()
if status.output_active:
obs_client.stop_record()
print("⏹ OBS Master Recording Stopped.")
except Exception as e:
print(f"⚠️ OBS Record Stop Warning: {e}")
socketio.emit('status', {'msg': 'Stopped'})
socketio.emit('hold_state', {'held': False})
@socketio.on('live_action')
def handle_live_action(data):
scene = data.get('scene')
trans = data.get('transition', '').strip()
dur = data.get('transition_duration') or 300
if obs_client and scene:
if trans and trans.lower() not in ["cut", "taglio"]:
try:
obs_client.set_current_scene_transition(trans)
obs_client.set_current_scene_transition_duration(int(dur))
except: pass
try:
obs_client.set_current_program_scene(scene)
trigger_physical_tally(scene) # Accende Tally Fisico
except: pass
socketio.emit('live_cut', {'scene': scene})
@socketio.on('nudge_time')
def handle_nudge(data):
global time_offset
time_offset += data.get('amount', 0.0)
@socketio.on('toggle_hold')
def handle_toggle_hold():
global is_paused, pause_timestamp, time_offset
if not is_paused:
is_paused = True
pause_timestamp = time.time()
socketio.emit('hold_state', {'held': True})
else:
is_paused = False
time_offset -= (time.time() - pause_timestamp)
socketio.emit('hold_state', {'held': False})
@socketio.on('send_urgent_message')
def handle_urgent_msg(data):
socketio.emit('urgent_message', data)
@socketio.on('intercom_audio')
def handle_intercom_audio(data):
socketio.emit('audio_broadcast', data, include_self=False)
@socketio.on('get_status_sync')
def handle_sync():
global active_rundown, show_mode, is_playing
socketio.emit('rundown_updated', active_rundown, room=request.sid)
socketio.emit('show_started', {'mode': show_mode}, room=request.sid)
if is_playing:
socketio.emit('status', {'msg': f'LIVE ({show_mode.upper()})'}, room=request.sid)
if __name__ == '__main__':
if not os.path.exists(COMPILATIONS_FILE):
with open(COMPILATIONS_FILE, 'w') as f: json.dump({}, f)
socketio.run(app, debug=True, port=5000)