利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(bgm): enforce Sonilo resource limits (#1374)
XiaoChenaf4db5afe791
diff --git a/app/services/sonilo.py b/app/services/sonilo.pyindex 0fe9d3ca..75221ffa 100644--- a/app/services/sonilo.py+++ b/app/services/sonilo.py@@ -23,6 +23,7 @@ MAX_VIDEO_DURATION_SECONDS = 360MAX_PROMPT_LENGTH = 2000MAX_PROXY_BYTES = 300 * 1024 * 1024MAX_GENERATED_AUDIO_BYTES = 30 * 1024 * 1024+MAX_ERROR_BODY_BYTES = 500VIDEO_TO_MUSIC_SERVICE_ID = "video_to_music"@@ -74,8 +75,29 @@ def _normalize_service_id(service_id: str) -> str:def _safe_response_error(response: requests.Response) -> str:- """仅保留简短响应信息,既方便定位又避免异常页面污染日志。"""- body = (response.text or "").strip().replace("\n", " ")[:500]+ """只读取有限的第三方错误正文,避免异常响应耗尽内存或污染任务日志。"""+ try:+ body_bytes = next(+ response.iter_content(chunk_size=MAX_ERROR_BODY_BYTES),+ b"",+ )+ except requests.RequestException:+ body_bytes = b""+ if isinstance(body_bytes, bytes):+ try:+ body = body_bytes.decode(+ response.encoding or "utf-8",+ errors="replace",+ )+ except LookupError:+ # response.encoding 直接取自上游声明的 charset,未知取值(例如+ # charset=unknown-charset)会让 codecs 抛 LookupError,而 errors+ # 只影响 UnicodeDecodeError。旧的 response.text 会在内部退回+ # UTF-8,这里保持同样行为,避免该异常绕过调用方的 Sonilo 降级链路。+ body = body_bytes.decode("utf-8", errors="replace")+ else:+ body = str(body_bytes)+ body = body.strip().replace("\n", " ")[:MAX_ERROR_BODY_BYTES]return body or response.reason or "request failed"@@ -89,22 +111,25 @@ def test_connection() -> dict[str, Any]:if not api_key:raise SoniloError("Sonilo API key is required")try:- response = requests.get(+ with requests.get(f"{_base_url()}{SERVICES_PATH}",headers={"Authorization": f"Bearer {api_key}"},timeout=(15, 30),- )+ stream=True,+ ) as response:+ if not response.ok:+ raise SoniloError(+ f"Sonilo connection failed ({response.status_code}): "+ f"{_safe_response_error(response)}"+ )+ try:+ payload = response.json()+ except ValueError as exc:+ raise SoniloError(+ "Sonilo returned an invalid service response"+ ) from excexcept requests.RequestException as exc:raise SoniloError(f"failed to connect to Sonilo: {exc}") from exc- if not response.ok:- raise SoniloError(- f"Sonilo connection failed ({response.status_code}): "- f"{_safe_response_error(response)}"- )- try:- payload = response.json()- except ValueError as exc:- raise SoniloError("Sonilo returned an invalid service response") from excif not isinstance(payload, dict):raise SoniloError("Sonilo returned an unexpected service response")available_services = payload.get("available_services")@@ -170,6 +195,8 @@ def _create_video_proxy(video_path: str) -> str:"yuv420p","-movflags","+faststart",+ "-fs",+ str(MAX_PROXY_BYTES),proxy_path,]try:diff --git a/test/services/test_sonilo.py b/test/services/test_sonilo.pyindex 4e2b7312..7442673d 100644--- a/test/services/test_sonilo.py+++ b/test/services/test_sonilo.py@@ -3,9 +3,13 @@ import jsonimport osimport tempfileimport unittest+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom pathlib import Path+from threading import Threadfrom unittest.mock import patch+import requests+from app.services import sonilo@@ -25,8 +29,15 @@ class _StreamingResponse:self.ok = 200 <= status_code < 300self.reason = "OK" if self.ok else "Request failed"self.text = "" if self.ok else "request failed"+ self.encoding = "utf-8"self.payload = payload if payload is not None else {"services": []}self.iter_error = iter_error+ self.closed = False++ def iter_content(self, chunk_size):+ if self.iter_error:+ raise self.iter_error+ return iter(())def iter_lines(self):if self.iter_error:@@ -40,6 +51,7 @@ class _StreamingResponse:return selfdef __exit__(self, *_args):+ self.closed = Truereturn False@@ -80,6 +92,90 @@ class TestSoniloService(unittest.TestCase):):self.assertEqual(sonilo._request_timeout(), expected)+ def test_safe_response_error_reads_only_one_bounded_chunk(self):+ class OversizedErrorResponse:+ reason = "Request failed"+ encoding = "utf-8"++ @property+ def text(self):+ raise AssertionError("response.text must not be materialized")++ def iter_content(self, chunk_size):+ self.requested_chunk_size = chunk_size+ yield b"x" * chunk_size+ raise AssertionError("error body must not be read further")++ response = OversizedErrorResponse()++ detail = sonilo._safe_response_error(response)++ self.assertEqual(+ response.requested_chunk_size,+ sonilo.MAX_ERROR_BODY_BYTES,+ )+ self.assertEqual(detail, "x" * sonilo.MAX_ERROR_BODY_BYTES)++ def test_request_bgm_reports_unknown_charset_body_as_sonilo_error(self):+ """上游错误正文声明未知 charset 时,取证降级不能变成 LookupError。"""+ received = []++ class Receiver(BaseHTTPRequestHandler):+ def do_POST(self):+ body = b"unknown-charset upstream failure"+ self.send_response(500)+ self.send_header("Content-Type", "text/plain; charset=unknown-charset")+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def log_message(self, *_args):+ # 本机接收器不打印请求头,测试输出无需包含认证信息。+ pass++ server = ThreadingHTTPServer(("127.0.0.1", 0), Receiver)+ worker = Thread(target=server.serve_forever, daemon=True)+ worker.start()+ try:+ # 只访问环回地址,绕过开发机代理,避免把测试请求交给外部代理服务。+ with requests.Session() as session:+ session.trust_env = False++ def post(*args, **kwargs):+ response = session.post(*args, **kwargs)+ received.append(response)+ return response++ with tempfile.TemporaryDirectory() as temp_dir:+ video_path = Path(temp_dir) / "proxy.mp4"+ output_path = Path(temp_dir) / "music.m4a"+ video_path.write_bytes(b"video")+ with (+ patch.object(+ sonilo.config,+ "app",+ {+ "sonilo_api_key": "test-key",+ "sonilo_base_url": (+ f"http://127.0.0.1:{server.server_port}"+ ),+ },+ ),+ patch.object(sonilo.requests, "post", side_effect=post),+ ):+ with self.assertRaisesRegex(+ sonilo.SoniloError,+ "unknown-charset upstream failure",+ ):+ sonilo._request_bgm(str(video_path), str(output_path), "")++ self.assertEqual(list(Path(temp_dir).glob(".sonilo-audio-*")), [])+ self.assertTrue(received[0].raw.closed)+ finally:+ server.shutdown()+ server.server_close()+ worker.join(timeout=5)+def test_connection_uses_non_billing_services_endpoint(self):response = _StreamingResponse(payload={"available_services": ["video_to_music"]}@@ -95,6 +191,8 @@ class TestSoniloService(unittest.TestCase):self.assertEqual(request.call_args.kwargs["headers"]["Authorization"], "Bearer test-key")+ self.assertTrue(request.call_args.kwargs["stream"])+ self.assertTrue(response.closed)def test_connection_accepts_documented_hyphenated_service_id(self):"""公开文档的连字符写法必须归一化为项目内部服务标识。"""@@ -187,6 +285,10 @@ class TestSoniloService(unittest.TestCase):command = run.call_args.args[0]self.assertEqual(command[0], "test-ffmpeg")self.assertIn("-an", command)+ self.assertEqual(+ command[command.index("-fs") + 1],+ str(sonilo.MAX_PROXY_BYTES),+ )self.assertIn("force_original_aspect_ratio=decrease", command[command.index("-vf") + 1])self.assertEqual(Path(proxy_path).read_bytes(), b"proxy-video")Path(proxy_path).unlink()