davinci-resolve-mcp 2.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/AGENTS.md +85 -0
  2. package/CHANGELOG.md +802 -0
  3. package/CLAUDE.md +15 -0
  4. package/LICENSE +21 -0
  5. package/README.md +159 -0
  6. package/SECURITY.md +53 -0
  7. package/bin/davinci-resolve-mcp.mjs +376 -0
  8. package/docs/README.md +56 -0
  9. package/docs/SKILL.md +1145 -0
  10. package/docs/authoring/fuse-dctl-authoring.md +242 -0
  11. package/docs/authoring/script-plugin-authoring.md +195 -0
  12. package/docs/contributing.md +82 -0
  13. package/docs/guides/color-decision-guide.md +387 -0
  14. package/docs/guides/editorial-decision-guide.md +136 -0
  15. package/docs/guides/media-analysis-guide.md +615 -0
  16. package/docs/guides/multicam-setup-guide.md +138 -0
  17. package/docs/install.md +198 -0
  18. package/docs/integrations/workflow-integrations.md +120 -0
  19. package/docs/kernels/README.md +28 -0
  20. package/docs/kernels/audio-fairlight-kernel.md +86 -0
  21. package/docs/kernels/color-grade-kernel.md +103 -0
  22. package/docs/kernels/extension-authoring-kernel.md +101 -0
  23. package/docs/kernels/fusion-composition-kernel.md +91 -0
  24. package/docs/kernels/media-pool-ingest-kernel.md +147 -0
  25. package/docs/kernels/project-lifecycle-kernel.md +120 -0
  26. package/docs/kernels/render-deliver-kernel.md +92 -0
  27. package/docs/kernels/review-annotation-kernel.md +110 -0
  28. package/docs/kernels/timeline-conform-interchange-kernel.md +99 -0
  29. package/docs/kernels/timeline-edit-kernel.md +189 -0
  30. package/docs/notes/codec-plugin-notes.md +136 -0
  31. package/docs/notes/dctl-notes.md +234 -0
  32. package/docs/notes/fusion-template-notes.md +136 -0
  33. package/docs/notes/lut-notes.md +136 -0
  34. package/docs/notes/openfx-notes.md +120 -0
  35. package/docs/process/release-process.md +152 -0
  36. package/docs/reference/api-coverage.md +488 -0
  37. package/docs/reference/resolve_scripting_api.txt +1012 -0
  38. package/examples/README.md +53 -0
  39. package/examples/markers/README.md +81 -0
  40. package/examples/media/README.md +94 -0
  41. package/examples/timeline/README.md +98 -0
  42. package/install.py +1196 -0
  43. package/package.json +52 -0
  44. package/scripts/audit_api_parity.py +275 -0
  45. package/scripts/live_media_analysis_polish_probe.py +65 -0
  46. package/src/__init__.py +3 -0
  47. package/src/analysis_dashboard.py +4936 -0
  48. package/src/control_panel.py +13 -0
  49. package/src/granular/__init__.py +17 -0
  50. package/src/granular/common.py +727 -0
  51. package/src/granular/folder.py +287 -0
  52. package/src/granular/gallery.py +306 -0
  53. package/src/granular/graph.py +309 -0
  54. package/src/granular/media_pool.py +679 -0
  55. package/src/granular/media_pool_item.py +852 -0
  56. package/src/granular/media_storage.py +179 -0
  57. package/src/granular/project.py +1594 -0
  58. package/src/granular/resolve_control.py +521 -0
  59. package/src/granular/timeline.py +1074 -0
  60. package/src/granular/timeline_item.py +2251 -0
  61. package/src/resolve_mcp_server.py +43 -0
  62. package/src/server.py +15691 -0
  63. package/src/utils/__init__.py +3 -0
  64. package/src/utils/app_control.py +319 -0
  65. package/src/utils/audio_fairlight_live_probe.py +263 -0
  66. package/src/utils/cdl.py +20 -0
  67. package/src/utils/cloud_operations.py +192 -0
  68. package/src/utils/color_grade_live_probe.py +444 -0
  69. package/src/utils/dctl_templates.py +368 -0
  70. package/src/utils/extension_authoring_live_probe.py +292 -0
  71. package/src/utils/fuse_templates.py +1968 -0
  72. package/src/utils/fusion_composition_live_probe.py +284 -0
  73. package/src/utils/layout_presets.py +333 -0
  74. package/src/utils/mcp_stdio.py +32 -0
  75. package/src/utils/media_analysis.py +3618 -0
  76. package/src/utils/media_analysis_jobs.py +796 -0
  77. package/src/utils/media_pool_ingest_live_probe.py +592 -0
  78. package/src/utils/multicam.py +393 -0
  79. package/src/utils/object_inspection.py +287 -0
  80. package/src/utils/platform.py +157 -0
  81. package/src/utils/project_lifecycle_live_probe.py +376 -0
  82. package/src/utils/project_properties.py +601 -0
  83. package/src/utils/render_deliver_live_probe.py +384 -0
  84. package/src/utils/resolve_connection.py +77 -0
  85. package/src/utils/review_annotation_live_probe.py +352 -0
  86. package/src/utils/script_templates.py +1193 -0
  87. package/src/utils/sync_detection.py +887 -0
  88. package/src/utils/timeline_conform_live_probe.py +280 -0
  89. package/src/utils/timeline_kernel_live_probe.py +1091 -0
  90. package/src/utils/timeline_kernel_probe.py +185 -0
  91. package/src/utils/timeline_title_text.py +87 -0
  92. package/src/utils/update_check.py +610 -0
