""" Zero-NPM Air-Gapped Offline HTML-to-MP4 Renderer (`offline_renderer.py`) Drives any locally installed Chromium / Google Chrome / Microsoft Edge via raw Chrome DevTools Protocol (CDP) over localhost WebSocket (Python stdlib only — zero Playwright / zero Node.js required!) + sample-accurate NumPy/FFmpeg audio mixing for Voiceover + UI SFX + BGM. """ from __future__ import annotations import base64 import http.server import json import os import platform import shutil import socket import struct import subprocess import tempfile import threading import time import urllib.request import wave from pathlib import Path from typing import Any import numpy as np def _is_working_browser(path_str: str) -> bool: if not path_str or not Path(path_str).exists(): return False try: res = subprocess.run([path_str, "--version"], capture_output=True, timeout=4) return res.returncode == 0 except Exception: return False def _find_local_browser() -> str: env_b = os.environ.get("CHROME_BIN") if env_b and _is_working_browser(env_b): return env_b # Check HyperFrames cached headless shell first if present hf_cache = Path.home() / ".cache" / "hyperframes" / "chrome" if hf_cache.exists(): for exe_name in ("chrome-headless-shell", "chrome", "chrome.exe"): for m in hf_cache.rglob(exe_name): if _is_working_browser(str(m)): return str(m) candidates = [] sys_name = platform.system().lower() if sys_name == "darwin": candidates = [ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", "/Applications/Chromium.app/Contents/MacOS/Chromium", ] elif sys_name == "windows": pf = os.environ.get("PROGRAMFILES", r"C:\Program Files") pfx86 = os.environ.get("PROGRAMFILES(X86)", r"C:\Program Files (x86)") local_app = os.environ.get("LOCALAPPDATA", "") candidates = [ rf"{pf}\Google\Chrome\Application\chrome.exe", rf"{pfx86}\Google\Chrome\Application\chrome.exe", rf"{local_app}\Google\Chrome\Application\chrome.exe", rf"{pf}\Microsoft\Edge\Application\msedge.exe", rf"{pfx86}\Microsoft\Edge\Application\msedge.exe", ] else: candidates = ["/usr/bin/google-chrome", "/usr/bin/chromium", "/usr/bin/chromium-browser"] for cmd in ("google-chrome", "chromium", "chromium-browser", "msedge", "brave-browser"): found = shutil.which(cmd) if found: candidates.append(found) for c in candidates: if _is_working_browser(c): return c raise RuntimeError("No working local Chrome, Edge, or Chromium browser found for offline rendering.") class _MiniCDPClient: """Minimal stdlib WebSocket client for localhost Chrome DevTools Protocol.""" def __init__(self, ws_url: str): # ws://127.0.0.1:PORT/devtools/page/ID rest = ws_url.replace("ws://", "") host_port, path = rest.split("/", 1) host, port_s = host_port.split(":") self.sock = socket.create_connection((host, int(port_s)), timeout=30) key = base64.b64encode(os.urandom(16)).decode("ascii") handshake = ( f"GET /{path} HTTP/1.1\r\n" f"Host: {host_port}\r\n" "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" f"Sec-WebSocket-Key: {key}\r\n" "Sec-WebSocket-Version: 13\r\n\r\n" ) self.sock.sendall(handshake.encode("ascii")) buf = b"" while b"\r\n\r\n" not in buf: buf += self.sock.recv(4096) self._msg_id = 0 def _send_frame(self, text: str) -> None: payload = text.encode("utf-8") mask_key = os.urandom(4) header = bytearray([0x81]) # FIN + text frame length = len(payload) if length < 126: header.append(0x80 | length) elif length < 65536: header.append(0x80 | 126) header.extend(struct.pack("!H", length)) else: header.append(0x80 | 127) header.extend(struct.pack("!Q", length)) header.extend(mask_key) masked = bytes(b ^ mask_key[i % 4] for i, b in enumerate(payload)) self.sock.sendall(bytes(header) + masked) def _recv_exact(self, n: int) -> bytes: chunks = [] rem = n while rem > 0: chunk = self.sock.recv(rem) if not chunk: raise EOFError("CDP socket closed") chunks.append(chunk) rem -= len(chunk) return b"".join(chunks) def _recv_frame(self) -> str: while True: b1, b2 = self._recv_exact(2) opcode = b1 & 0x0F masked = bool(b2 & 0x80) length = b2 & 0x7F if length == 126: length = struct.unpack("!H", self._recv_exact(2))[0] elif length == 127: length = struct.unpack("!Q", self._recv_exact(8))[0] mask_key = self._recv_exact(4) if masked else b"" data = self._recv_exact(length) if masked: data = bytes(b ^ mask_key[i % 4] for i, b in enumerate(data)) if opcode == 0x8: raise EOFError("CDP WebSocket closed") if opcode == 0x9: # Ping -> Pong pong = bytearray([0x8A, 0x80]) + os.urandom(4) self.sock.sendall(bytes(pong)) continue if opcode == 0x1: return data.decode("utf-8") def call(self, method: str, params: dict[str, Any] | None = None) -> dict[str, Any]: self._msg_id += 1 mid = self._msg_id self._send_frame(json.dumps({"id": mid, "method": method, "params": params or {}})) while True: msg = json.loads(self._recv_frame()) if msg.get("id") == mid: if "error" in msg: raise RuntimeError(f"CDP error on {method}: {msg['error']}") return msg.get("result", {}) def close(self) -> None: try: self.sock.close() except Exception: pass def _decode_to_pcm48k(ffmpeg_bin: str, src_path: Path) -> np.ndarray: cmd = [ ffmpeg_bin, "-v", "error", "-i", str(src_path), "-f", "s16le", "-acodec", "pcm_s16le", "-ar", "48000", "-ac", "2", "-", ] res = subprocess.run(cmd, capture_output=True, check=True) arr = np.frombuffer(res.stdout, dtype=np.int16).astype(np.float32) / 32768.0 return arr.reshape(-1, 2) def mix_offline_master_audio(ffmpeg_bin: str, spec: dict[str, Any], project_dir: Path, out_wav: Path) -> Path: """Sample-accurate 48kHz stereo master mix of all Voiceover + SFX + BGM tracks.""" sr = 48000 total_samples = int(round(spec["total_duration"] * sr)) + sr // 4 master = np.zeros((total_samples, 2), dtype=np.float32) def place_clip(rel_src: str, start_sec: float, gain: float) -> None: if not rel_src: return p = project_dir / rel_src if not p.is_file(): return pcm = _decode_to_pcm48k(ffmpeg_bin, p) * gain s0 = max(0, int(round(start_sec * sr))) s1 = min(total_samples, s0 + len(pcm)) if s1 > s0: master[s0:s1] += pcm[: s1 - s0] if spec.get("bgm"): place_clip(spec["bgm"]["src"], float(spec["bgm"]["start"]), float(spec["bgm"]["volume"])) for slide in spec.get("slides", []): for beat in slide.get("beats", []): place_clip(beat["src"], float(beat["start"]), 1.0) for ev in spec.get("sfx_events", []): place_clip(ev["src"], float(ev["start"]), float(ev.get("volume", 0.3))) # Soft limiter ceiling at -1.0 dBFS peak = float(np.max(np.abs(master))) if peak > 0.89: master *= 0.89 / peak trim_len = int(round(spec["total_duration"] * sr)) pcm16 = np.clip(master[:trim_len] * 32767.0, -32768, 32767).astype(np.int16) out_wav.parent.mkdir(parents=True, exist_ok=True) with wave.open(str(out_wav), "wb") as wf: wf.setnchannels(2) wf.setsampwidth(2) wf.setframerate(sr) wf.writeframes(pcm16.tobytes()) return out_wav def render_offline_via_local_browser( project_dir: Path, spec: dict[str, Any], output_mp4: Path, fps: int = 30, ) -> Path: """Renders `project_dir/index.html` + audio stems to `output_mp4` with zero npm/internet.""" ffmpeg_bin = shutil.which("ffmpeg") or "/opt/homebrew/bin/ffmpeg" browser_bin = _find_local_browser() # 1. Serve `project_dir` on an ephemeral localhost port class QuietHandler(http.server.SimpleHTTPRequestHandler): def __init__(self, *args: Any, **kwargs: Any): super().__init__(*args, directory=str(project_dir), **kwargs) def log_message(self, format: str, *args: Any) -> None: pass httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), QuietHandler) http_port = httpd.server_address[1] srv_thread = threading.Thread(target=httpd.serve_forever, daemon=True) srv_thread.start() with socket.socket() as s: s.bind(("127.0.0.1", 0)) cdp_port = s.getsockname()[1] tmp_profile = Path(tempfile.mkdtemp(prefix="deck2video_cdp_")) master_wav = tmp_profile / "master_mix.wav" mix_offline_master_audio(ffmpeg_bin, spec, project_dir, master_wav) is_headless_shell = "headless-shell" in Path(browser_bin).name.lower() browser_args = [browser_bin] if not is_headless_shell: browser_args.append("--headless=new") browser_args.extend([ "--disable-gpu-vsync", "--hide-scrollbars", "--mute-audio", "--no-first-run", "--no-default-browser-check", "--no-proxy-server", # Zero-Egress Chromium Network Lockdown: block ALL external DNS & background telemetry/updates! "--host-resolver-rules=MAP * ~NOTFOUND , EXCLUDE 127.0.0.1, EXCLUDE localhost, EXCLUDE ::1", "--disable-background-networking", "--disable-client-side-phishing-detection", "--disable-component-update", "--disable-default-apps", "--disable-domain-reliability", "--disable-features=OptimizationHints,MediaRouter,DialMediaRouteProvider,Translate,AutofillServerCommunication,CertificateTransparencyComponentUpdater", "--disable-sync", "--metrics-recording-only", "--safebrowsing-disable-auto-update", "--no-pings", "--autoplay-policy=no-user-gesture-required", "--window-size=1920,1080", f"--user-data-dir={tmp_profile / 'profile'}", f"--remote-debugging-port={cdp_port}", "about:blank", ]) browser_proc = subprocess.Popen( browser_args, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, ) no_proxy_opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) cdp: _MiniCDPClient | None = None try: ws_url = "" for _ in range(60): try: with no_proxy_opener.open(f"http://127.0.0.1:{cdp_port}/json", timeout=1) as resp: targets = json.loads(resp.read().decode("utf-8")) for tgt in targets: if tgt.get("type") == "page" and "webSocketDebuggerUrl" in tgt: ws_url = tgt["webSocketDebuggerUrl"] break if ws_url: break except Exception: time.sleep(0.15) if not ws_url: raise RuntimeError("Failed to attach to headless browser CDP endpoint.") cdp = _MiniCDPClient(ws_url) cdp.call("Page.enable") cdp.call("Runtime.enable") cdp.call( "Emulation.setDeviceMetricsOverride", {"width": 1920, "height": 1080, "deviceScaleFactor": 1, "mobile": False}, ) cdp.call( "Page.addScriptToEvaluateOnNewDocument", {"source": "window.__timelines = window.__timelines || {}; window.__offlineCapture = true;"}, ) cdp.call("Page.navigate", {"url": f"http://127.0.0.1:{http_port}/index.html"}) # Poll until fonts and main GSAP timeline are both initialized tl_ready = False for _ in range(50): ev = cdp.call( "Runtime.evaluate", { "expression": ( "Boolean(document.readyState === 'complete' && window.__timelines && window.__timelines['main'])" ), "returnByValue": True, }, ) if ev.get("result", {}).get("value") is True: tl_ready = True break time.sleep(0.1) if not tl_ready: err_probe = cdp.call( "Runtime.evaluate", {"expression": "String(window.__lastScriptError || 'timeline not registered')", "returnByValue": True}, ) raise RuntimeError(f"GSAP main timeline failed to initialize in offline browser: {err_probe}") cdp.call("Runtime.evaluate", {"expression": "document.fonts.ready", "awaitPromise": True}) # Inject async clip visibility +