Skip to content

Reverse-engineer any video, shot by shot: filmed, AI-generated or coded, and how to remake it. Breakdowns in the open VIDEO.md format. 逆向一切视频,逐个镜头看是拍的、AI 生成的还是代码做的,再告诉你怎么复刻;拆解结果按 VIDEO.md 格式输出。

Send a User-Agent on VLM requests

Hugodfbe376fb18c
diff --git a/rea_video/vlm.py b/rea_video/vlm.py
index 3933ec5..ab3058b 100644
--- a/rea_video/vlm.py
+++ b/rea_video/vlm.py
@@ -22,6 +22,7 @@ import urllib.request
from dataclasses import dataclass
from pathlib import Path
+from . import __version__
from .classify import LABELS
from .util import LOG
@@ -137,8 +138,10 @@ class VLMClient:
def _post(self, model: str, content: list[dict], max_tokens: int) -> str:
body = json.dumps({"model": model, "messages": [{"role": "user", "content": content}],
"max_tokens": max_tokens, "temperature": 0.2}).encode()
+ # urllib's default "Python-urllib/x.y" gets a 403 from some gateways, which looks like a bad key
req = urllib.request.Request(self.cfg.base_url + "/chat/completions", data=body, method="POST",
- headers={"Content-Type": "application/json", "Authorization": f"Bearer {self.cfg.api_key}"})
+ headers={"Content-Type": "application/json", "Authorization": f"Bearer {self.cfg.api_key}",
+ "User-Agent": f"rea-video/{__version__}"})
with urllib.request.urlopen(req, timeout=self.cfg.timeout) as resp:
data = json.loads(resp.read().decode())
try:
diff --git a/tests/test_vlm_mock.py b/tests/test_vlm_mock.py
index c5e92c9..efbf3bb 100644
--- a/tests/test_vlm_mock.py
+++ b/tests/test_vlm_mock.py
@@ -19,10 +19,12 @@ CANNED = {
class Handler(BaseHTTPRequestHandler):
seen = []
+ user_agents = []
def do_POST(self): # noqa: N802
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
Handler.seen.append(body)
+ Handler.user_agents.append(self.headers.get("User-Agent", ""))
text = body["messages"][0]["content"][-1]["text"]
payload = CANNED if "label_probs" in text else {"summary": "测试视频", "format": "知识讲解", "segments": []}
resp = {"choices": [{"message": {"content": "```json\n" + json.dumps(payload, ensure_ascii=False) + "\n```"}}]}
@@ -50,6 +52,7 @@ def fake_server(monkeypatch):
def test_vlm_fusion_end_to_end(media, tmp_path, fake_server):
b = decompose(media["code"], tmp_path / "o", use_vlm=True, ocr=False, workers=1, stems=False)
+ assert fake_server.user_agents and all(ua.startswith("rea-video/") for ua in fake_server.user_agents)
assert b["analysis"]["vlm"]["used"] and not b["analysis"]["vlm"]["errors"]
s = b["shots"][0]
assert s["base"]["decided_by"] == "fused" and s["base"]["label"] == "code_render"