@@ -0,0 +1,887 @@
1
+ """Source-safe sync event detection for 2-pops and slate claps.
2
+
3
+ The detector reads source audio through ffprobe/ffmpeg and returns advisory
4
+ sync points. It never writes media, creates derivatives, or modifies Resolve.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import math
11
+ import os
12
+ import shutil
13
+ import subprocess
14
+ import sys
15
+ from array import array
16
+ from pathlib import Path
17
+ from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple
18
+
19
+ from src.utils.multicam import timecode_to_frames
20
+
21
+
22
+ SYNC_EVENT_TYPES = ("two_pop", "slate_clap")
23
+ DEFAULT_SAMPLE_RATE = 16000
24
+ DEFAULT_HEAD_SCAN_SECONDS = 30.0
25
+ DEFAULT_TAIL_SCAN_SECONDS = 30.0
26
+ DEFAULT_COMMAND_TIMEOUT_SECONDS = 180
27
+
28
+
29
+ def _err(message: str, **extra: Any) -> Dict[str, Any]:
30
+ payload = {"success": False, "error": message}
31
+ payload.update(extra)
32
+ return payload
33
+
34
+
35
+ def _coerce_bool(value: Any, default: bool = False) -> bool:
36
+ if value is None:
37
+ return default
38
+ if isinstance(value, bool):
39
+ return value
40
+ if isinstance(value, str):
41
+ return value.strip().lower() in {"1", "true", "yes", "y", "on"}
42
+ return bool(value)
43
+
44
+
45
+ def _coerce_float(value: Any, default: Optional[float] = None) -> Optional[float]:
46
+ if value in (None, ""):
47
+ return default
48
+ try:
49
+ return float(value)
50
+ except (TypeError, ValueError):
51
+ return default
52
+
53
+
54
+ def _coerce_int(value: Any, default: Optional[int] = None) -> Optional[int]:
55
+ if value in (None, ""):
56
+ return default
57
+ try:
58
+ return int(float(value))
59
+ except (TypeError, ValueError):
60
+ return default
61
+
62
+
63
+ def _fraction_to_float(value: Any) -> Optional[float]:
64
+ if value in (None, "", "0/0"):
65
+ return None
66
+ raw = str(value)
67
+ if "/" in raw:
68
+ numerator, denominator = raw.split("/", 1)
69
+ try:
70
+ denominator_f = float(denominator)
71
+ if denominator_f == 0:
72
+ return None
73
+ return float(numerator) / denominator_f
74
+ except ValueError:
75
+ return None
76
+ return _coerce_float(raw)
77
+
78
+
79
+ def _dbfs(value: float) -> float:
80
+ return round(20.0 * math.log10(max(abs(value), 1e-12)), 2)
81
+
82
+
83
+ def _percentile(values: Sequence[float], percentile: float) -> float:
84
+ if not values:
85
+ return 0.0
86
+ ordered = sorted(values)
87
+ if len(ordered) == 1:
88
+ return float(ordered[0])
89
+ index = max(0.0, min(1.0, percentile)) * (len(ordered) - 1)
90
+ lower = int(math.floor(index))
91
+ upper = int(math.ceil(index))
92
+ if lower == upper:
93
+ return float(ordered[lower])
94
+ weight = index - lower
95
+ return float((ordered[lower] * (1.0 - weight)) + (ordered[upper] * weight))
96
+
97
+
98
+ def _normalize_event_types(value: Any) -> Tuple[List[str], Optional[str]]:
99
+ if value in (None, "", "all"):
100
+ return list(SYNC_EVENT_TYPES), None
101
+ raw_values = value if isinstance(value, list) else [value]
102
+ normalized = []
103
+ aliases = {
104
+ "two-pop": "two_pop",
105
+ "2-pop": "two_pop",
106
+ "2pop": "two_pop",
107
+ "pop": "two_pop",
108
+ "slate": "slate_clap",
109
+ "slate-clap": "slate_clap",
110
+ "clap": "slate_clap",
111
+ "slate_clap": "slate_clap",
112
+ "all": "all",
113
+ }
114
+ for raw in raw_values:
115
+ key = str(raw or "").strip().lower().replace(" ", "_")
116
+ event_type = aliases.get(key, key)
117
+ if event_type == "all":
118
+ return list(SYNC_EVENT_TYPES), None
119
+ if event_type not in SYNC_EVENT_TYPES:
120
+ return [], f"Unknown sync event type '{raw}'. Valid: {list(SYNC_EVENT_TYPES)}"
121
+ if event_type not in normalized:
122
+ normalized.append(event_type)
123
+ return normalized or list(SYNC_EVENT_TYPES), None
124
+
125
+
126
+ def detect_sync_event_capabilities() -> Dict[str, Any]:
127
+ ffmpeg = shutil.which("ffmpeg")
128
+ ffprobe = shutil.which("ffprobe")
129
+ return {
130
+ "success": True,
131
+ "available": bool(ffmpeg and ffprobe),
132
+ "no_auto_install": True,
133
+ "tools": {
134
+ "ffmpeg": {"available": bool(ffmpeg), "path": ffmpeg},
135
+ "ffprobe": {"available": bool(ffprobe), "path": ffprobe},
136
+ },
137
+ "event_types": list(SYNC_EVENT_TYPES),
138
+ "analysis": {
139
+ "source_safe": True,
140
+ "writes_media": False,
141
+ "default_windows": ["head", "tail"],
142
+ "outputs": ["event times", "frames", "record_offset suggestions"],
143
+ },
144
+ }
145
+
146
+
147
+ def sync_event_install_guidance(capabilities: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
148
+ caps = capabilities or detect_sync_event_capabilities()
149
+ missing = {}
150
+ tools = caps.get("tools", {})
151
+ if not tools.get("ffmpeg", {}).get("available") or not tools.get("ffprobe", {}).get("available"):
152
+ missing["ffmpeg_suite"] = {
153
+ "required_for": ["2-pop detection", "slate-clap detection", "sync offset suggestions"],
154
+ "macos": "Ask the user before running: brew install ffmpeg",
155
+ "linux": "Ask the user to install ffmpeg with their distribution package manager.",
156
+ "windows": "Ask the user to install ffmpeg and add ffmpeg/ffprobe to PATH.",
157
+ }
158
+ return {"success": True, "no_auto_install": True, "missing": missing}
159
+
160
+
161
+ def _run_json(args: List[str], timeout: int) -> Tuple[int, Dict[str, Any], str]:
162
+ try:
163
+ proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=timeout)
164
+ except subprocess.TimeoutExpired:
165
+ return 124, {}, f"Command timed out after {timeout}s"
166
+ except OSError as exc:
167
+ return 127, {}, str(exc)
168
+ try:
169
+ payload = json.loads(proc.stdout or "{}")
170
+ except json.JSONDecodeError:
171
+ payload = {}
172
+ return proc.returncode, payload, proc.stderr or ""
173
+
174
+
175
+ def _probe_media(path: str, ffprobe_path: str, timeout: int) -> Dict[str, Any]:
176
+ code, raw, stderr = _run_json(
177
+ [
178
+ ffprobe_path,
179
+ "-v",
180
+ "quiet",
181
+ "-print_format",
182
+ "json",
183
+ "-show_format",
184
+ "-show_streams",
185
+ path,
186
+ ],
187
+ timeout,
188
+ )
189
+ if code != 0:
190
+ return _err(stderr.strip() or "ffprobe failed")
191
+
192
+ streams = raw.get("streams") or []
193
+ fmt = raw.get("format") or {}
194
+ duration = _coerce_float(fmt.get("duration"))
195
+ audio_streams = []
196
+ video_streams = []
197
+ source_timecode = (fmt.get("tags") or {}).get("timecode")
198
+
199
+ for stream in streams:
200
+ tags = stream.get("tags") or {}
201
+ if not source_timecode and tags.get("timecode"):
202
+ source_timecode = tags.get("timecode")
203
+ if stream.get("codec_type") == "audio":
204
+ if duration is None:
205
+ duration = _coerce_float(stream.get("duration"))
206
+ audio_streams.append({
207
+ "index": stream.get("index"),
208
+ "codec": stream.get("codec_name"),
209
+ "sample_rate": _coerce_int(stream.get("sample_rate")),
210
+ "channels": stream.get("channels"),
211
+ "duration_seconds": _coerce_float(stream.get("duration")),
212
+ })
213
+ elif stream.get("codec_type") == "video":
214
+ frame_rate = _fraction_to_float(stream.get("avg_frame_rate")) or _fraction_to_float(stream.get("r_frame_rate"))
215
+ if duration is None:
216
+ duration = _coerce_float(stream.get("duration"))
217
+ video_streams.append({
218
+ "index": stream.get("index"),
219
+ "codec": stream.get("codec_name"),
220
+ "frame_rate": frame_rate,
221
+ "duration_seconds": _coerce_float(stream.get("duration")),
222
+ })
223
+
224
+ return {
225
+ "success": True,
226
+ "duration_seconds": duration,
227
+ "source_timecode": source_timecode,
228
+ "audio_streams": audio_streams,
229
+ "video_streams": video_streams,
230
+ "format": {
231
+ "format_name": fmt.get("format_name"),
232
+ "size_bytes": _coerce_int(fmt.get("size")),
233
+ "duration_seconds": _coerce_float(fmt.get("duration")),
234
+ },
235
+ }
236
+
237
+
238
+ def _nominal_timecode_rate(fps: float) -> int:
239
+ if abs(fps - 23.976) < 0.02:
240
+ return 24
241
+ if abs(fps - 29.97) < 0.02:
242
+ return 30
243
+ if abs(fps - 47.952) < 0.05:
244
+ return 48
245
+ if abs(fps - 59.94) < 0.05:
246
+ return 60
247
+ return int(round(fps))
248
+
249
+
250
+ def _frames_to_timecode(frame: int, fps: float) -> Optional[str]:
251
+ if fps <= 0:
252
+ return None
253
+ nominal = _nominal_timecode_rate(fps)
254
+ if nominal <= 0:
255
+ return None
256
+ frame = max(0, int(frame))
257
+ hours, remainder = divmod(frame, nominal * 3600)
258
+ minutes, remainder = divmod(remainder, nominal * 60)
259
+ seconds, frames = divmod(remainder, nominal)
260
+ return f"{hours:02d}:{minutes:02d}:{seconds:02d}:{frames:02d}"
261
+
262
+
263
+ def _timecode_for_event(time_seconds: float, fps: Optional[float], start_timecode: Optional[str]) -> Optional[str]:
264
+ if not fps or not start_timecode:
265
+ return None
266
+ start_frame = timecode_to_frames(start_timecode, fps)
267
+ if start_frame is None:
268
+ return None
269
+ return _frames_to_timecode(start_frame + int(round(time_seconds * fps)), fps)
270
+
271
+
272
+ def _event_marker_color(event_type: str, params: Dict[str, Any]) -> str:
273
+ override = params.get("marker_color") or params.get("markerColor")
274
+ if override:
275
+ return str(override)
276
+ return "Cyan" if event_type == "two_pop" else "Yellow"
277
+
278
+
279
+ def _event_marker_name(event: Dict[str, Any], params: Dict[str, Any]) -> str:
280
+ prefix = str(params.get("marker_name_prefix") or params.get("markerNamePrefix") or "Sync")
281
+ return f"{prefix}: {event.get('label') or event.get('type') or 'event'}"
282
+
283
+
284
+ def _event_marker_custom_data(record: Dict[str, Any], event: Dict[str, Any]) -> str:
285
+ clip_key = record.get("clip_id") or Path(str(record.get("file_path") or record.get("clip_name") or "file")).name
286
+ frame_key = event.get("frame")
287
+ if frame_key is None:
288
+ frame_key = f"{float(event.get('time_seconds') or 0.0):.3f}s"
289
+ return f"mcp.sync_event:{clip_key}:{event.get('type')}:{frame_key}"
290
+
291
+
292
+ def _event_marker_suggestion(record: Dict[str, Any], event: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
293
+ clip_id = record.get("clip_id")
294
+ frame = event.get("frame")
295
+ note = (
296
+ f"Detected {event.get('label')} at {event.get('time_seconds')}s"
297
+ f" (confidence {event.get('confidence')}). Verify sync before using."
298
+ )
299
+ if event.get("timecode"):
300
+ note += f" Timecode: {event['timecode']}."
301
+ marker = {
302
+ "frame": frame,
303
+ "color": _event_marker_color(str(event.get("type") or ""), params),
304
+ "name": _event_marker_name(event, params),
305
+ "note": note,
306
+ "duration": max(1, _coerce_int(params.get("marker_duration_frames"), 1) or 1),
307
+ "custom_data": _event_marker_custom_data(record, event),
308
+ }
309
+ return {
310
+ "scope": "media_pool_item",
311
+ "clip_id": clip_id,
312
+ "clip_name": record.get("clip_name"),
313
+ "event_type": event.get("type"),
314
+ "event_time_seconds": event.get("time_seconds"),
315
+ "event_frame": frame,
316
+ "confidence": event.get("confidence"),
317
+ "marker": marker,
318
+ "eligible": bool(clip_id and frame is not None),
319
+ "requires_confirmation": True,
320
+ "reason": None if clip_id and frame is not None else "Requires a Media Pool clip id and a detected event frame.",
321
+ }
322
+
323
+
324
+ def _analysis_windows(duration: Optional[float], params: Dict[str, Any]) -> Tuple[List[Dict[str, float]], List[str]]:
325
+ warnings: List[str] = []
326
+ raw_windows = params.get("windows")
327
+ max_window = max(1.0, _coerce_float(params.get("max_window_seconds"), 120.0) or 120.0)
328
+
329
+ if isinstance(raw_windows, list) and raw_windows:
330
+ windows = []
331
+ for index, raw in enumerate(raw_windows):
332
+ if not isinstance(raw, dict):
333
+ warnings.append(f"Skipping windows[{index}] because it is not an object")
334
+ continue
335
+ start = max(0.0, _coerce_float(raw.get("start_seconds", raw.get("start")), 0.0) or 0.0)
336
+ window_duration = _coerce_float(raw.get("duration_seconds", raw.get("duration")), None)
337
+ if window_duration is None and duration is not None:
338
+ window_duration = max(0.0, duration - start)
339
+ if window_duration is None or window_duration <= 0:
340
+ warnings.append(f"Skipping windows[{index}] because duration is missing or non-positive")
341
+ continue
342
+ if window_duration > max_window and not _coerce_bool(params.get("allow_long_windows"), False):
343
+ warnings.append(f"Clamped windows[{index}] from {window_duration:.3f}s to {max_window:.3f}s")
344
+ window_duration = max_window
345
+ windows.append({
346
+ "label": str(raw.get("label") or f"window_{index + 1}"),
347
+ "start": start,
348
+ "duration": window_duration,
349
+ })
350
+ return windows, warnings
351
+
352
+ head = max(0.0, _coerce_float(params.get("scan_start_seconds"), DEFAULT_HEAD_SCAN_SECONDS) or 0.0)
353
+ tail = max(0.0, _coerce_float(params.get("scan_tail_seconds"), DEFAULT_TAIL_SCAN_SECONDS) or 0.0)
354
+
355
+ if _coerce_bool(params.get("scan_full"), False):
356
+ full_duration = duration or max_window
357
+ if full_duration > max_window and not _coerce_bool(params.get("allow_long_windows"), False):
358
+ warnings.append(f"Clamped full scan from {full_duration:.3f}s to {max_window:.3f}s")
359
+ full_duration = max_window
360
+ return [{"label": "full", "start": 0.0, "duration": max(0.0, full_duration)}], warnings
361
+
362
+ if duration is None:
363
+ return [{"label": "head", "start": 0.0, "duration": min(head or max_window, max_window)}], warnings
364
+
365
+ windows = []
366
+ if head > 0:
367
+ windows.append({"label": "head", "start": 0.0, "duration": min(head, duration, max_window)})
368
+ if tail > 0 and duration > 0:
369
+ tail_duration = min(tail, duration, max_window)
370
+ tail_start = max(0.0, duration - tail_duration)
371
+ if not windows or tail_start > windows[-1]["start"] + windows[-1]["duration"] - 0.5:
372
+ windows.append({"label": "tail", "start": tail_start, "duration": tail_duration})
373
+ return windows, warnings
374
+
375
+
376
+ def _decode_audio_window(
377
+ path: str,
378
+ window: Dict[str, float],
379
+ *,
380
+ ffmpeg_path: str,
381
+ audio_stream_index: int,
382
+ sample_rate: int,
383
+ timeout: int,
384
+ ) -> Tuple[Optional[array], Optional[str]]:
385
+ args = [
386
+ ffmpeg_path,
387
+ "-hide_banner",
388
+ "-loglevel",
389
+ "error",
390
+ "-nostdin",
391
+ ]
392
+ if window["start"] > 0:
393
+ args.extend(["-ss", f"{window['start']:.6f}"])
394
+ args.extend([
395
+ "-i",
396
+ path,
397
+ "-t",
398
+ f"{window['duration']:.6f}",
399
+ "-map",
400
+ f"0:a:{audio_stream_index}",
401
+ "-vn",
402
+ "-ac",
403
+ "1",
404
+ "-ar",
405
+ str(sample_rate),
406
+ "-f",
407
+ "f32le",
408
+ "-",
409
+ ])
410
+ try:
411
+ proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
412
+ except subprocess.TimeoutExpired:
413
+ return None, f"ffmpeg audio decode timed out after {timeout}s"
414
+ except OSError as exc:
415
+ return None, str(exc)
416
+ if proc.returncode != 0:
417
+ return None, (proc.stderr or b"ffmpeg audio decode failed").decode("utf-8", errors="replace").strip()
418
+
419
+ data = proc.stdout[: len(proc.stdout) - (len(proc.stdout) % 4)]
420
+ samples = array("f")
421
+ samples.frombytes(data)
422
+ if sys.byteorder != "little":
423
+ samples.byteswap()
424
+ return samples, None
425
+
426
+
427
+ def _estimate_frequency(samples: Sequence[float], sample_rate: int) -> Optional[float]:
428
+ if len(samples) < max(8, sample_rate // 2000):
429
+ return None
430
+ active_indices = [index for index, sample in enumerate(samples) if abs(sample) >= 1e-4]
431
+ if len(active_indices) < 4:
432
+ return None
433
+ first = active_indices[0]
434
+ last = active_indices[-1]
435
+ samples = samples[first:last + 1]
436
+ crossings = 0
437
+ previous = 0
438
+ for sample in samples:
439
+ if abs(sample) < 1e-4:
440
+ continue
441
+ sign = 1 if sample > 0 else -1
442
+ if previous and sign != previous:
443
+ crossings += 1
444
+ previous = sign
445
+ duration = len(samples) / float(sample_rate)
446
+ if duration <= 0 or crossings < 2:
447
+ return None
448
+ return round(crossings / (2.0 * duration), 1)
449
+
450
+
451
+ def _window_envelope(samples: Sequence[float], sample_rate: int, step_seconds: float) -> List[Dict[str, float]]:
452
+ window_size = max(1, int(round(sample_rate * step_seconds)))
453
+ envelope = []
454
+ for start in range(0, len(samples), window_size):
455
+ chunk = samples[start:start + window_size]
456
+ if not chunk:
457
+ continue
458
+ peak = max(abs(float(value)) for value in chunk)
459
+ rms = math.sqrt(sum(float(value) * float(value) for value in chunk) / len(chunk))
460
+ envelope.append({
461
+ "sample_start": float(start),
462
+ "sample_end": float(min(start + window_size, len(samples))),
463
+ "peak": peak,
464
+ "rms": rms,
465
+ })
466
+ return envelope
467
+
468
+
469
+ def _group_active_windows(envelope: List[Dict[str, float]], params: Dict[str, Any]) -> List[Tuple[int, int]]:
470
+ if not envelope:
471
+ return []
472
+ rms_values = [row["rms"] for row in envelope]
473
+ peak_values = [row["peak"] for row in envelope]
474
+ noise_rms = _percentile(rms_values, 0.50)
475
+ p90_rms = _percentile(rms_values, 0.90)
476
+ p90_peak = _percentile(peak_values, 0.90)
477
+ absolute_rms = _coerce_float(params.get("absolute_rms_threshold"), 0.015) or 0.015
478
+ absolute_peak = _coerce_float(params.get("absolute_peak_threshold"), 0.08) or 0.08
479
+ rms_threshold = max(absolute_rms, noise_rms * 6.0, p90_rms * 0.45)
480
+ peak_threshold = max(absolute_peak, p90_peak * 0.60)
481
+
482
+ active = [
483
+ index
484
+ for index, row in enumerate(envelope)
485
+ if row["rms"] >= rms_threshold or row["peak"] >= peak_threshold
486
+ ]
487
+ if not active:
488
+ return []
489
+
490
+ groups = []
491
+ gap_limit = max(0, _coerce_int(params.get("merge_gap_windows"), 2) or 0)
492
+ start = active[0]
493
+ previous = active[0]
494
+ for index in active[1:]:
495
+ if index - previous <= gap_limit + 1:
496
+ previous = index
497
+ continue
498
+ groups.append((start, previous))
499
+ start = previous = index
500
+ groups.append((start, previous))
501
+ return groups
502
+
503
+
504
+ def _score_event(metrics: Dict[str, Any], event_types: List[str]) -> Tuple[Optional[str], float, Dict[str, float]]:
505
+ duration = float(metrics.get("duration_seconds") or 0)
506
+ frequency = metrics.get("estimated_frequency_hz")
507
+ crest = float(metrics.get("crest_factor") or 0)
508
+ peak_dbfs = float(metrics.get("peak_dbfs") or -120)
509
+ rms_dbfs = float(metrics.get("rms_dbfs") or -120)
510
+ onset = float(metrics.get("onset_ratio") or 0)
511
+
512
+ scores: Dict[str, float] = {}
513
+
514
+ if "two_pop" in event_types:
515
+ tonal = 0.0
516
+ if frequency:
517
+ tonal = max(0.0, 1.0 - abs(float(frequency) - 1000.0) / 400.0)
518
+ score = 0.0
519
+ if 0.015 <= duration <= 0.40:
520
+ score += 0.25
521
+ elif 0.005 <= duration <= 0.75:
522
+ score += 0.10
523
+ score += tonal * 0.45
524
+ if rms_dbfs > -30:
525
+ score += 0.15
526
+ elif rms_dbfs > -42:
527
+ score += 0.08
528
+ if 0 < crest <= 4.0:
529
+ score += 0.10
530
+ if peak_dbfs > -24:
531
+ score += 0.05
532
+ scores["two_pop"] = min(0.99, score)
533
+
534
+ if "slate_clap" in event_types:
535
+ tonal_1k = bool(frequency and 800.0 <= float(frequency) <= 1200.0)
536
+ score = 0.0
537
+ if duration <= 0.25:
538
+ score += 0.20
539
+ elif duration <= 0.50:
540
+ score += 0.08
541
+ if peak_dbfs > -15:
542
+ score += 0.20
543
+ elif peak_dbfs > -30:
544
+ score += 0.10
545
+ if crest >= 4.0:
546
+ score += 0.25
547
+ elif crest >= 2.5:
548
+ score += 0.10
549
+ if not tonal_1k:
550
+ score += 0.15
551
+ if onset >= 8.0:
552
+ score += 0.20
553
+ elif onset >= 4.0:
554
+ score += 0.10
555
+ scores["slate_clap"] = min(0.99, score)
556
+
557
+ if not scores:
558
+ return None, 0.0, scores
559
+ best_type, best_score = max(scores.items(), key=lambda row: row[1])
560
+ return best_type, best_score, scores
561
+
562
+
563
+ def analyze_samples_for_sync_events(
564
+ samples: Sequence[float],
565
+ sample_rate: int,
566
+ *,
567
+ window_start_seconds: float = 0.0,
568
+ fps: Optional[float] = None,
569
+ start_timecode: Optional[str] = None,
570
+ event_types: Optional[List[str]] = None,
571
+ params: Optional[Dict[str, Any]] = None,
572
+ ) -> List[Dict[str, Any]]:
573
+ params = params or {}
574
+ event_types = event_types or list(SYNC_EVENT_TYPES)
575
+ step_seconds = max(0.002, _coerce_float(params.get("envelope_step_seconds"), 0.01) or 0.01)
576
+ envelope = _window_envelope(samples, sample_rate, step_seconds)
577
+ groups = _group_active_windows(envelope, params)
578
+ min_confidence = _coerce_float(params.get("min_confidence"), 0.45) or 0.45
579
+ events: List[Dict[str, Any]] = []
580
+
581
+ for start_index, end_index in groups:
582
+ start_sample = int(envelope[start_index]["sample_start"])
583
+ end_sample = int(envelope[end_index]["sample_end"])
584
+ event_segment = samples[start_sample:end_sample]
585
+ if not event_segment:
586
+ continue
587
+ peak = max(abs(float(value)) for value in event_segment)
588
+ rms = math.sqrt(sum(float(value) * float(value) for value in event_segment) / len(event_segment))
589
+ if rms <= 0 and peak <= 0:
590
+ continue
591
+ previous_rms = _percentile([row["rms"] for row in envelope[max(0, start_index - 10):start_index]], 0.50)
592
+ onset_ratio = peak / max(previous_rms, 1e-6)
593
+ duration_seconds = max(step_seconds, (end_sample - start_sample) / float(sample_rate))
594
+ local_time = start_sample / float(sample_rate)
595
+ absolute_time = window_start_seconds + local_time
596
+ estimated_frequency = _estimate_frequency(event_segment, sample_rate)
597
+ crest = peak / max(rms, 1e-9)
598
+ metrics = {
599
+ "duration_seconds": round(duration_seconds, 6),
600
+ "estimated_frequency_hz": estimated_frequency,
601
+ "peak_dbfs": _dbfs(peak),
602
+ "rms_dbfs": _dbfs(rms),
603
+ "crest_factor": round(crest, 3),
604
+ "onset_ratio": round(onset_ratio, 3),
605
+ }
606
+ event_type, confidence, scores = _score_event(metrics, event_types)
607
+ if not event_type or confidence < min_confidence:
608
+ continue
609
+ event_frame = int(round(absolute_time * fps)) if fps else None
610
+ event = {
611
+ "type": event_type,
612
+ "label": "2-pop" if event_type == "two_pop" else "slate clap",
613
+ "time_seconds": round(absolute_time, 6),
614
+ "window_local_time_seconds": round(local_time, 6),
615
+ "frame": event_frame,
616
+ "timecode": _timecode_for_event(absolute_time, fps, start_timecode),
617
+ "confidence": round(confidence, 3),
618
+ "scores": {key: round(value, 3) for key, value in scores.items()},
619
+ **metrics,
620
+ }
621
+ events.append(event)
622
+
623
+ return sorted(events, key=lambda row: (-row["confidence"], row["time_seconds"]))
624
+
625
+
626
+ def _record_path(record: Dict[str, Any]) -> Optional[str]:
627
+ path = record.get("file_path") or record.get("path")
628
+ if not path:
629
+ return None
630
+ return os.path.realpath(os.path.abspath(os.path.expanduser(str(path))))
631
+
632
+
633
+ def _record_fps(record: Dict[str, Any], probe: Dict[str, Any], params: Dict[str, Any]) -> Optional[float]:
634
+ explicit = _coerce_float(params.get("fps"))
635
+ if explicit:
636
+ return explicit
637
+ record_fps = _coerce_float(record.get("fps"))
638
+ if record_fps:
639
+ return record_fps
640
+ videos = probe.get("video_streams") or []
641
+ for video in videos:
642
+ if video.get("frame_rate"):
643
+ return _coerce_float(video.get("frame_rate"))
644
+ return None
645
+
646
+
647
+ def _start_timecode(record: Dict[str, Any], probe: Dict[str, Any], params: Dict[str, Any]) -> Optional[str]:
648
+ return (
649
+ params.get("start_timecode")
650
+ or params.get("source_timecode")
651
+ or record.get("start_timecode")
652
+ or record.get("source_timecode")
653
+ or probe.get("source_timecode")
654
+ )
655
+
656
+
657
+ def detect_sync_events_for_file(record: Dict[str, Any], params: Dict[str, Any], capabilities: Dict[str, Any]) -> Dict[str, Any]:
658
+ path = _record_path(record)
659
+ if not path:
660
+ return _err("Record has no file_path", clip_name=record.get("clip_name"))
661
+ if not os.path.isfile(path):
662
+ return _err(f"Media file not found: {path}", path=path, clip_name=record.get("clip_name"))
663
+
664
+ tools = capabilities.get("tools", {})
665
+ ffmpeg_path = tools.get("ffmpeg", {}).get("path") or "ffmpeg"
666
+ ffprobe_path = tools.get("ffprobe", {}).get("path") or "ffprobe"
667
+ timeout = _coerce_int(params.get("timeout_seconds"), DEFAULT_COMMAND_TIMEOUT_SECONDS) or DEFAULT_COMMAND_TIMEOUT_SECONDS
668
+ probe = _probe_media(path, ffprobe_path, timeout)
669
+ if not probe.get("success"):
670
+ probe.update({"path": path, "clip_name": record.get("clip_name")})
671
+ return probe
672
+ if not probe.get("audio_streams"):
673
+ return _err("No audio streams found for sync-event detection", path=path, clip_name=record.get("clip_name"))
674
+
675
+ event_types, event_err = _normalize_event_types(params.get("event_types"))
676
+ if event_err:
677
+ return _err(event_err, path=path, clip_name=record.get("clip_name"))
678
+
679
+ sample_rate = max(1000, _coerce_int(params.get("sample_rate"), DEFAULT_SAMPLE_RATE) or DEFAULT_SAMPLE_RATE)
680
+ audio_stream_index = max(0, _coerce_int(params.get("audio_stream_index"), 0) or 0)
681
+ fps = _record_fps(record, probe, params)
682
+ start_tc = _start_timecode(record, probe, params)
683
+ windows, warnings = _analysis_windows(probe.get("duration_seconds"), params)
684
+
685
+ all_events = []
686
+ for window in windows:
687
+ samples, decode_error = _decode_audio_window(
688
+ path,
689
+ window,
690
+ ffmpeg_path=ffmpeg_path,
691
+ audio_stream_index=audio_stream_index,
692
+ sample_rate=sample_rate,
693
+ timeout=timeout,
694
+ )
695
+ if decode_error:
696
+ warnings.append(f"{window['label']} decode failed: {decode_error}")
697
+ continue
698
+ events = analyze_samples_for_sync_events(
699
+ samples or [],
700
+ sample_rate,
701
+ window_start_seconds=window["start"],
702
+ fps=fps,
703
+ start_timecode=start_tc,
704
+ event_types=event_types,
705
+ params=params,
706
+ )
707
+ for event in events:
708
+ event["window"] = {
709
+ "label": window["label"],
710
+ "start_seconds": round(window["start"], 6),
711
+ "duration_seconds": round(window["duration"], 6),
712
+ }
713
+ all_events.extend(events)
714
+
715
+ max_events = max(1, _coerce_int(params.get("max_events_per_file"), 12) or 12)
716
+ all_events = sorted(all_events, key=lambda row: (-row["confidence"], row["time_seconds"]))[:max_events]
717
+ all_events = sorted(all_events, key=lambda row: row["time_seconds"])
718
+ marker_suggestions = [_event_marker_suggestion(record, event, params) for event in all_events]
719
+
720
+ return {
721
+ "success": True,
722
+ "clip_id": record.get("clip_id"),
723
+ "clip_name": record.get("clip_name") or Path(path).name,
724
+ "path": path,
725
+ "source_safe": True,
726
+ "writes_media": False,
727
+ "metadata": {
728
+ "duration_seconds": probe.get("duration_seconds"),
729
+ "fps": fps,
730
+ "source_timecode": start_tc,
731
+ "audio_streams": probe.get("audio_streams"),
732
+ "video_streams": probe.get("video_streams"),
733
+ },
734
+ "scan": {
735
+ "sample_rate": sample_rate,
736
+ "audio_stream_index": audio_stream_index,
737
+ "windows": windows,
738
+ "event_types": event_types,
739
+ },
740
+ "events": all_events,
741
+ "marker_suggestions": marker_suggestions,
742
+ "marker_write": {
743
+ "available": any(suggestion.get("eligible") for suggestion in marker_suggestions),
744
+ "requires_confirmation": True,
745
+ "action": "media_analysis(action='add_sync_event_markers')",
746
+ "note": "Detection only suggests markers; marker writes require an explicit confirmed action.",
747
+ },
748
+ "warnings": warnings,
749
+ }
750
+
751
+
752
+ def _best_alignment_event(file_result: Dict[str, Any], preferred_type: Optional[str]) -> Optional[Dict[str, Any]]:
753
+ events = file_result.get("events") or []
754
+ if preferred_type:
755
+ typed = [event for event in events if event.get("type") == preferred_type]
756
+ if typed:
757
+ return max(typed, key=lambda row: row.get("confidence", 0))
758
+ if not events:
759
+ return None
760
+ return max(events, key=lambda row: row.get("confidence", 0))
761
+
762
+
763
+ def _alignment_suggestions(file_results: List[Dict[str, Any]], params: Dict[str, Any]) -> Dict[str, Any]:
764
+ preferred_type = params.get("prefer_event_type") or params.get("alignment_event_type")
765
+ if preferred_type:
766
+ event_types, event_err = _normalize_event_types([preferred_type])
767
+ if event_err:
768
+ return {"success": False, "error": event_err}
769
+ preferred_type = event_types[0]
770
+
771
+ choices = []
772
+ for index, result in enumerate(file_results):
773
+ if not result.get("success"):
774
+ continue
775
+ event = _best_alignment_event(result, preferred_type)
776
+ if not event:
777
+ continue
778
+ fps = _coerce_float((result.get("metadata") or {}).get("fps")) or _coerce_float(params.get("fps")) or 24.0
779
+ event_frame = event.get("frame")
780
+ if event_frame is None:
781
+ event_frame = int(round(float(event.get("time_seconds") or 0.0) * fps))
782
+ choices.append((index, result, event, int(event_frame), fps))
783
+
784
+ if not choices:
785
+ return {
786
+ "success": True,
787
+ "status": "no_common_sync_events",
788
+ "suggestions": [],
789
+ "notes": ["No sync events were detected strongly enough to suggest record offsets."],
790
+ }
791
+
792
+ reference_index = _coerce_int(params.get("reference_index"), None)
793
+ reference_path = params.get("reference_path")
794
+ reference = None
795
+ if reference_path:
796
+ reference_path = os.path.realpath(os.path.abspath(os.path.expanduser(str(reference_path))))
797
+ for choice in choices:
798
+ if choice[1].get("path") == reference_path:
799
+ reference = choice
800
+ break
801
+ if reference is None and reference_index is not None:
802
+ for choice in choices:
803
+ if choice[0] == reference_index:
804
+ reference = choice
805
+ break
806
+ if reference is None:
807
+ reference = choices[0]
808
+
809
+ _, reference_result, reference_event, reference_frame, reference_fps = reference
810
+ suggestions = []
811
+ for index, result, event, event_frame, fps in choices:
812
+ offset_frames = reference_frame - event_frame
813
+ offset_seconds = offset_frames / float(fps or reference_fps or 24.0)
814
+ suggestions.append({
815
+ "index": index,
816
+ "clip_id": result.get("clip_id"),
817
+ "clip_name": result.get("clip_name"),
818
+ "path": result.get("path"),
819
+ "event_type": event.get("type"),
820
+ "event_time_seconds": event.get("time_seconds"),
821
+ "event_frame": event_frame,
822
+ "confidence": event.get("confidence"),
823
+ "suggested_record_offset_frames": offset_frames,
824
+ "suggested_record_offset_seconds": round(offset_seconds, 6),
825
+ })
826
+
827
+ return {
828
+ "success": True,
829
+ "reference": {
830
+ "clip_id": reference_result.get("clip_id"),
831
+ "clip_name": reference_result.get("clip_name"),
832
+ "path": reference_result.get("path"),
833
+ "event_type": reference_event.get("type"),
834
+ "event_time_seconds": reference_event.get("time_seconds"),
835
+ "event_frame": reference_frame,
836
+ "confidence": reference_event.get("confidence"),
837
+ },
838
+ "suggestions": suggestions,
839
+ "notes": [
840
+ "Use suggested_record_offset_frames as per-angle record_offset values with "
841
+ "media_pool.setup_multicam_timeline(sync_mode='record_frame').",
842
+ "Offsets are advisory; verify sync visually and audibly in Resolve before converting to a native multicam clip.",
843
+ ],
844
+ }
845
+
846
+
847
+ def detect_sync_events_for_records(records: Iterable[Dict[str, Any]], params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
848
+ params = dict(params or {})
849
+ caps = detect_sync_event_capabilities()
850
+ if not caps.get("available"):
851
+ return {
852
+ "success": False,
853
+ "status": "missing_dependency",
854
+ "no_auto_install": True,
855
+ "capabilities": caps,
856
+ "install_guidance": sync_event_install_guidance(caps),
857
+ }
858
+
859
+ file_results = []
860
+ warnings = []
861
+ for record in records:
862
+ result = detect_sync_events_for_file(record, params, caps)
863
+ if result.get("warnings"):
864
+ warnings.extend(result["warnings"])
865
+ file_results.append(result)
866
+
867
+ alignment = _alignment_suggestions(file_results, params)
868
+ return {
869
+ "success": any(result.get("success") for result in file_results),
870
+ "source_safe": True,
871
+ "writes_media": False,
872
+ "no_auto_install": True,
873
+ "capabilities": caps,
874
+ "files": file_results,
875
+ "alignment": alignment,
876
+ "marker_write": {
877
+ "available": any(
878
+ suggestion.get("eligible")
879
+ for result in file_results
880
+ for suggestion in (result.get("marker_suggestions") or [])
881
+ ),
882
+ "requires_confirmation": True,
883
+ "action": "media_analysis(action='add_sync_event_markers')",
884
+ "note": "Ask the user before adding Resolve markers from detected sync events.",
885
+ },
886
+ "warnings": warnings,
887
+ }