examples: follow the stream's format; wall guard checks URL, page text, and end of recording

This commit is contained in:
Hermenix committed 2026-09-30 19:40:39 +00:00
1 parent f667654f01
commit 1664ccf710
2 files changed
+60 -7

No files matched your search

+23
View File
@@ -45,6 +45,29 @@ accurate.
the encoder uses the *measured* rate, so the video plays at real speed either
way, and decimating only makes it choppier.
### It films a desktop, not a browser
The browser is **windowed by default** (1040×560, floating, centred), so the
session's wallpaper, gaps and window borders are all in shot — that is the
difference between "a screen recording of a browser" and "here is the machine I
look at". `--fullscreen` gives the old full-bleed kiosk window.
The desktop furniture itself lives in `/etc/nixos/hyprland-stream.nix` (wallpaper
via `swaybg`, `gaps_out = 36`, a catch-all float window rule), not in this repo:
it is this machine's taste, not part of the kit.
Two notes for anyone editing that file:
- Window rules are **v3** syntax here (`windowrule = match:class .*, float on`).
The v2 spelling (`float, class:^(chromium)$`) parses as an unknown field and
Hyprland draws the error *across the screen*, which lands in the recording.
Always check an edit with `Hyprland --verify-config --config <file>` — it does
not need a display, so it costs seconds instead of a rebuild.
- `hyprctl` **cannot connect** to this session: the compositor names its IPC
socket `.socket.so`/`.socket.s` instead of `.socket.sock`, so nothing using the
documented path works. Do not plan on `hyprctl` for window control — measure
what you need from the frames instead.
### Posting the result to a chat
Xiph/Blossom media stores reject files that carry metadata. `encode()` in the
+37 -7
View File
@@ -224,6 +224,23 @@ class DevTools:
self._ws.close()
def is_walled(devtools: DevTools) -> bool:
"""Google's bot wall, however it presents itself.
Both checks are needed. The URL is the obvious tell (`/sorry/index?...`),
but the redirect can arrive *after* the results page has already painted —
so a guard that only runs right after the search passes and then films a
CAPTCHA for sixteen seconds. Hence: check the URL, check the page text, and
call this again once the recording has finished.
"""
url = devtools.evaluate("location.href") or ""
if "/sorry/" in url:
return True
text = devtools.evaluate(
"(() => { const b = document.body; return b ? b.innerText.slice(0, 600) : ''; })()") or ""
return "unusual traffic" in text.lower()
def type_query(devtools: DevTools, query: str, per_char: float = 0.11) -> None:
"""Focus the search box and type into it, one character at a time.
@@ -291,6 +308,9 @@ class Recorder(threading.Thread):
self.out_dir = out_dir
self.target_fps = target_fps
self.hello: dict = {}
# Frame extension follows the Hello, not an assumption: the stream can
# legitimately be JPEG, and the caller must not have to know which.
self.ext = "png"
self.count = 0
self.error: str | None = None
# NOT `_stop` / `_started`: `threading.Thread` owns both names. It sets
@@ -316,6 +336,8 @@ class Recorder(threading.Thread):
# CONTRACT: the Hello can take a moment (it is written on the
# first successful capture), so give it the socket timeout.
self.hello = protocol.read_hello(stream)
self.ext = {"image/png": "png", "image/jpeg": "jpg"}.get(
str(self.hello.get("format", "image/png")), "png")
server_fps = float(self.hello.get("fps") or 30)
step = max(1, round(server_fps / self.target_fps))
self._ready.set()
@@ -328,7 +350,7 @@ class Recorder(threading.Thread):
if (index - 1) % step:
continue # decimate to the target rate
self.count += 1
(self.out_dir / f"frame-{self.count:05d}.png").write_bytes(payload)
(self.out_dir / f"frame-{self.count:05d}.{self.ext}").write_bytes(payload)
except (OSError, EOFError, ValueError) as exc:
self.error = str(exc)
finally:
@@ -340,11 +362,11 @@ class Recorder(threading.Thread):
self.join(timeout=10)
def encode(frames: pathlib.Path, fps: float, out: pathlib.Path) -> None:
def encode(frames: pathlib.Path, fps: float, out: pathlib.Path, ext: str = "png") -> None:
argv = [
"ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-framerate", f"{fps:.4f}",
"-i", str(frames / "frame-%05d.png"),
"-i", str(frames / f"frame-%05d.{ext}"),
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
# yuv420p + faststart: what every player and every chat client wants.
"-pix_fmt", "yuv420p", "-movflags", "+faststart",
@@ -431,9 +453,12 @@ def main() -> int:
type_query(devtools, args.query)
landed = submit_and_wait(devtools)
log(f"landed on {landed}")
if "/sorry/" in landed:
# Google's bot wall. Filming it would produce a video that looks
# like a working demo and is not one, so stop instead.
# The wall can arrive as a redirect *after* the results paint, so give
# it a beat and then look again rather than trusting the first URL.
time.sleep(1.5)
if is_walled(devtools):
# A video that looks like a working demo and is not one is worse
# than no video.
fail(
"Google served its 'unusual traffic' wall instead of results "
"(rate-limited IP, not a broken browser). Wait a few minutes and "
@@ -446,6 +471,11 @@ def main() -> int:
remaining = args.seconds - (time.monotonic() - began)
if remaining > 0:
time.sleep(remaining)
# Final gate, after the whole recording: if the page ended up on the
# wall at any point, nothing is encoded.
if is_walled(devtools):
fail("Google walled the search during recording — no video written")
finally:
if recorder is not None:
recorder.stop()
@@ -462,7 +492,7 @@ def main() -> int:
fps = recorder.count / args.seconds
log(f"captured {recorder.count} frames ({fps:.2f} fps effective)")
encode(frames, fps, out)
encode(frames, fps, out, recorder.ext)
size = out.stat().st_size
log(f"wrote {out} ({size / 1024:.0f} KiB, {recorder.count} frames)")