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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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""
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 + <video> seek + GSAP seek helper
        init_js = """
        window.__offlineSeek = async function(t) {
          var clips = document.querySelectorAll('.clip[data-start]');
          var seekPromises = [];
          for (var i = 0; i < clips.length; i++) {
            var el = clips[i];
            var st = parseFloat(el.getAttribute('data-start') || '0');
            var dur = parseFloat(el.getAttribute('data-duration') || '999999');
            var active = (t >= st && t < st + dur);
            el.style.visibility = active ? 'visible' : 'hidden';
            if (active && el.tagName === 'VIDEO') {
              var localT = Math.max(0, t - st);
              if (el.duration && isFinite(el.duration) && el.duration > 0) {
                localT = localT % el.duration;
              }
              if (Math.abs((el.currentTime || 0) - localT) > 0.02) {
                seekPromises.push(new Promise(function(resolve) {
                  var done = false;
                  var finish = function() { if (!done) { done = true; resolve(); } };
                  el.addEventListener('seeked', finish, { once: true });
                  setTimeout(finish, 120);
                  try { el.currentTime = localT; } catch (e) { finish(); }
                }));
              }
            }
          }
          if (window.__timelines && window.__timelines['main']) {
            window.__timelines['main'].seek(t, false);
          }
          if (seekPromises.length) {
            await Promise.all(seekPromises);
          }
          return true;
        };
        """
        cdp.call("Runtime.evaluate", {"expression": init_js})

        total_frames = max(1, int(round(spec["total_duration"] * fps)))
        output_mp4.parent.mkdir(parents=True, exist_ok=True)

        ff_cmd = [
            ffmpeg_bin, "-y",
            "-f", "image2pipe",
            "-vcodec", "mjpeg",
            "-framerate", str(fps),
            "-i", "-",
            "-i", str(master_wav),
            "-c:v", "libx264",
            "-preset", "fast",
            "-crf", "17",
            "-pix_fmt", "yuv420p",
            "-c:a", "aac",
            "-b:a", "192k",
            "-shortest",
            str(output_mp4),
        ]
        ff_proc = subprocess.Popen(ff_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        assert ff_proc.stdin is not None

        for frame_idx in range(total_frames):
            t_sec = frame_idx / float(fps)
            cdp.call("Runtime.evaluate", {"expression": f"window.__offlineSeek({t_sec:.5f})", "awaitPromise": True})
            shot = cdp.call("Page.captureScreenshot", {"format": "jpeg", "quality": 94})
            ff_proc.stdin.write(base64.b64decode(shot["data"]))
            if frame_idx % 60 == 0 or frame_idx == total_frames - 1:
                pct = int((frame_idx + 1) * 100 / total_frames)
                print(f"\r      [Offline Renderer] {pct:3d}% ({frame_idx + 1}/{total_frames} frames)", end="", flush=True)

        print()
        ff_proc.stdin.close()
        ff_proc.stdin = None
        _, stderr_bytes = ff_proc.communicate()
        if ff_proc.returncode != 0:
            raise RuntimeError(f"FFmpeg encode failed: {stderr_bytes.decode('utf-8', errors='ignore')[:600]}")

    finally:
        if cdp:
            cdp.close()
        browser_proc.terminate()
        httpd.shutdown()
        shutil.rmtree(tmp_profile, ignore_errors=True)

    return output_mp4