Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

feat(bgm): add Sonilo video-matched music

harry0703281b5eb3cc8b
diff --git a/app/models/schema.py b/app/models/schema.py
index 08da480b..97cfa463 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -97,6 +97,7 @@ class VideoParams(BaseModel):
bgm_type: Optional[str] = "random"
bgm_file: Optional[str] = ""
bgm_volume: Optional[float] = 0.2
+ sonilo_bgm_prompt: str = Field(default="", max_length=2000)
subtitle_enabled: Optional[bool] = True
subtitle_position: Optional[str] = config.ui.get("subtitle_position", "bottom") # top, bottom, center, custom
diff --git a/app/services/bgm.py b/app/services/bgm.py
index c7fb669b..37c5a846 100644
--- a/app/services/bgm.py
+++ b/app/services/bgm.py
@@ -1,3 +1,4 @@
+import math
import os
import subprocess
import tempfile
@@ -44,6 +45,23 @@ class BgmServiceError(RuntimeError):
"""表示 FFmpeg 或文件系统不可用等服务端执行故障。"""
+def should_use_bgm(bgm_type: str | None, bgm_volume: float | None) -> bool:
+ """
+ 统一判断当前任务是否需要处理任何背景音乐。
+
+ 该规则与具体来源无关:没有选择来源、音量不合法或音量不大于 0 时,随机、
+ 自定义、Sonilo 以及未来新增的提供商都必须跳过文件解析、外部生成和最终混音。
+ 放在通用 BGM 服务中可以避免每增加一个提供商就复制一套 0 音量判断。
+ """
+ if not str(bgm_type or "").strip():
+ return False
+ try:
+ normalized_volume = float(bgm_volume or 0)
+ except (TypeError, ValueError):
+ return False
+ return math.isfinite(normalized_volume) and normalized_volume > 0
+
+
def uploaded_bgm_dir(create: bool = True) -> str:
"""
返回用户背景音乐的持久化目录。
@@ -99,7 +117,7 @@ def sanitize_upload_filename(filename: str) -> str:
return safe_name
-def _validate_audio(file_path: str) -> None:
+def _validate_audio(file_path: str, timeout_seconds: int = 30) -> None:
"""
仅使用项目当前配置的 FFmpeg 验证文件包含可完整解码的音频流。
@@ -125,7 +143,7 @@ def _validate_audio(file_path: str) -> None:
"-",
],
capture_output=True,
- timeout=30,
+ timeout=timeout_seconds,
check=False,
)
except subprocess.TimeoutExpired as exc:
@@ -136,6 +154,18 @@ def _validate_audio(file_path: str) -> None:
raise BgmUploadError("uploaded file must contain a decodable audio stream")
+def validate_audio_file(file_path: str, timeout_seconds: int = 120) -> None:
+ """
+ 校验磁盘上的音频文件可由项目 FFmpeg 完整解码。
+
+ 上传预检通常只需 30 秒;Sonilo 生成的配乐最长可达 6 分钟,因此对外提供
+ 可调整超时的复用入口。服务只依赖 FFmpeg,不要求系统额外安装 FFprobe。
+ """
+ if not os.path.isfile(file_path) or os.path.getsize(file_path) <= 0:
+ raise BgmUploadError("background music file is empty or missing")
+ _validate_audio(file_path, timeout_seconds=timeout_seconds)
+
+
def _stage_bgm_upload(filename: str, source: BinaryIO) -> tuple[str, str, int]:
"""
将上传流写入同目录临时文件,并返回安全文件名、临时路径和字节数。
diff --git a/app/services/sonilo.py b/app/services/sonilo.py
new file mode 100644
index 00000000..b2bb9df9
--- /dev/null
+++ b/app/services/sonilo.py
@@ -0,0 +1,357 @@
+import base64
+import binascii
+import json
+import math
+import os
+import subprocess
+import tempfile
+from pathlib import Path
+from typing import Any
+
+import requests
+from loguru import logger
+
+from app.config import config
+from app.services import bgm as bgm_service
+from app.utils import utils
+
+
+DEFAULT_BASE_URL = "https://api.sonilo.com"
+VIDEO_TO_MUSIC_PATH = "/v1/video-to-music"
+SERVICES_PATH = "/v1/account/services"
+MAX_VIDEO_DURATION_SECONDS = 360
+MAX_PROMPT_LENGTH = 2000
+MAX_PROXY_BYTES = 300 * 1024 * 1024
+MAX_GENERATED_AUDIO_BYTES = 30 * 1024 * 1024
+VIDEO_TO_MUSIC_SERVICE_ID = "video_to_music"
+
+
+class SoniloError(RuntimeError):
+ """表示 Sonilo 请求、响应协议或生成音频校验失败。"""
+
+
+def get_api_key() -> str:
+ """优先读取 WebUI 保存的配置,未配置时允许使用环境变量。"""
+ configured_key = str(config.app.get("sonilo_api_key", "") or "").strip()
+ return configured_key or os.getenv("SONILO_API_KEY", "").strip()
+
+
+def is_enabled() -> bool:
+ return bool(get_api_key())
+
+
+def _base_url() -> str:
+ return str(
+ config.app.get("sonilo_base_url", DEFAULT_BASE_URL) or DEFAULT_BASE_URL
+ ).rstrip(
+ "/"
+ )
+
+
+def _request_timeout() -> tuple[int, int]:
+ """限制配置值范围,避免无穷大或负数让请求永久挂起或立即失败。"""
+ raw_timeout = config.app.get("sonilo_timeout", 600)
+ try:
+ read_timeout = float(raw_timeout)
+ except (TypeError, ValueError):
+ read_timeout = 600
+ if not math.isfinite(read_timeout) or read_timeout <= 0:
+ read_timeout = 600
+ # Requests 不接受 0 秒读取超时。向上取整既保留小数配置的有效含义,也能
+ # 避免 0.1~0.9 被 int() 截断为 0 后抛出未进入 Sonilo 降级链路的 ValueError。
+ return 15, max(1, math.ceil(min(read_timeout, 1800)))
+
+
+def _normalize_service_id(service_id: str) -> str:
+ """
+ 将 Sonilo 服务标识统一为项目内部使用的下划线格式。
+
+ 2026-07-14 实际接口返回 ``video_to_music``,但同日公开文档示例使用
+ ``video-to-music``。差异仅在单词分隔符,因此在第三方协议边界统一格式,
+ 避免 UI 连接测试因提供方文档与生产响应暂时不一致而误报失败。
+ """
+ return service_id.strip().lower().replace("-", "_")
+
+
+def _safe_response_error(response: requests.Response) -> str:
+ """仅保留简短响应信息,既方便定位又避免异常页面污染日志。"""
+ body = (response.text or "").strip().replace("\n", " ")[:500]
+ return body or response.reason or "request failed"
+
+
+def test_connection() -> dict[str, Any]:
+ """
+ 使用不消耗配乐额度的服务列表接口验证 API Key。
+
+ 返回原始 JSON 便于 UI 展示可用服务,但日志中绝不记录 Key 或请求头。
+ """
+ api_key = get_api_key()
+ if not api_key:
+ raise SoniloError("Sonilo API key is required")
+ try:
+ response = requests.get(
+ f"{_base_url()}{SERVICES_PATH}",
+ headers={"Authorization": f"Bearer {api_key}"},
+ timeout=(15, 30),
+ )
+ except 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 exc
+ if not isinstance(payload, dict):
+ raise SoniloError("Sonilo returned an unexpected service response")
+ available_services = payload.get("available_services")
+ if not isinstance(available_services, list) or not all(
+ isinstance(service_id, str) for service_id in available_services
+ ):
+ raise SoniloError("Sonilo returned an invalid service list")
+ normalized_services = {
+ _normalize_service_id(service_id) for service_id in available_services
+ }
+ if VIDEO_TO_MUSIC_SERVICE_ID not in normalized_services:
+ raise SoniloError("Sonilo video-to-music service is not available for this key")
+ logger.info("Sonilo connection test succeeded")
+ return payload
+
+
+def _remove_file(file_path: str) -> None:
+ """尽力清理 Sonilo 中间文件,不覆盖调用方正在处理的原始异常。"""
+ if not file_path or not os.path.exists(file_path):
+ return
+ try:
+ os.remove(file_path)
+ except OSError as exc:
+ logger.warning(
+ f"failed to remove Sonilo temporary file: path={file_path}, error={exc}"
+ )
+
+
+def _create_video_proxy(video_path: str) -> str:
+ """
+ 生成无音轨、最长边 1280 像素的 H.264 代理视频。
+
+ Sonilo 只需分析画面节奏和内容,上传原始高清成片会增加等待时间和流量,
+ 对生成质量没有实际收益。代理文件放在输入文件同目录,任务结束后统一清理。
+ """
+ descriptor, proxy_path = tempfile.mkstemp(
+ prefix=".sonilo-proxy-",
+ suffix=".mp4",
+ dir=os.path.dirname(os.path.abspath(video_path)),
+ )
+ os.close(descriptor)
+ command = [
+ utils.get_ffmpeg_binary(),
+ "-nostdin",
+ "-v",
+ "error",
+ "-y",
+ "-i",
+ video_path,
+ "-vf",
+ (
+ "scale=w=1280:h=1280:force_original_aspect_ratio=decrease:"
+ "force_divisible_by=2"
+ ),
+ "-an",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "veryfast",
+ "-crf",
+ "30",
+ "-pix_fmt",
+ "yuv420p",
+ "-movflags",
+ "+faststart",
+ proxy_path,
+ ]
+ try:
+ result = subprocess.run(
+ command,
+ capture_output=True,
+ text=True,
+ timeout=600,
+ check=False,
+ )
+ except subprocess.TimeoutExpired as exc:
+ _remove_file(proxy_path)
+ raise SoniloError("Sonilo video proxy generation timed out") from exc
+ except OSError as exc:
+ _remove_file(proxy_path)
+ raise SoniloError("failed to run FFmpeg for Sonilo video proxy") from exc
+ if result.returncode != 0:
+ _remove_file(proxy_path)
+ detail = (result.stderr or "").strip().replace("\n", " ")[-500:]
+ raise SoniloError(f"failed to generate Sonilo video proxy: {detail}")
+ proxy_size = os.path.getsize(proxy_path) if os.path.isfile(proxy_path) else 0
+ if proxy_size <= 0 or proxy_size > MAX_PROXY_BYTES:
+ _remove_file(proxy_path)
+ raise SoniloError("Sonilo video proxy is empty or exceeds the 300 MB limit")
+ logger.info(
+ f"Sonilo video proxy prepared: source={video_path}, size={proxy_size} bytes"
+ )
+ return proxy_path
+
+
+def _parse_event(raw_line: bytes) -> dict[str, Any]:
+ """严格解析单条 NDJSON,禁止静默忽略截断或非对象响应。"""
+ try:
+ event = json.loads(raw_line.decode("utf-8"))
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
+ raise SoniloError("Sonilo returned malformed streaming data") from exc
+ if not isinstance(event, dict) or not isinstance(event.get("type"), str):
+ raise SoniloError("Sonilo returned an invalid streaming event")
+ return event
+
+
+def _stream_audio(response: requests.Response, temp_audio_path: str) -> tuple[int, str]:
+ """
+ 把第一条配乐流按事件顺序写入临时文件,并限制最大体积。
+
+ API 可能同时返回多条候选流;当前产品只需要一条 BGM,所以固定选择
+ stream_index=0。只有收到 complete 事件并通过 FFmpeg 完整解码后才会发布。
+ """
+ total_bytes = 0
+ title = ""
+ completed = False
+ with open(temp_audio_path, "wb") as output:
+ for raw_line in response.iter_lines():
+ if not raw_line:
+ continue
+ event = _parse_event(raw_line)
+ event_type = event["type"]
+ if event_type == "error":
+ message = str(
+ event.get("message") or event.get("error") or "unknown error"
+ )
+ raise SoniloError(f"Sonilo generation failed: {message}")
+ if event_type == "title":
+ title = str(event.get("title") or event.get("data") or "")[:200]
+ continue
+ if event_type == "complete":
+ completed = True
+ break
+ if event_type != "audio_chunk":
+ logger.debug(f"ignoring unsupported Sonilo event: type={event_type}")
+ continue
+
+ stream_index = event.get("stream_index", 0)
+ if stream_index != 0:
+ continue
+ encoded_chunk = event.get("data") or event.get("audio")
+ if not isinstance(encoded_chunk, str) or not encoded_chunk:
+ raise SoniloError("Sonilo returned an empty audio chunk")
+ try:
+ chunk = base64.b64decode(encoded_chunk, validate=True)
+ except (binascii.Error, ValueError) as exc:
+ raise SoniloError("Sonilo returned an invalid audio chunk") from exc
+ if not chunk:
+ raise SoniloError("Sonilo returned an empty audio chunk")
+ total_bytes += len(chunk)
+ if total_bytes > MAX_GENERATED_AUDIO_BYTES:
+ raise SoniloError("Sonilo audio exceeds the 30 MB limit")
+ output.write(chunk)
+ output.flush()
+ os.fsync(output.fileno())
+
+ if not completed:
+ raise SoniloError("Sonilo stream ended before completion")
+ if total_bytes <= 0:
+ raise SoniloError("Sonilo returned no audio data")
+ return total_bytes, title
+
+
+def _request_bgm(video_path: str, output_path: str, prompt: str) -> str:
+ """请求配乐并在完整协议及音频校验通过后原子保存。"""
+ output_dir = os.path.dirname(os.path.abspath(output_path))
+ os.makedirs(output_dir, exist_ok=True)
+ descriptor, temp_audio_path = tempfile.mkstemp(
+ prefix=".sonilo-audio-",
+ suffix=Path(output_path).suffix or ".m4a",
+ dir=output_dir,
+ )
+ os.close(descriptor)
+ try:
+ logger.info(
+ f"requesting Sonilo background music: video={video_path}, "
+ f"prompt_provided={bool(prompt)}"
+ )
+ try:
+ with open(video_path, "rb") as video_file:
+ response = requests.post(
+ f"{_base_url()}{VIDEO_TO_MUSIC_PATH}",
+ headers={"Authorization": f"Bearer {get_api_key()}"},
+ files={"video": (Path(video_path).name, video_file, "video/mp4")},
+ data={"prompt": prompt} if prompt else None,
+ stream=True,
+ timeout=_request_timeout(),
+ )
+ with response:
+ if not response.ok:
+ raise SoniloError(
+ f"Sonilo generation failed ({response.status_code}): "
+ f"{_safe_response_error(response)}"
+ )
+ total_bytes, title = _stream_audio(response, temp_audio_path)
+ except requests.RequestException as exc:
+ # iter_lines() 期间的网络中断同样属于 requests 异常,不能只捕获
+ # 建立连接阶段,否则半条音频可能让任务直接异常退出而无法降级。
+ raise SoniloError(f"failed to request Sonilo music: {exc}") from exc
+
+ try:
+ bgm_service.validate_audio_file(temp_audio_path, timeout_seconds=120)
+ except (bgm_service.BgmUploadError, bgm_service.BgmServiceError) as exc:
+ raise SoniloError("Sonilo returned audio that FFmpeg cannot decode") from exc
+ os.replace(temp_audio_path, output_path)
+ temp_audio_path = ""
+ logger.info(
+ f"Sonilo background music generated: output={output_path}, "
+ f"size={total_bytes} bytes, title={title or '-'}"
+ )
+ return output_path
+ finally:
+ _remove_file(temp_audio_path)
+
+
+def generate_bgm(
+ video_path: str,
+ output_path: str,
+ video_duration: float,
+ prompt: str = "",
+) -> str:
+ """为一条已拼接视频生成时长匹配的 Sonilo 背景音乐。"""
+ if not get_api_key():
+ raise SoniloError("Sonilo API key is required")
+ if not os.path.isfile(video_path):
+ raise SoniloError("Sonilo input video does not exist")
+ try:
+ duration = float(video_duration)
+ except (TypeError, ValueError) as exc:
+ raise SoniloError("Sonilo video duration is invalid") from exc
+ if not math.isfinite(duration) or duration <= 0:
+ raise SoniloError("Sonilo video duration is invalid")
+ if duration > MAX_VIDEO_DURATION_SECONDS:
+ raise SoniloError("Sonilo supports videos up to 360 seconds")
+ prompt = str(prompt or "").strip()
+ if len(prompt) > MAX_PROMPT_LENGTH:
+ raise SoniloError("Sonilo music prompt exceeds 2000 characters")
+
+ proxy_path = ""
+ try:
+ proxy_path = _create_video_proxy(video_path)
+ return _request_bgm(proxy_path, output_path, prompt)
+ except SoniloError:
+ raise
+ except OSError as exc:
+ # 临时目录、代理文件和最终原子替换都可能发生文件系统错误。统一转换为
+ # SoniloError,任务编排层才能按设计降级为“无背景音乐”并保留成片。
+ raise SoniloError(f"Sonilo local file operation failed: {exc}") from exc
+ finally:
+ _remove_file(proxy_path)
diff --git a/app/services/task.py b/app/services/task.py
index 68a113f6..7ed4ea39 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -8,7 +8,9 @@ from loguru import logger
from app.config import config
from app.models import const
from app.models.schema import VideoConcatMode, VideoParams
-from app.services import llm, material, subtitle, twelvelabs, video, voice, upload_post
+from app.services import bgm as bgm_service
+from app.services import llm, material, sonilo, subtitle, twelvelabs, video, voice
+from app.services import upload_post
from app.services import state as sm
from app.utils import file_security, utils
@@ -274,10 +276,15 @@ def get_video_materials(task_id, params, video_terms, audio_duration):
def generate_final_videos(
- task_id, params, downloaded_videos, audio_file, subtitle_path
+ task_id, params, downloaded_videos, audio_file, subtitle_path, audio_duration
):
final_video_paths = []
combined_video_paths = []
+ warnings = []
+ sonilo_bgm_requested = (
+ params.bgm_type == "sonilo"
+ and bgm_service.should_use_bgm(params.bgm_type, params.bgm_volume)
+ )
# 多视频生成默认会打散素材以增加差异;但“按文案顺序匹配素材”追求的是
# 时间线稳定性和可解释性,所以开启后所有输出都使用顺序拼接。
if params.match_materials_to_script:
@@ -312,14 +319,52 @@ def generate_final_videos(
final_video_path = path.join(utils.task_dir(task_id), f"final-{index}.mp4")
+ # Sonilo 模式下先明确禁用默认 BGM 解析,避免恢复旧任务时残留的
+ # bgm_file 被误当成当前配乐。只有音量大于 0 才生成代理并调用付费 API;
+ # 0 音量表示完整禁用背景音乐,不产生生成、下载或混音开销。
+ bgm_file_override = "" if params.bgm_type == "sonilo" else None
+ if sonilo_bgm_requested:
+ sonilo_bgm_path = path.join(
+ utils.task_dir(task_id), f"sonilo-bgm-{index}.m4a"
+ )
+ try:
+ sonilo.generate_bgm(
+ video_path=combined_video_path,
+ output_path=sonilo_bgm_path,
+ video_duration=audio_duration,
+ prompt=params.sonilo_bgm_prompt,
+ )
+ bgm_file_override = sonilo_bgm_path
+ except sonilo.SoniloError as exc:
+ # 视频、旁白和字幕都已生成时,第三方配乐临时失败不应浪费整条
+ # 任务。当前视频明确禁用 BGM,并把降级结果返回 WebUI 提醒用户。
+ logger.warning(
+ f"Sonilo BGM generation failed: task_id={task_id}, "
+ f"video_index={index}, error={exc}"
+ )
+ bgm_file_override = ""
+ warnings.append(
+ {"code": "sonilo_bgm_failed", "video_index": index}
+ )
+
logger.info(f"\n\n## generating video: {index} => {final_video_path}")
- video.generate_video(
+ bgm_mix_succeeded = video.generate_video(
video_path=combined_video_path,
audio_path=audio_file,
subtitle_path=subtitle_path,
output_file=final_video_path,
params=params,
+ bgm_file_override=bgm_file_override,
)
+ if (
+ params.bgm_type == "sonilo"
+ and bgm_file_override
+ and not bgm_mix_succeeded
+ ):
+ # Sonilo 已成功返回并通过 FFmpeg 校验,但 MoviePy 最终混音仍可能
+ # 因运行环境失败。视频服务会保留无 BGM 成片,任务层复用同一结构化
+ # 警告通知 WebUI;API 生成失败时 override 为空,不会重复追加警告。
+ warnings.append({"code": "sonilo_bgm_failed", "video_index": index})
_progress += 50 / params.video_count / 2
sm.state.update_task(task_id, progress=_progress)
@@ -327,13 +372,25 @@ def generate_final_videos(
final_video_paths.append(final_video_path)
combined_video_paths.append(combined_video_path)
- return final_video_paths, combined_video_paths
+ return final_video_paths, combined_video_paths, warnings
def start(task_id, params: VideoParams, stop_at: str = "video"):
logger.info(f"start task: {task_id}, stop_at: {stop_at}")
sm.state.update_task(task_id, state=const.TASK_STATE_PROCESSING, progress=5)
+ # 只有完整成片流程需要 Sonilo。尽早阻止缺少 Key 的完整任务,避免先消耗
+ # LLM、TTS 和素材服务额度;各个中间产物接口仍可独立使用,不受配乐配置影响。
+ if (
+ stop_at == "video"
+ and params.bgm_type == "sonilo"
+ and bgm_service.should_use_bgm(params.bgm_type, params.bgm_volume)
+ and not sonilo.is_enabled()
+ ):
+ logger.error("Sonilo background music requires an API key")
+ sm.state.update_task(task_id, state=const.TASK_STATE_FAILED)
+ return
+
# 1. Generate script
video_script = generate_script(task_id, params)
if not video_script or "Error: " in video_script:
@@ -426,8 +483,13 @@ def start(task_id, params: VideoParams, stop_at: str = "video"):
params.video_concat_mode = VideoConcatMode(params.video_concat_mode)
# 6. Generate final videos
- final_video_paths, combined_video_paths = generate_final_videos(
- task_id, params, downloaded_videos, audio_file, subtitle_path
+ final_video_paths, combined_video_paths, generation_warnings = generate_final_videos(
+ task_id,
+ params,
+ downloaded_videos,
+ audio_file,
+ subtitle_path,
+ audio_duration,
)
if not final_video_paths:
@@ -482,6 +544,7 @@ def start(task_id, params: VideoParams, stop_at: str = "video"):
"subtitle_path": subtitle_path,
"materials": downloaded_videos,
"cross_post_results": cross_post_results if cross_post_results else None,
+ "warnings": generation_warnings or None,
}
sm.state.update_task(
task_id, state=const.TASK_STATE_COMPLETE, progress=100, **kwargs
diff --git a/app/services/video.py b/app/services/video.py
index 0e9cbc97..3a44eb58 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -8,7 +8,7 @@ import subprocess
import sys
import tempfile
import unicodedata
-from contextlib import redirect_stdout
+from contextlib import ExitStack, redirect_stdout
from functools import lru_cache
from typing import List
from loguru import logger
@@ -965,7 +965,15 @@ def generate_video(
subtitle_path: str,
output_file: str,
params: VideoParams,
-):
+ bgm_file_override: str | None = None,
+) -> bool:
+ """
+ 合成最终视频,并返回本次背景音乐处理是否成功。
+
+ 返回值只描述 BGM 处理状态:没有请求 BGM 或成功混合时返回 True;请求了
+ BGM 但加载、特效或混合失败时返回 False。即使 BGM 失败仍会继续输出只有
+ 旁白的视频,让任务编排层决定是否向用户展示降级警告。
+ """
aspect = VideoAspect(params.video_aspect)
video_width, video_height = aspect.to_resolution()
@@ -1148,60 +1156,106 @@ def generate_video(
_clip = _clip.with_position(("center", "center"))
return _clip
- video_clip = _open_video_clip_quietly(video_path)
- audio_clip = AudioFileClip(audio_path).with_effects(
- [afx.MultiplyVolume(params.voice_volume)]
- )
-
- def make_textclip(text):
- return TextClip(
- text=text,
- font=font_path,
- font_size=params.font_size,
+ # MoviePy 的 CompositeAudioClip.close() 不会关闭子 AudioFileClip。这里用
+ # ExitStack 显式持有所有原始文件 reader,确保成功、字幕异常、混音失败和
+ # 视频写入失败等路径都能释放 FFmpeg 子进程,尤其避免 Windows 文件被占用。
+ with ExitStack() as clip_stack:
+ source_video_clip = clip_stack.enter_context(
+ _open_video_clip_quietly(video_path)
)
+ voice_source_clip = clip_stack.enter_context(AudioFileClip(audio_path))
+ video_clip = source_video_clip
+ audio_clip = voice_source_clip.with_effects(
+ [afx.MultiplyVolume(params.voice_volume)]
+ )
+
+ def make_textclip(text):
+ return TextClip(
+ text=text,
+ font=font_path,
+ font_size=params.font_size,
+ )
- if subtitle_path and os.path.exists(subtitle_path):
- sub = SubtitlesClip(
- subtitles=subtitle_path, encoding="utf-8", make_textclip=make_textclip
+ if subtitle_path and os.path.exists(subtitle_path):
+ sub = clip_stack.enter_context(
+ SubtitlesClip(
+ subtitles=subtitle_path,
+ encoding="utf-8",
+ make_textclip=make_textclip,
+ )
+ )
+ text_clips = []
+ for item in sub.subtitles:
+ clip = create_text_clip(subtitle_item=item)
+ text_clips.append(clip)
+ video_clip = CompositeVideoClip([video_clip, *text_clips])
+ clip_stack.callback(video_clip.close)
+
+ bgm_enabled = bgm_service.should_use_bgm(
+ params.bgm_type, params.bgm_volume
)
- text_clips = []
- for item in sub.subtitles:
- clip = create_text_clip(subtitle_item=item)
- text_clips.append(clip)
- video_clip = CompositeVideoClip([video_clip, *text_clips])
+ if not bgm_enabled and params.bgm_type:
+ # 所有 BGM 来源共用这一条短路规则。音量不大于 0 时不能解析随机或
+ # 自定义文件,也不能加载提供商返回的文件,避免无意义的 IO 和混音。
+ logger.info(
+ f"skipping background music because volume is not positive: "
+ f"type={params.bgm_type}, volume={params.bgm_volume}"
+ )
- bgm_file = get_bgm_file(bgm_type=params.bgm_type, bgm_file=params.bgm_file)
- if bgm_file:
- try:
- bgm_clip = AudioFileClip(bgm_file).with_effects(
- [
+ # 提供商配乐可由任务编排层直接传入对应文件。None 表示沿用随机/自定义
+ # BGM 解析,空字符串明确禁用本条 BGM;但任何来源都必须先通过通用音量规则。
+ bgm_file = ""
+ if bgm_enabled:
+ bgm_file = (
+ bgm_file_override
+ if bgm_file_override is not None
+ else get_bgm_file(
+ bgm_type=params.bgm_type,
+ bgm_file=params.bgm_file,
+ )
+ )
+ bgm_mix_succeeded = True
+ if bgm_file:
+ try:
+ bgm_effects = [
afx.MultiplyVolume(params.bgm_volume),
afx.AudioFadeOut(3),
- afx.AudioLoop(duration=video_clip.duration),
]
- )
- audio_clip = CompositeAudioClip([audio_clip, bgm_clip])
- except Exception as e:
- logger.error(f"failed to add bgm: {str(e)}")
-
- video_clip = video_clip.with_audio(audio_clip)
- # 显式沿用输入音频的采样率;如果取不到,再回退到 MoviePy 默认的 44100Hz。
- # 这样可以减少不同运行环境,尤其是 Docker 环境中再次重采样带来的音质波动。
- output_audio_fps = int(getattr(audio_clip, "fps", 0) or 44100)
- _write_videofile_with_codec_fallback(
- video_clip,
- output_file=output_file,
- codec=_get_configured_video_codec(),
- audio_codec=audio_codec,
- audio_fps=output_audio_fps,
- audio_bitrate=audio_bitrate,
- temp_audiofile_path=_get_temp_audio_dir(output_dir),
- threads=params.n_threads or 2,
- logger=None,
- fps=fps,
- )
- video_clip.close()
- del video_clip
+ # 服务内解析的随机/自定义音乐可能比成片短,需要循环铺满;任务层
+ # 通过 override 传入的文件表示提供商已经完成时长适配。这里依据
+ # 文件来源决定是否循环,避免今后每增加一个提供商都修改名称白名单。
+ if bgm_file_override is None:
+ bgm_effects.append(afx.AudioLoop(duration=video_clip.duration))
+ bgm_source_clip = clip_stack.enter_context(AudioFileClip(bgm_file))
+ bgm_clip = bgm_source_clip.with_effects(bgm_effects)
+ audio_clip = CompositeAudioClip([audio_clip, bgm_clip])
+ except Exception:
+ bgm_mix_succeeded = False
+ # 记录完整堆栈和稳定上下文,便于区分文件解码、MoviePy 特效和
+ # CompositeAudioClip 失败;文件内容与 API Key 不会进入日志。
+ logger.exception(
+ f"failed to mix background music: type={params.bgm_type}, "
+ f"file={bgm_file}"
+ )
+
+ final_video_clip = video_clip.with_audio(audio_clip)
+ clip_stack.callback(final_video_clip.close)
+ # 显式沿用输入音频的采样率;如果取不到,再回退 MoviePy 默认的 44100Hz。
+ # 这样可以减少不同环境,尤其 Docker 中再次重采样带来的音质波动。
+ output_audio_fps = int(getattr(audio_clip, "fps", 0) or 44100)
+ _write_videofile_with_codec_fallback(
+ final_video_clip,
+ output_file=output_file,
+ codec=_get_configured_video_codec(),
+ audio_codec=audio_codec,
+ audio_fps=output_audio_fps,
+ audio_bitrate=audio_bitrate,
+ temp_audiofile_path=_get_temp_audio_dir(output_dir),
+ threads=params.n_threads or 2,
+ logger=None,
+ fps=fps,
+ )
+ return bgm_mix_succeeded
def preprocess_video(materials: List[MaterialInfo], clip_duration=4):
diff --git a/cli.py b/cli.py
index 781339c9..9a94a1f9 100644
--- a/cli.py
+++ b/cli.py
@@ -119,9 +119,11 @@ def _bgm_type(value: str) -> str:
normalized = value.strip().lower()
if normalized == "none":
return ""
- if normalized in {"", "random", "custom"}:
+ if normalized in {"", "random", "custom", "sonilo"}:
return normalized
- raise argparse.ArgumentTypeError("bgm-type must be one of: none, random, custom")
+ raise argparse.ArgumentTypeError(
+ "bgm-type must be one of: none, random, custom, sonilo"
+ )
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
@@ -319,19 +321,25 @@ Output and exit status:
"--bgm-type",
type=_bgm_type,
default=None,
- metavar="{none,random,custom}",
+ metavar="{none,random,custom,sonilo}",
help=(
- "background music mode; --bgm-file implies custom when omitted "
+ "background music mode; Sonilo reads its API key from config.toml or "
+ "SONILO_API_KEY; --bgm-file implies custom when omitted "
"(default: random)"
),
)
+ audio_group.add_argument(
+ "--sonilo-bgm-prompt",
+ default=None,
+ help="optional music style prompt for Sonilo, up to 2000 characters",
+ )
audio_group.add_argument(
"--bgm-file",
default=None,
metavar="PATH",
help=(
- "custom MP3 inside resource/songs; accepts a filename or a path "
- "relative to the project root"
+ "custom supported audio file inside storage/bgm or resource/songs; "
+ "accepts a filename or an allowed managed path"
),
)
audio_group.add_argument(
@@ -463,9 +471,15 @@ Output and exit status:
if args.bgm_type in (None, "custom"):
args.bgm_type = "custom"
else:
- parser.error("--bgm-file cannot be combined with --bgm-type none or random")
- elif args.bgm_type == "custom":
- parser.error("--bgm-file is required when --bgm-type is custom")
+ parser.error("--bgm-file can only be combined with --bgm-type custom")
+
+ if args.sonilo_bgm_prompt:
+ if args.bgm_type in (None, "sonilo"):
+ args.bgm_type = "sonilo"
+ else:
+ parser.error(
+ "--sonilo-bgm-prompt can only be combined with --bgm-type sonilo"
+ )
if args.custom_position is not None and args.subtitle_position != "custom":
parser.error("--custom-position requires --subtitle-position custom")
@@ -532,6 +546,7 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
"bgm_type",
"bgm_file",
"bgm_volume",
+ "sonilo_bgm_prompt",
"font_name",
"subtitle_position",
"custom_position",
@@ -637,6 +652,7 @@ def prepare_cli_files(params: VideoParams, stop_at: str) -> None:
复制到受控目录,再把参数替换为服务层可安全使用的绝对路径。
"""
from app.models import const
+ from app.services import bgm as bgm_service
from app.utils import utils
local_material_extensions = {
@@ -659,14 +675,32 @@ def prepare_cli_files(params: VideoParams, stop_at: str) -> None:
f"allowed extensions: {allowed}"
)
- if params.bgm_type == "custom" and params.bgm_file:
- params.bgm_file = _resolve_managed_resource_file(
- params.bgm_file,
- resource_dir=utils.song_dir(),
- description="background music",
- )
- if not params.bgm_file.lower().endswith(".mp3"):
- raise ValueError("background music must use the .mp3 extension")
+ if params.bgm_type == "custom":
+ if not bgm_service.should_use_bgm(params.bgm_type, params.bgm_volume):
+ # 0 音量时下游会统一跳过所有 BGM。这里同时清空文件参数,避免
+ # CLI 为一个不会被读取的文件执行路径解析、存在性检查或格式
+ # 校验。
+ params.bgm_file = ""
+ elif not params.bgm_file:
+ # 缺少文件是否构成错误取决于通用 BGM 开关,不能在 argparse 阶段
+ # 无条件拦截,否则 ``custom + 0%`` 会和 WebUI、服务层行为不一致。
+ raise ValueError("--bgm-file is required when --bgm-type is custom")
+ else:
+ try:
+ # CLI、WebUI 和任务服务必须共用同一个 BGM 文件边界。这里直接
+ # 复用服务层解析,既支持用户上传目录和内置歌曲目录,也
+ # 自动继承新增音频格式及路径安全规则,避免多个入口分别
+ # 维护白名单。
+ params.bgm_file = bgm_service.resolve_bgm_file(params.bgm_file)
+ except ValueError as exc:
+ supported_extensions = ", ".join(
+ bgm_service.SUPPORTED_BGM_EXTENSIONS
+ )
+ raise ValueError(
+ "background music must be a supported audio file inside "
+ f"storage/bgm or resource/songs ({supported_extensions}): "
+ f"{params.bgm_file}"
+ ) from exc
if params.subtitle_enabled and params.font_name and stop_at == "video":
font_path = _resolve_managed_resource_file(
diff --git a/config.example.toml b/config.example.toml
index 4edb6cc1..59f6a963 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -64,6 +64,14 @@ twelvelabs_rerank_terms = false
# 默认关闭,开启后会减少随机性,使素材顺序更贴近文案结构。
match_materials_to_script = false
+# Optional Sonilo video-matched original background music.
+# Create a key at https://platform.sonilo.com/dashboard. The WebUI can also save
+# this value, or you can set the SONILO_API_KEY environment variable instead.
+sonilo_api_key = ""
+sonilo_base_url = "https://api.sonilo.com"
+# 单次生成流连续无数据时的最长等待秒数;它不是整个请求的总耗时上限。
+sonilo_timeout = 600
+
# -----------------------------------------------------------------------------
# LLM Providers / 大模型提供商
# -----------------------------------------------------------------------------
diff --git a/test/services/test_bgm.py b/test/services/test_bgm.py
index 30a1dfe2..1ca1a0d5 100644
--- a/test/services/test_bgm.py
+++ b/test/services/test_bgm.py
@@ -33,6 +33,27 @@ class _TextUpload(io.BytesIO):
class TestBackgroundMusicService(unittest.TestCase):
+ def test_should_use_bgm_is_provider_independent(self):
+ """通用开关必须覆盖当前来源和未来提供商,不能再写提供商特判。"""
+ test_cases = [
+ ("random", 0.2, True),
+ ("custom", 0.2, True),
+ ("sonilo", 0.2, True),
+ ("future_provider", 0.2, True),
+ ("random", 0.0, False),
+ ("custom", -0.1, False),
+ ("sonilo", None, False),
+ ("future_provider", float("nan"), False),
+ ("future_provider", "invalid", False),
+ ("", 0.2, False),
+ (None, 0.2, False),
+ ]
+ for bgm_type, bgm_volume, expected in test_cases:
+ with self.subTest(bgm_type=bgm_type, bgm_volume=bgm_volume):
+ self.assertEqual(
+ bgm.should_use_bgm(bgm_type, bgm_volume), expected
+ )
+
def test_sanitize_upload_filename_strips_directory_components(self):
self.assertEqual(
bgm.sanitize_upload_filename("../../用户音乐.mp3"), "用户音乐.mp3"
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 4062afcb..6d074859 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -215,6 +215,19 @@ class TestCli(unittest.TestCase):
params = cli.build_video_params(args)
self.assertEqual(params.bgm_type, "")
+ def test_sonilo_prompt_implies_sonilo_bgm_mode(self):
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--sonilo-bgm-prompt",
+ "warm acoustic",
+ ]
+ )
+ params = cli.build_video_params(args)
+ self.assertEqual(params.bgm_type, "sonilo")
+ self.assertEqual(params.sonilo_bgm_prompt, "warm acoustic")
+
def test_local_material_filename_resolved_to_absolute_path(self):
"""After preprocess_video, material.url should be an absolute path, not a bare filename."""
import os
@@ -295,17 +308,118 @@ class TestCli(unittest.TestCase):
self.assertEqual(cm.exception.code, 2)
- def test_custom_bgm_requires_file_and_file_implies_custom_mode(self):
- with self.assertRaises(SystemExit):
- cli.parse_args(
- ["--video-subject", "test", "--bgm-type", "custom"]
+ def test_positive_volume_custom_bgm_requires_file_before_task_start(self):
+ """启用自定义 BGM 时仍必须在任务启动前报告缺少文件。"""
+ with (
+ patch("app.services.task.start") as start,
+ patch.object(cli.logger, "error") as log_error,
+ ):
+ code = cli.run_cli(
+ [
+ "--video-subject",
+ "test",
+ "--bgm-type",
+ "custom",
+ "--stop-at",
+ "script",
+ ]
)
+ self.assertEqual(code, 2)
+ start.assert_not_called()
+ self.assertIn(
+ "--bgm-file is required",
+ str(log_error.call_args),
+ )
+
+ def test_bgm_file_implies_custom_mode(self):
args = cli.parse_args(
["--video-subject", "test", "--bgm-file", "output001.mp3"]
)
self.assertEqual(args.bgm_type, "custom")
+ def test_zero_volume_custom_bgm_skips_file_requirement_and_resolution(self):
+ """0 音量应忽略缺失或无效文件,与 WebUI 和视频服务保持一致。"""
+ file_arguments = [[], ["--bgm-file", "missing-background.mp3"]]
+ for extra_arguments in file_arguments:
+ with self.subTest(extra_arguments=extra_arguments):
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--bgm-type",
+ "custom",
+ "--bgm-volume",
+ "0",
+ *extra_arguments,
+ ]
+ )
+ params = cli.build_video_params(args)
+ with patch(
+ "app.services.bgm.resolve_bgm_file",
+ side_effect=AssertionError(
+ "zero-volume BGM must not resolve a file"
+ ),
+ ) as resolver:
+ cli.prepare_cli_files(params, stop_at="script")
+
+ resolver.assert_not_called()
+ self.assertEqual(params.bgm_file, "")
+
+ def test_custom_bgm_reuses_service_formats_and_managed_path_resolution(self):
+ """CLI 必须跟随 BGM 服务的格式白名单,不能继续单独限制为 MP3。"""
+ from app.services import bgm as bgm_service
+
+ for extension in bgm_service.SUPPORTED_BGM_EXTENSIONS:
+ with self.subTest(extension=extension):
+ filename = f"uploaded{extension}"
+ resolved_path = f"/managed/storage/bgm/{filename}"
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--bgm-type",
+ "custom",
+ "--bgm-file",
+ filename,
+ ]
+ )
+ params = cli.build_video_params(args)
+ with patch.object(
+ bgm_service,
+ "resolve_bgm_file",
+ return_value=resolved_path,
+ ) as resolver:
+ cli.prepare_cli_files(params, stop_at="script")
+
+ resolver.assert_called_once_with(filename)
+ self.assertEqual(params.bgm_file, resolved_path)
+
+ def test_custom_bgm_reports_service_resolution_failure_before_task_start(self):
+ """非法格式或越界路径应转换为包含统一格式范围的 CLI 错误。"""
+ from app.services import bgm as bgm_service
+
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--bgm-type",
+ "custom",
+ "--bgm-file",
+ "unsafe.exe",
+ ]
+ )
+ params = cli.build_video_params(args)
+ with (
+ patch.object(
+ bgm_service,
+ "resolve_bgm_file",
+ side_effect=ValueError("unsupported background music path"),
+ ),
+ self.assertRaisesRegex(ValueError, "storage/bgm or resource/songs"),
+ ):
+ cli.prepare_cli_files(params, stop_at="script")
+
def test_invalid_aspect_and_non_finite_numbers_are_argument_errors(self):
invalid_argvs = [
["--video-subject", "test", "--video-aspect", "invalid"],
diff --git a/test/services/test_sonilo.py b/test/services/test_sonilo.py
new file mode 100644
index 00000000..4e2b7312
--- /dev/null
+++ b/test/services/test_sonilo.py
@@ -0,0 +1,533 @@
+import base64
+import json
+import os
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+from app.services import sonilo
+
+
+class _StreamingResponse:
+ """提供 requests.Response 在 Sonilo 服务中实际使用的最小接口。"""
+
+ def __init__(
+ self,
+ events=None,
+ *,
+ status_code=200,
+ payload=None,
+ iter_error=None,
+ ):
+ self.events = events or []
+ self.status_code = status_code
+ self.ok = 200 <= status_code < 300
+ self.reason = "OK" if self.ok else "Request failed"
+ self.text = "" if self.ok else "request failed"
+ self.payload = payload if payload is not None else {"services": []}
+ self.iter_error = iter_error
+
+ def iter_lines(self):
+ if self.iter_error:
+ raise self.iter_error
+ return iter(self.events)
+
+ def json(self):
+ return self.payload
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ return False
+
+
+def _event(event_type, **values):
+ return json.dumps({"type": event_type, **values}).encode("utf-8")
+
+
+class TestSoniloService(unittest.TestCase):
+ def test_api_key_prefers_config_and_falls_back_to_environment(self):
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "config-key"}),
+ patch.dict(os.environ, {"SONILO_API_KEY": "env-key"}),
+ ):
+ self.assertEqual(sonilo.get_api_key(), "config-key")
+
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": ""}),
+ patch.dict(os.environ, {"SONILO_API_KEY": "env-key"}),
+ ):
+ self.assertEqual(sonilo.get_api_key(), "env-key")
+
+ def test_request_timeout_clamps_fractional_and_invalid_values(self):
+ """读取超时必须保持为 Requests 接受的正整数,并限制最大等待时间。"""
+ test_cases = [
+ (0.5, (15, 1)),
+ (1.1, (15, 2)),
+ (1800.5, (15, 1800)),
+ (0, (15, 600)),
+ (-1, (15, 600)),
+ (float("inf"), (15, 600)),
+ ("invalid", (15, 600)),
+ ]
+ for configured_timeout, expected in test_cases:
+ with self.subTest(configured_timeout=configured_timeout), patch.object(
+ sonilo.config,
+ "app",
+ {"sonilo_timeout": configured_timeout},
+ ):
+ self.assertEqual(sonilo._request_timeout(), expected)
+
+ def test_connection_uses_non_billing_services_endpoint(self):
+ response = _StreamingResponse(
+ payload={"available_services": ["video_to_music"]}
+ )
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(sonilo.requests, "get", return_value=response) as request,
+ ):
+ result = sonilo.test_connection()
+
+ self.assertEqual(result, {"available_services": ["video_to_music"]})
+ self.assertTrue(request.call_args.args[0].endswith("/v1/account/services"))
+ self.assertEqual(
+ request.call_args.kwargs["headers"]["Authorization"], "Bearer test-key"
+ )
+
+ def test_connection_accepts_documented_hyphenated_service_id(self):
+ """公开文档的连字符写法必须归一化为项目内部服务标识。"""
+ response = _StreamingResponse(
+ payload={"available_services": ["video-to-music"]}
+ )
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(sonilo.requests, "get", return_value=response),
+ ):
+ result = sonilo.test_connection()
+
+ self.assertEqual(result, {"available_services": ["video-to-music"]})
+
+ def test_connection_rejects_malformed_service_lists(self):
+ """200 响应缺少规范服务列表时不能向 WebUI 报告连接成功。"""
+ invalid_payloads = [
+ {},
+ {"available_services": "video_to_music"},
+ {"available_services": ["video_to_music", 1]},
+ ]
+ for payload in invalid_payloads:
+ with (
+ self.subTest(payload=payload),
+ patch.object(
+ sonilo.config, "app", {"sonilo_api_key": "test-key"}
+ ),
+ patch.object(
+ sonilo.requests,
+ "get",
+ return_value=_StreamingResponse(payload=payload),
+ ),
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "service list"):
+ sonilo.test_connection()
+
+ def test_connection_rejects_key_without_video_to_music_service(self):
+ response = _StreamingResponse(
+ payload={"available_services": ["text_to_music"]}
+ )
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(sonilo.requests, "get", return_value=response),
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "not available"):
+ sonilo.test_connection()
+
+ def test_connection_converts_network_and_invalid_json_errors(self):
+ """连接测试的网络中断和非 JSON 响应都转换为稳定的领域异常。"""
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(
+ sonilo.requests,
+ "get",
+ side_effect=sonilo.requests.Timeout("timed out"),
+ ),
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "failed to connect"):
+ sonilo.test_connection()
+
+ response = _StreamingResponse(payload={})
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(sonilo.requests, "get", return_value=response),
+ patch.object(response, "json", side_effect=ValueError("invalid json")),
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "invalid service response"):
+ sonilo.test_connection()
+
+ def test_create_video_proxy_uses_expected_ffmpeg_policy(self):
+ """成功代理必须去音轨、限制尺寸,并由调用方接管生成文件。"""
+ with tempfile.TemporaryDirectory() as temp_dir:
+ source = Path(temp_dir) / "source.mp4"
+ source.write_bytes(b"source-video")
+
+ def create_proxy(command, **_kwargs):
+ Path(command[-1]).write_bytes(b"proxy-video")
+ return sonilo.subprocess.CompletedProcess(command, 0, "", "")
+
+ with (
+ patch.object(
+ sonilo.utils, "get_ffmpeg_binary", return_value="test-ffmpeg"
+ ),
+ patch.object(
+ sonilo.subprocess, "run", side_effect=create_proxy
+ ) as run,
+ ):
+ proxy_path = sonilo._create_video_proxy(str(source))
+
+ command = run.call_args.args[0]
+ self.assertEqual(command[0], "test-ffmpeg")
+ self.assertIn("-an", command)
+ 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()
+
+ def test_create_video_proxy_cleans_file_after_execution_failures(self):
+ """FFmpeg 超时、不可执行或编码失败时均不能遗留隐藏代理文件。"""
+ failure_cases = [
+ (
+ sonilo.subprocess.TimeoutExpired("ffmpeg", 600),
+ "timed out",
+ ),
+ (OSError("ffmpeg missing"), "failed to run FFmpeg"),
+ (
+ sonilo.subprocess.CompletedProcess(
+ ["ffmpeg"], 1, "", "encoder unavailable"
+ ),
+ "encoder unavailable",
+ ),
+ ]
+ for result_or_error, expected_message in failure_cases:
+ with (
+ self.subTest(expected_message=expected_message),
+ tempfile.TemporaryDirectory() as temp_dir,
+ ):
+ source = Path(temp_dir) / "source.mp4"
+ source.write_bytes(b"source-video")
+ run_kwargs = (
+ {"return_value": result_or_error}
+ if isinstance(result_or_error, sonilo.subprocess.CompletedProcess)
+ else {"side_effect": result_or_error}
+ )
+ with patch.object(sonilo.subprocess, "run", **run_kwargs):
+ with self.assertRaisesRegex(
+ sonilo.SoniloError, expected_message
+ ):
+ sonilo._create_video_proxy(str(source))
+
+ self.assertEqual(list(Path(temp_dir).glob(".sonilo-proxy-*")), [])
+
+ def test_create_video_proxy_rejects_empty_and_oversized_outputs(self):
+ """FFmpeg 返回成功也必须再次校验代理文件存在、非空且未超上限。"""
+ for output_size, expected_size in (
+ (0, 0),
+ (1, sonilo.MAX_PROXY_BYTES + 1),
+ ):
+ with (
+ self.subTest(expected_size=expected_size),
+ tempfile.TemporaryDirectory() as temp_dir,
+ ):
+ source = Path(temp_dir) / "source.mp4"
+ source.write_bytes(b"source-video")
+
+ def create_proxy(command, **_kwargs):
+ Path(command[-1]).write_bytes(b"x" * output_size)
+ return sonilo.subprocess.CompletedProcess(command, 0, "", "")
+
+ with (
+ patch.object(sonilo.subprocess, "run", side_effect=create_proxy),
+ patch.object(
+ sonilo.os.path,
+ "getsize",
+ return_value=expected_size,
+ ),
+ ):
+ with self.assertRaisesRegex(
+ sonilo.SoniloError, "empty or exceeds"
+ ):
+ sonilo._create_video_proxy(str(source))
+
+ self.assertEqual(list(Path(temp_dir).glob(".sonilo-proxy-*")), [])
+
+ def test_stream_audio_selects_first_track_and_requires_complete(self):
+ first_chunk = b"first-track"
+ second_chunk = b"second-track"
+ response = _StreamingResponse(
+ [
+ _event(
+ "audio_chunk",
+ stream_index=1,
+ data=base64.b64encode(second_chunk).decode(),
+ ),
+ _event(
+ "audio_chunk",
+ stream_index=0,
+ data=base64.b64encode(first_chunk).decode(),
+ ),
+ _event("complete"),
+ ]
+ )
+ with tempfile.TemporaryDirectory() as temp_dir:
+ output = Path(temp_dir) / "music.m4a"
+ size, _ = sonilo._stream_audio(response, str(output))
+ self.assertEqual(output.read_bytes(), first_chunk)
+ self.assertEqual(size, len(first_chunk))
+
+ incomplete = _StreamingResponse(
+ [
+ _event(
+ "audio_chunk",
+ stream_index=0,
+ data=base64.b64encode(first_chunk).decode(),
+ )
+ ]
+ )
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with self.assertRaisesRegex(sonilo.SoniloError, "before completion"):
+ sonilo._stream_audio(
+ incomplete, str(Path(temp_dir) / "incomplete.m4a")
+ )
+
+ def test_stream_audio_rejects_malformed_json_and_base64(self):
+ invalid_cases = [
+ [b"not-json"],
+ [_event("audio_chunk", stream_index=0, data="%%%")],
+ [_event("audio_chunk", stream_index=0, data=""), _event("complete")],
+ ]
+ for events in invalid_cases:
+ with self.subTest(events=events), tempfile.TemporaryDirectory() as temp_dir:
+ with self.assertRaises(sonilo.SoniloError):
+ sonilo._stream_audio(
+ _StreamingResponse(events), str(Path(temp_dir) / "bad.m4a")
+ )
+
+ def test_stream_audio_rejects_error_empty_and_oversized_results(self):
+ """服务端错误、仅完成事件和超体积音频都不能发布为有效 BGM。"""
+ oversized_chunk = base64.b64encode(b"1234").decode()
+ cases = [
+ ([_event("error", message="credit exhausted")], "credit exhausted"),
+ ([_event("complete")], "no audio data"),
+ (
+ [
+ _event("audio_chunk", stream_index=0, data=oversized_chunk),
+ _event("complete"),
+ ],
+ "exceeds",
+ ),
+ ]
+ for events, expected_message in cases:
+ with (
+ self.subTest(expected_message=expected_message),
+ tempfile.TemporaryDirectory() as temp_dir,
+ ):
+ output = Path(temp_dir) / "music.m4a"
+ # 所有用例统一缩小体积上限;错误事件和空结果不受该值影响,
+ # 超限用例则无需在测试中分配 30 MB 数据。
+ with (
+ patch.object(sonilo, "MAX_GENERATED_AUDIO_BYTES", 3),
+ self.assertRaisesRegex(sonilo.SoniloError, expected_message),
+ ):
+ sonilo._stream_audio(_StreamingResponse(events), str(output))
+
+ def test_request_bgm_validates_then_atomically_publishes_audio(self):
+ audio_bytes = b"synthetic-fmp4-audio"
+ response = _StreamingResponse(
+ [
+ _event(
+ "audio_chunk",
+ stream_index=0,
+ data=base64.b64encode(audio_bytes).decode(),
+ ),
+ _event("complete"),
+ ]
+ )
+ 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"}),
+ patch.object(sonilo.requests, "post", return_value=response) as post,
+ patch.object(
+ sonilo.bgm_service, "validate_audio_file"
+ ) as validate_audio,
+ ):
+ result = sonilo._request_bgm(
+ str(video_path), str(output_path), "warm"
+ )
+
+ self.assertEqual(result, str(output_path))
+ self.assertEqual(output_path.read_bytes(), audio_bytes)
+ validate_audio.assert_called_once()
+ self.assertEqual(post.call_args.kwargs["data"], {"prompt": "warm"})
+ self.assertEqual(post.call_args.kwargs["stream"], True)
+ self.assertEqual(list(Path(temp_dir).glob(".sonilo-audio-*")), [])
+
+ def test_request_bgm_preserves_existing_output_and_cleans_temp_on_failures(self):
+ """HTTP、流读取和音频校验失败都不能覆盖已有结果或留下半成品。"""
+ audio_event = _event(
+ "audio_chunk",
+ stream_index=0,
+ data=base64.b64encode(b"invalid-audio").decode(),
+ )
+ failure_cases = [
+ (
+ _StreamingResponse(status_code=401),
+ None,
+ "401",
+ ),
+ (
+ _StreamingResponse(
+ iter_error=sonilo.requests.ConnectionError("stream lost")
+ ),
+ None,
+ "failed to request",
+ ),
+ (
+ _StreamingResponse([audio_event, _event("complete")]),
+ sonilo.bgm_service.BgmUploadError("invalid audio"),
+ "cannot decode",
+ ),
+ ]
+ for response, validation_error, expected_message in failure_cases:
+ with (
+ self.subTest(expected_message=expected_message),
+ 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")
+ output_path.write_bytes(b"existing-music")
+ with (
+ patch.object(
+ sonilo.config, "app", {"sonilo_api_key": "test-key"}
+ ),
+ patch.object(sonilo.requests, "post", return_value=response),
+ patch.object(
+ sonilo.bgm_service,
+ "validate_audio_file",
+ side_effect=validation_error,
+ ),
+ ):
+ with self.assertRaisesRegex(
+ sonilo.SoniloError, expected_message
+ ):
+ sonilo._request_bgm(
+ str(video_path), str(output_path), ""
+ )
+
+ self.assertEqual(output_path.read_bytes(), b"existing-music")
+ self.assertEqual(list(Path(temp_dir).glob(".sonilo-audio-*")), [])
+
+ def test_generate_bgm_cleans_proxy_when_request_fails(self):
+ with tempfile.TemporaryDirectory() as temp_dir:
+ source = Path(temp_dir) / "source.mp4"
+ proxy = Path(temp_dir) / "proxy.mp4"
+ source.write_bytes(b"video")
+ proxy.write_bytes(b"proxy")
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(
+ sonilo, "_create_video_proxy", return_value=str(proxy)
+ ),
+ patch.object(
+ sonilo,
+ "_request_bgm",
+ side_effect=sonilo.SoniloError("network failed"),
+ ),
+ ):
+ with self.assertRaises(sonilo.SoniloError):
+ sonilo.generate_bgm(
+ str(source), str(Path(temp_dir) / "music.m4a"), 5
+ )
+
+ self.assertFalse(proxy.exists())
+
+ def test_generate_bgm_converts_file_errors_and_cleans_proxy(self):
+ """文件系统失败也必须转换为可降级异常,并清理已经生成的代理。"""
+ with tempfile.TemporaryDirectory() as temp_dir:
+ source = Path(temp_dir) / "source.mp4"
+ proxy = Path(temp_dir) / "proxy.mp4"
+ source.write_bytes(b"video")
+ proxy.write_bytes(b"proxy")
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": "test-key"}),
+ patch.object(
+ sonilo, "_create_video_proxy", return_value=str(proxy)
+ ),
+ patch.object(
+ sonilo,
+ "_request_bgm",
+ side_effect=OSError("disk full"),
+ ),
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "file operation"):
+ sonilo.generate_bgm(
+ str(source), str(Path(temp_dir) / "music.m4a"), 5
+ )
+
+ self.assertFalse(proxy.exists())
+
+ def test_generate_bgm_rejects_invalid_duration_and_long_prompt(self):
+ with tempfile.TemporaryDirectory() as temp_dir:
+ source = Path(temp_dir) / "source.mp4"
+ source.write_bytes(b"video")
+ with patch.object(
+ sonilo.config, "app", {"sonilo_api_key": "test-key"}
+ ):
+ for duration in (0, -1, float("nan"), 361):
+ with self.subTest(duration=duration):
+ with self.assertRaises(sonilo.SoniloError):
+ sonilo.generate_bgm(
+ str(source), str(Path(temp_dir) / "music.m4a"), duration
+ )
+ with self.assertRaisesRegex(sonilo.SoniloError, "2000"):
+ sonilo.generate_bgm(
+ str(source),
+ str(Path(temp_dir) / "music.m4a"),
+ 5,
+ "x" * 2001,
+ )
+
+ def test_generate_bgm_rejects_missing_key_and_input_before_proxy_work(self):
+ """缺少凭证或输入文件时应快速失败,不能调用 FFmpeg 或外部 API。"""
+ with tempfile.TemporaryDirectory() as temp_dir:
+ source = Path(temp_dir) / "source.mp4"
+ source.write_bytes(b"video")
+ with (
+ patch.object(sonilo.config, "app", {"sonilo_api_key": ""}),
+ patch.dict(os.environ, {}, clear=True),
+ patch.object(sonilo, "_create_video_proxy") as create_proxy,
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "API key"):
+ sonilo.generate_bgm(
+ str(source), str(Path(temp_dir) / "music.m4a"), 5
+ )
+ create_proxy.assert_not_called()
+
+ with (
+ patch.object(
+ sonilo.config, "app", {"sonilo_api_key": "test-key"}
+ ),
+ patch.object(sonilo, "_create_video_proxy") as create_proxy,
+ ):
+ with self.assertRaisesRegex(sonilo.SoniloError, "does not exist"):
+ sonilo.generate_bgm(
+ str(Path(temp_dir) / "missing.mp4"),
+ str(Path(temp_dir) / "music.m4a"),
+ 5,
+ )
+ create_proxy.assert_not_called()
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/services/test_task.py b/test/services/test_task.py
index d83178a6..e8c25ed2 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -72,10 +72,174 @@ class TestTaskService(unittest.TestCase):
downloaded_videos=["material.mp4"],
audio_file="audio.mp3",
subtitle_path="",
+ audio_duration=5,
)
self.assertEqual(combine_videos.call_args.kwargs["clip_speed"], 1.25)
+ def test_generate_final_videos_uses_generated_sonilo_music(self):
+ """Sonilo 必须针对每条拼接后的视频生成配乐,并传给最终混音。"""
+ params = VideoParams(
+ video_subject="test",
+ video_count=1,
+ bgm_type="sonilo",
+ sonilo_bgm_prompt="warm acoustic",
+ )
+
+ with (
+ patch.object(tm.video, "combine_videos"),
+ patch.object(
+ tm.sonilo,
+ "generate_bgm",
+ side_effect=lambda **kwargs: kwargs["output_path"],
+ ) as generate_bgm,
+ patch.object(tm.video, "generate_video") as generate_video,
+ patch.object(tm.sm.state, "update_task"),
+ ):
+ _, _, warnings = tm.generate_final_videos(
+ task_id="sonilo-task",
+ params=params,
+ downloaded_videos=["material.mp4"],
+ audio_file="audio.mp3",
+ subtitle_path="",
+ audio_duration=5,
+ )
+
+ self.assertEqual(warnings, [])
+ self.assertEqual(generate_bgm.call_args.kwargs["video_duration"], 5)
+ self.assertEqual(generate_bgm.call_args.kwargs["prompt"], "warm acoustic")
+ self.assertTrue(
+ generate_video.call_args.kwargs["bgm_file_override"].endswith(
+ "sonilo-bgm-1.m4a"
+ )
+ )
+
+ def test_generate_final_videos_falls_back_without_bgm_on_sonilo_failure(self):
+ """第三方配乐失败时应完成视频并返回可见警告,而不是丢弃所有产物。"""
+ params = VideoParams(video_subject="test", bgm_type="sonilo")
+
+ with (
+ patch.object(tm.video, "combine_videos"),
+ patch.object(
+ tm.sonilo,
+ "generate_bgm",
+ side_effect=tm.sonilo.SoniloError("temporary outage"),
+ ),
+ patch.object(tm.video, "generate_video") as generate_video,
+ patch.object(tm.sm.state, "update_task"),
+ ):
+ final_paths, _, warnings = tm.generate_final_videos(
+ task_id="sonilo-fallback",
+ params=params,
+ downloaded_videos=["material.mp4"],
+ audio_file="audio.mp3",
+ subtitle_path="",
+ audio_duration=5,
+ )
+
+ self.assertEqual(len(final_paths), 1)
+ self.assertEqual(
+ warnings, [{"code": "sonilo_bgm_failed", "video_index": 1}]
+ )
+ self.assertEqual(generate_video.call_args.kwargs["bgm_file_override"], "")
+
+ def test_generate_final_videos_skips_sonilo_when_volume_is_zero(self):
+ """0 音量必须完全跳过 Sonilo 生成,并显式禁用残留背景音乐。"""
+ params = VideoParams(
+ video_subject="test",
+ bgm_type="sonilo",
+ bgm_volume=0.0,
+ bgm_file="stale-custom-bgm.mp3",
+ )
+
+ with (
+ patch.object(tm.video, "combine_videos"),
+ patch.object(tm.sonilo, "generate_bgm") as generate_bgm,
+ patch.object(tm.video, "generate_video", return_value=True) as generate,
+ patch.object(tm.sm.state, "update_task"),
+ ):
+ final_paths, _, warnings = tm.generate_final_videos(
+ task_id="sonilo-zero-volume",
+ params=params,
+ downloaded_videos=["material.mp4"],
+ audio_file="audio.mp3",
+ subtitle_path="",
+ audio_duration=5,
+ )
+
+ self.assertEqual(len(final_paths), 1)
+ self.assertEqual(warnings, [])
+ generate_bgm.assert_not_called()
+ self.assertEqual(generate.call_args.kwargs["bgm_file_override"], "")
+
+ def test_generate_final_videos_warns_when_sonilo_mix_fails(self):
+ """Sonilo 生成成功但最终混音失败时,任务必须保留视频并返回警告。"""
+ params = VideoParams(video_subject="test", bgm_type="sonilo")
+
+ with (
+ patch.object(tm.video, "combine_videos"),
+ patch.object(
+ tm.sonilo,
+ "generate_bgm",
+ side_effect=lambda **kwargs: kwargs["output_path"],
+ ),
+ patch.object(tm.video, "generate_video", return_value=False) as generate,
+ patch.object(tm.sm.state, "update_task"),
+ ):
+ final_paths, _, warnings = tm.generate_final_videos(
+ task_id="sonilo-mix-fallback",
+ params=params,
+ downloaded_videos=["material.mp4"],
+ audio_file="audio.mp3",
+ subtitle_path="",
+ audio_duration=5,
+ )
+
+ self.assertEqual(len(final_paths), 1)
+ self.assertEqual(
+ warnings, [{"code": "sonilo_bgm_failed", "video_index": 1}]
+ )
+ self.assertTrue(
+ generate.call_args.kwargs["bgm_file_override"].endswith(".m4a")
+ )
+
+ def test_start_rejects_missing_sonilo_key_before_costly_pipeline_steps(self):
+ """完整任务缺少 Sonilo Key 时不能先调用 LLM、TTS 或素材服务。"""
+ params = VideoParams(video_subject="test", bgm_type="sonilo")
+ with (
+ patch.object(tm.sonilo, "is_enabled", return_value=False),
+ patch.object(tm, "generate_script") as generate_script,
+ patch.object(tm, "generate_audio") as generate_audio,
+ patch.object(tm, "get_video_materials") as get_materials,
+ patch.object(tm.sm.state, "update_task") as update_task,
+ ):
+ result = tm.start("missing-sonilo-key", params)
+
+ self.assertIsNone(result)
+ generate_script.assert_not_called()
+ generate_audio.assert_not_called()
+ get_materials.assert_not_called()
+ update_task.assert_any_call(
+ "missing-sonilo-key", state=tm.const.TASK_STATE_FAILED
+ )
+
+ def test_start_does_not_require_sonilo_key_when_volume_is_zero(self):
+ """0 音量不会使用 Sonilo,因此缺少 Key 时仍应进入正常任务流水线。"""
+ params = VideoParams(
+ video_subject="test",
+ bgm_type="sonilo",
+ bgm_volume=0.0,
+ )
+ with (
+ patch.object(tm.sonilo, "is_enabled", return_value=False),
+ patch.object(tm, "generate_script", return_value="") as generate_script,
+ patch.object(tm.sm.state, "update_task"),
+ ):
+ result = tm.start("zero-volume-without-key", params)
+
+ self.assertIsNone(result)
+ generate_script.assert_called_once_with("zero-volume-without-key", params)
+
def test_generate_terms_uses_script_order_mode_when_enabled(self):
"""
默认模式不受影响;只有用户显式开启素材按文案顺序匹配时,任务层才
@@ -384,7 +548,7 @@ class TestTaskService(unittest.TestCase):
patch.object(
tm,
"generate_final_videos",
- return_value=(["final.mp4"], ["combined.mp4"]),
+ return_value=(["final.mp4"], ["combined.mp4"], []),
),
patch.object(
tm.upload_post.upload_post_service,
@@ -417,14 +581,14 @@ class TestTaskService(unittest.TestCase):
"audio": (
(None, None, None),
["clip.mp4"],
- (["final.mp4"], ["combined.mp4"]),
+ (["final.mp4"], ["combined.mp4"], []),
),
"materials": (
("audio.mp3", 5, object()),
None,
- (["final.mp4"], ["combined.mp4"]),
+ (["final.mp4"], ["combined.mp4"], []),
),
- "video": (("audio.mp3", 5, object()), ["clip.mp4"], ([], [])),
+ "video": (("audio.mp3", 5, object()), ["clip.mp4"], ([], [], [])),
}
for stage, failure_results in failure_cases.items():
@@ -493,6 +657,7 @@ class TestTaskService(unittest.TestCase):
return_value=(
["final-1.mp4", "final-2.mp4"],
["combined-1.mp4", "combined-2.mp4"],
+ [],
),
),
patch.object(service, "is_configured", return_value=True),
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 4ba07a6e..8cd97f73 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -25,6 +25,31 @@ from app.utils import utils
resources_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources")
+class _FakeMoviePyClip:
+ """为最终混音单测提供最小 MoviePy 接口,避免 CI 真实编码大型视频。"""
+
+ def __init__(self, *, duration=5, fps=44100):
+ self.duration = duration
+ self.fps = fps
+ self.close_calls = 0
+ self.with_audio_result = self
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ self.close()
+
+ def close(self):
+ self.close_calls += 1
+
+ def with_effects(self, _effects):
+ return self
+
+ def with_audio(self, _audio):
+ return self.with_audio_result
+
+
class TestVideoService(unittest.TestCase):
def setUp(self):
self.original_app_config = dict(config.app)
@@ -38,6 +63,207 @@ class TestVideoService(unittest.TestCase):
vd._runtime_disabled_video_codecs.clear()
vd._ffmpeg_encoder_exists.cache_clear()
+ def test_generate_video_reports_successful_bgm_mix_and_closes_sources(self):
+ """BGM 混合成功后应返回 True,并释放所有原始文件 reader。"""
+ params = vd.VideoParams(
+ video_subject="test",
+ subtitle_enabled=False,
+ bgm_type="sonilo",
+ )
+ source_video = _FakeMoviePyClip()
+ voice_source = _FakeMoviePyClip()
+ bgm_source = _FakeMoviePyClip()
+ mixed_audio = _FakeMoviePyClip(fps=48000)
+ final_video = _FakeMoviePyClip()
+ source_video.with_audio_result = final_video
+
+ with (
+ patch.object(
+ vd, "_open_video_clip_quietly", return_value=source_video
+ ),
+ patch.object(
+ vd, "AudioFileClip", side_effect=[voice_source, bgm_source]
+ ),
+ patch.object(vd, "CompositeAudioClip", return_value=mixed_audio),
+ patch.object(vd, "_write_videofile_with_codec_fallback") as writer,
+ patch.object(vd, "_get_configured_video_codec", return_value="libx264"),
+ ):
+ result = vd.generate_video(
+ video_path="combined.mp4",
+ audio_path="voice.mp3",
+ subtitle_path="",
+ output_file="final.mp4",
+ params=params,
+ bgm_file_override="sonilo.m4a",
+ )
+
+ self.assertTrue(result)
+ writer.assert_called_once()
+ self.assertEqual(writer.call_args.kwargs["audio_fps"], 48000)
+ self.assertEqual(source_video.close_calls, 1)
+ self.assertEqual(voice_source.close_calls, 1)
+ self.assertEqual(bgm_source.close_calls, 1)
+ self.assertEqual(final_video.close_calls, 1)
+
+ def test_generate_video_keeps_output_and_reports_failed_bgm_mix(self):
+ """BGM 打开失败时仍应只写一次无 BGM 视频,并返回 False。"""
+ params = vd.VideoParams(
+ video_subject="test",
+ subtitle_enabled=False,
+ bgm_type="sonilo",
+ )
+ source_video = _FakeMoviePyClip()
+ voice_source = _FakeMoviePyClip()
+ final_video = _FakeMoviePyClip()
+ source_video.with_audio_result = final_video
+
+ with (
+ patch.object(
+ vd, "_open_video_clip_quietly", return_value=source_video
+ ),
+ patch.object(
+ vd,
+ "AudioFileClip",
+ side_effect=[voice_source, RuntimeError("invalid BGM")],
+ ),
+ patch.object(vd, "CompositeAudioClip") as composite_audio,
+ patch.object(vd, "_write_videofile_with_codec_fallback") as writer,
+ patch.object(vd, "_get_configured_video_codec", return_value="libx264"),
+ patch.object(vd.logger, "exception") as log_exception,
+ ):
+ result = vd.generate_video(
+ video_path="combined.mp4",
+ audio_path="voice.mp3",
+ subtitle_path="",
+ output_file="final.mp4",
+ params=params,
+ bgm_file_override="broken.m4a",
+ )
+
+ self.assertFalse(result)
+ writer.assert_called_once()
+ composite_audio.assert_not_called()
+ log_exception.assert_called_once()
+ self.assertEqual(source_video.close_calls, 1)
+ self.assertEqual(voice_source.close_calls, 1)
+ self.assertEqual(final_video.close_calls, 1)
+
+ def test_generate_video_skips_every_bgm_source_when_volume_is_zero(self):
+ """0 音量必须在解析文件前统一短路当前来源和未来提供商。"""
+ test_cases = [
+ ("random", None),
+ ("custom", None),
+ ("sonilo", "sonilo.m4a"),
+ ("future_provider", "future-provider.wav"),
+ ]
+ for bgm_type, bgm_override in test_cases:
+ with self.subTest(bgm_type=bgm_type):
+ params = vd.VideoParams(
+ video_subject="test",
+ subtitle_enabled=False,
+ bgm_type=bgm_type,
+ bgm_file="missing-background.mp3",
+ bgm_volume=0.0,
+ )
+ source_video = _FakeMoviePyClip()
+ voice_source = _FakeMoviePyClip()
+ final_video = _FakeMoviePyClip()
+ source_video.with_audio_result = final_video
+
+ with (
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ return_value=source_video,
+ ),
+ patch.object(
+ vd, "AudioFileClip", return_value=voice_source
+ ) as audio_file_clip,
+ patch.object(vd, "get_bgm_file") as get_bgm_file,
+ patch.object(vd, "CompositeAudioClip") as composite_audio,
+ patch.object(
+ vd, "_write_videofile_with_codec_fallback"
+ ) as writer,
+ patch.object(
+ vd, "_get_configured_video_codec", return_value="libx264"
+ ),
+ ):
+ result = vd.generate_video(
+ video_path="combined.mp4",
+ audio_path="voice.mp3",
+ subtitle_path="",
+ output_file="final.mp4",
+ params=params,
+ bgm_file_override=bgm_override,
+ )
+
+ self.assertTrue(result)
+ audio_file_clip.assert_called_once_with("voice.mp3")
+ get_bgm_file.assert_not_called()
+ composite_audio.assert_not_called()
+ writer.assert_called_once()
+ self.assertEqual(source_video.close_calls, 1)
+ self.assertEqual(voice_source.close_calls, 1)
+ self.assertEqual(final_video.close_calls, 1)
+
+ def test_generate_video_chooses_looping_by_bgm_file_source(self):
+ """默认曲库需要循环,任务层提供的时长适配文件不应依赖提供商名称。"""
+ test_cases = [
+ ("random", None, True),
+ ("custom", None, True),
+ ("sonilo", "sonilo.m4a", False),
+ ("future_provider", "future-provider.wav", False),
+ ]
+ for bgm_type, bgm_override, should_loop in test_cases:
+ with self.subTest(bgm_type=bgm_type, bgm_override=bgm_override):
+ params = vd.VideoParams(
+ video_subject="test",
+ subtitle_enabled=False,
+ bgm_type=bgm_type,
+ bgm_file="library.mp3",
+ bgm_volume=0.2,
+ )
+ source_video = _FakeMoviePyClip()
+ voice_source = _FakeMoviePyClip()
+ bgm_source = _FakeMoviePyClip()
+ mixed_audio = _FakeMoviePyClip()
+ final_video = _FakeMoviePyClip()
+ source_video.with_audio_result = final_video
+
+ with (
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ return_value=source_video,
+ ),
+ patch.object(
+ vd,
+ "AudioFileClip",
+ side_effect=[voice_source, bgm_source],
+ ),
+ patch.object(vd, "get_bgm_file", return_value="library.mp3"),
+ patch.object(vd, "CompositeAudioClip", return_value=mixed_audio),
+ patch.object(vd.afx, "AudioLoop") as audio_loop,
+ patch.object(vd, "_write_videofile_with_codec_fallback"),
+ patch.object(
+ vd, "_get_configured_video_codec", return_value="libx264"
+ ),
+ ):
+ result = vd.generate_video(
+ video_path="combined.mp4",
+ audio_path="voice.mp3",
+ subtitle_path="",
+ output_file="final.mp4",
+ params=params,
+ bgm_file_override=bgm_override,
+ )
+
+ self.assertTrue(result)
+ if should_loop:
+ audio_loop.assert_called_once_with(duration=source_video.duration)
+ else:
+ audio_loop.assert_not_called()
+
def test_preprocess_video(self):
if not os.path.exists(self.test_img_path):
self.fail(f"test image not found: {self.test_img_path}")
diff --git a/test/services/test_webui_bgm.py b/test/services/test_webui_bgm.py
index 95a112ea..cbe25c0b 100644
--- a/test/services/test_webui_bgm.py
+++ b/test/services/test_webui_bgm.py
@@ -11,7 +11,8 @@ from streamlit.testing.v1 import AppTest
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
-from app.services import bgm
+from app.config import config
+from app.services import bgm, sonilo
ROOT_DIR = Path(__file__).parent.parent.parent
@@ -65,6 +66,14 @@ class TestWebuiBackgroundMusic(unittest.TestCase):
source_select.set_value("custom").run()
return app
+ def _open_sonilo_bgm_panel(self, locale):
+ app = AppTest.from_file(str(WEBUI_MAIN), default_timeout=30)
+ app.session_state["ui_language"] = locale
+ app.run()
+ source_select = self._widget_by_key(app.selectbox, "bgm_type_select")
+ source_select.set_value("sonilo").run()
+ return app
+
def _uploader(self, app):
return self._widget_by_key(app.file_uploader, "custom_bgm_uploader")
@@ -133,6 +142,33 @@ class TestWebuiBackgroundMusic(unittest.TestCase):
)
self.assertEqual(len(app.get("audio")), 1)
+ def test_zero_volume_defers_custom_upload_validation_until_enabled(self):
+ """0 音量保留上传选择,但必须等重新启用 BGM 后才校验和预览。"""
+ app = self._open_custom_bgm_panel("en")
+ self._volume_select(app).set_value(0.0).run()
+
+ with patch.object(bgm, "validate_bgm_upload") as validation:
+ self._uploader(app).set_value(
+ ("deferred.wav", _valid_wav_bytes(), "audio/wav")
+ ).run()
+
+ validation.assert_not_called()
+ self.assertEqual([str(item.value) for item in app.exception], [])
+ self.assertEqual([item.value for item in app.error], [])
+ self.assertFalse(any("deferred.wav" in item.value for item in app.info))
+ self.assertEqual(len(app.get("audio")), 0)
+
+ # 文件仍保留在 Streamlit 会话中。用户调高音量后,同一次 rerun 应自动
+ # 完成校验并显示播放器,不需要重新选择文件。
+ with patch.object(bgm, "validate_bgm_upload") as validation:
+ self._volume_select(app).set_value(0.2).run()
+
+ validation.assert_called_once()
+ self.assertEqual([str(item.value) for item in app.exception], [])
+ self.assertEqual([item.value for item in app.error], [])
+ self.assertTrue(any("deferred.wav" in item.value for item in app.info))
+ self.assertEqual(len(app.get("audio")), 1)
+
def test_service_failure_is_not_reported_as_invalid_user_audio(self):
for locale in TEST_LOCALES:
with self.subTest(locale=locale):
@@ -157,6 +193,74 @@ class TestWebuiBackgroundMusic(unittest.TestCase):
)
self.assertEqual(len(app.get("audio")), 0)
+ def test_sonilo_source_shows_masked_prefilled_key_and_optional_prompt(self):
+ """选择 Sonilo 后应回填本机 Key,但控件必须保持密码显示模式。"""
+ for locale in TEST_LOCALES:
+ with self.subTest(locale=locale):
+ test_config = dict(config.app, sonilo_api_key="saved-test-key")
+ with (
+ patch.object(config, "app", test_config),
+ patch.object(config, "save_config"),
+ ):
+ app = self._open_sonilo_bgm_panel(locale)
+
+ api_key_input = self._widget_by_key(
+ app.text_input, "sonilo_api_key_input"
+ )
+ prompt_input = self._widget_by_key(
+ app.text_input, "sonilo_bgm_prompt_input"
+ )
+ self.assertEqual(api_key_input.value, "saved-test-key")
+ self.assertEqual(
+ api_key_input.label,
+ self._translation(locale, "Sonilo API Key"),
+ )
+ self.assertIn(
+ "https://platform.sonilo.com/", api_key_input.label
+ )
+ # AppTest 的 element.type 表示控件种类(text_input);密码模式
+ # 保存在底层 protobuf 枚举中,必须检查该字段才能验证真实渲染。
+ self.assertEqual(
+ api_key_input.proto.type, api_key_input.proto.PASSWORD
+ )
+ self.assertEqual(prompt_input.value, "")
+ self.assertEqual([str(item.value) for item in app.exception], [])
+
+ def test_sonilo_connection_button_reports_success(self):
+ test_config = dict(config.app, sonilo_api_key="saved-test-key")
+ with (
+ patch.object(config, "app", test_config),
+ patch.object(config, "save_config"),
+ patch.object(sonilo, "test_connection", return_value={}) as connection,
+ ):
+ app = self._open_sonilo_bgm_panel("en")
+ button = self._widget_by_key(
+ app.button, "test_sonilo_connection_button"
+ )
+ button.click().run()
+
+ connection.assert_called_once_with()
+ self.assertIn(
+ self._translation("en", "Sonilo Connection Test Succeeded"),
+ [item.value for item in app.success],
+ )
+
+ def test_zero_volume_does_not_require_sonilo_key(self):
+ """Sonilo 音量为 0 时,WebUI 不应继续显示 API Key 必填警告。"""
+ test_config = dict(config.app, sonilo_api_key="")
+ required_warning = self._translation("en", "Sonilo API Key Required")
+ with (
+ patch.object(config, "app", test_config),
+ patch.object(config, "save_config"),
+ patch.object(sonilo, "is_enabled", return_value=False),
+ ):
+ app = self._open_sonilo_bgm_panel("en")
+ self.assertIn(required_warning, [item.value for item in app.warning])
+ self._volume_select(app).set_value(0.0).run()
+
+ self.assertNotIn(required_warning, [item.value for item in app.warning])
+ self.assertEqual([str(item.value) for item in app.exception], [])
+
if __name__ == "__main__":
unittest.main()
diff --git a/webui/Main.py b/webui/Main.py
index 6c14ab9f..c062c82d 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -40,6 +40,7 @@ from app.models.schema import (
)
from app.services import bgm as bgm_service
from app.services import cache_manager, llm, video, voice
+from app.services import sonilo as sonilo_service
from app.services import state as sm
from app.services import task as tm
from app.utils import utils
@@ -940,6 +941,9 @@ def _apply_pending_task_restore():
_set_stable_widget_value("bgm_type_select", bgm_type)
_set_stable_widget_value("bgm_volume_select", params.get("bgm_volume", 0.2))
st.session_state["custom_bgm_file_input"] = params.get("bgm_file") or ""
+ st.session_state["sonilo_bgm_prompt_input"] = (
+ params.get("sonilo_bgm_prompt") or ""
+ )
# 字幕设置。对旧任务中的越界数值做最小限幅,避免 Slider 无法初始化。
st.session_state["subtitle_enabled_checkbox"] = bool(
@@ -2336,6 +2340,7 @@ def _render_background_music_settings(params):
(tr("No Background Music"), ""),
(tr("Random Background Music"), "random"),
(tr("Custom Background Music"), "custom"),
+ (tr("Sonilo Background Music"), "sonilo"),
]
selected_bgm_type = stable_selectbox(
tr("Background Music Source"),
@@ -2345,6 +2350,17 @@ def _render_background_music_settings(params):
format_func=lambda value: dict((v, label) for label, v in bgm_options)[value],
)
params.bgm_type = selected_bgm_type
+ params.bgm_volume = stable_selectbox(
+ tr("Background Music Volume"),
+ options=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
+ default_value=0.2,
+ key="bgm_volume_select",
+ format_func=lambda value: f"{int(value * 100)}%",
+ disabled=not params.bgm_type,
+ )
+ bgm_enabled = bgm_service.should_use_bgm(
+ params.bgm_type, params.bgm_volume
+ )
if params.bgm_type == "custom":
uploaded_bgm_file = st.file_uploader(
@@ -2360,7 +2376,7 @@ def _render_background_music_settings(params):
# 30MB 硬限制保持一致,避免界面允许选择、提交时才被服务端拒绝。
max_upload_size=bgm_service.MAX_BGM_UPLOAD_BYTES // (1024 * 1024),
)
- if uploaded_bgm_file is not None:
+ if uploaded_bgm_file is not None and bgm_enabled:
try:
safe_name = bgm_service.sanitize_upload_filename(
uploaded_bgm_file.name
@@ -2447,18 +2463,59 @@ def _render_background_music_settings(params):
key="custom_bgm_file_input",
disabled=uploaded_bgm_file is not None,
)
- if uploaded_bgm_file is None and custom_bgm_file:
+ if uploaded_bgm_file is None and custom_bgm_file and bgm_enabled:
# 文件名由服务层映射到 storage/bgm 或 resource/songs 后校验,
# UI 不接受两个白名单目录之外的任意路径。
params.bgm_file = custom_bgm_file.strip()
- params.bgm_volume = stable_selectbox(
- tr("Background Music Volume"),
- options=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
- default_value=0.2,
- key="bgm_volume_select",
- format_func=lambda value: f"{int(value * 100)}%",
- disabled=not params.bgm_type,
- )
+ elif not bgm_enabled:
+ # 上传控件继续保留用户已选择的文件,调高音量后的下一次 rerun 会自动
+ # 完整校验;当前任务参数必须清空,避免 0 音量任务保存或解析该文件。
+ params.bgm_file = ""
+
+ if params.bgm_type == "sonilo":
+ configured_key = str(config.app.get("sonilo_api_key", "") or "").strip()
+ effective_key = configured_key or os.getenv("SONILO_API_KEY", "").strip()
+ entered_key = st.text_input(
+ tr("Sonilo API Key"),
+ value=effective_key,
+ type="password",
+ key="sonilo_api_key_input",
+ help=tr("Sonilo API Key Help"),
+ ).strip()
+ # 用户要求已配置的 Key 直接回填到密码输入框。配置值优先于环境变量;
+ # 仅当用户确实修改输入或本来就使用配置时写回,避免把环境变量中的 Key
+ # 在无操作的情况下复制进 config.toml。
+ if configured_key or entered_key != effective_key:
+ config.app["sonilo_api_key"] = entered_key
+
+ params.sonilo_bgm_prompt = st.text_input(
+ tr("Sonilo Music Prompt"),
+ key="sonilo_bgm_prompt_input",
+ max_chars=sonilo_service.MAX_PROMPT_LENGTH,
+ help=tr("Sonilo Music Prompt Help"),
+ ).strip()
+ if params.video_count > 1:
+ st.warning(tr("Sonilo Multiple Videos Warning"))
+ if st.button(
+ tr("Test Sonilo Connection"),
+ key="test_sonilo_connection_button",
+ use_container_width=True,
+ ):
+ try:
+ sonilo_service.test_connection()
+ except sonilo_service.SoniloError as exc:
+ logger.warning(f"Sonilo connection test failed: {exc}")
+ st.error(tr("Sonilo Connection Test Failed").format(error=str(exc)))
+ else:
+ st.success(tr("Sonilo Connection Test Succeeded"))
+ if (
+ params.bgm_type == "sonilo"
+ and bgm_enabled
+ and not sonilo_service.is_enabled()
+ ):
+ # 音量为 0 时任务层不会生成或混合 Sonilo 配乐,因此无需提示 Key;
+ # 该判断与任务入口共用服务层规则,避免界面提示和实际执行条件分叉。
+ st.warning(tr("Sonilo API Key Required"))
return uploaded_bgm_file
@@ -3178,6 +3235,15 @@ def _render_generation_controls(
st.error(tr("Please Enter the Coverr API Key"))
st.stop()
+ if (
+ params.bgm_type == "sonilo"
+ and bgm_service.should_use_bgm(params.bgm_type, params.bgm_volume)
+ and not sonilo_service.is_enabled()
+ ):
+ _remove_active_generation_task(task_id)
+ st.error(tr("Sonilo API Key Required"))
+ st.stop()
+
if params.video_source == "local" and not has_local_materials:
# 本地素材为空时继续执行会先产生 TTS/字幕,最后才在素材预处理阶段失败。
# 在任务启动前拦截,可以避免无意义的 API 调用和中间文件。
@@ -3199,7 +3265,9 @@ def _render_generation_controls(
st.error(tr("Task Restore Custom Audio Warning"))
st.stop()
- if uploaded_bgm_file:
+ if uploaded_bgm_file and bgm_service.should_use_bgm(
+ params.bgm_type, params.bgm_volume
+ ):
try:
saved_bgm_name = bgm_service.save_bgm_upload(
uploaded_bgm_file.name, uploaded_bgm_file
@@ -3217,6 +3285,10 @@ def _render_generation_controls(
# 保存成功后只把文件名写入任务参数。视频服务会在两个 BGM 白名单
# 目录中重新解析,避免把服务器绝对路径持久化或展示给用户。
params.bgm_file = saved_bgm_name
+ elif uploaded_bgm_file:
+ # 0 音量时视频服务不会使用任何 BGM,因此不再把已经预览的上传文件
+ # 持久化到 storage。用户之后调高音量时可直接再次点击生成完成保存。
+ params.bgm_file = ""
if uploaded_audio_file:
task_dir = utils.task_dir(task_id)
@@ -3306,6 +3378,18 @@ def _render_generation_controls(
video_files = result.get("videos", [])
st.success(tr("Video Generation Completed"))
+ for warning in result.get("warnings") or []:
+ if (
+ isinstance(warning, Mapping)
+ and warning.get("code") == "sonilo_bgm_failed"
+ ):
+ st.warning(
+ tr("Sonilo BGM Fallback Warning").format(
+ index=warning.get("video_index", "")
+ )
+ )
+ else:
+ st.warning(str(warning))
try:
if video_files:
player_cols = st.columns(len(video_files) * 2 + 1)
diff --git a/webui/i18n/de.json b/webui/i18n/de.json
index 41e19a9a..41b057e9 100644
--- a/webui/i18n/de.json
+++ b/webui/i18n/de.json
@@ -108,6 +108,17 @@
"No Background Music": "Ohne Hintergrundmusik",
"Random Background Music": "Zufällig erzeugte Hintergrundmusik",
"Custom Background Music": "Benutzerdefinierte Hintergrundmusik",
+ "Sonilo Background Music": "Sonilo videobasierte Musik",
+ "Sonilo API Key": "Sonilo API-Schlüssel ([Schlüssel abrufen](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Erstellen Sie einen Schlüssel auf platform.sonilo.com. Der Wert wird maskiert und lokal in config.toml gespeichert.",
+ "Sonilo Music Prompt": "Musikstil (optional)",
+ "Sonilo Music Prompt Help": "Optionale Stil- oder Stimmungsvorgabe mit bis zu 2000 Zeichen. Leer lassen, damit Sonilo das Video automatisch abstimmt.",
+ "Sonilo Multiple Videos Warning": "Sonilo erzeugt für jedes Ausgabevideo separate Musik; mehrere Videos verbrauchen mehrere Generierungen.",
+ "Sonilo API Key Required": "Geben Sie einen Sonilo API-Schlüssel ein, um Sonilo-Hintergrundmusik zu verwenden.",
+ "Test Sonilo Connection": "Sonilo-Verbindung testen",
+ "Sonilo Connection Test Succeeded": "Sonilo-Verbindung erfolgreich.",
+ "Sonilo Connection Test Failed": "Sonilo-Verbindung fehlgeschlagen: {error}",
+ "Sonilo BGM Fallback Warning": "Video {index} wurde ohne Sonilo-Hintergrundmusik erstellt, weil die Musikanfrage fehlgeschlagen ist.",
"Custom Background Music File": "Pfad der eigenen Musik",
"Background Music Volume": "Hintergrundmusik-Lautstärke",
"Subtitle Settings": "**Untertitel-Einstellungen**",
diff --git a/webui/i18n/en.json b/webui/i18n/en.json
index 59f01d1b..a76a9603 100644
--- a/webui/i18n/en.json
+++ b/webui/i18n/en.json
@@ -109,6 +109,17 @@
"No Background Music": "No Background Music",
"Random Background Music": "Random Background Music",
"Custom Background Music": "Custom Background Music",
+ "Sonilo Background Music": "Sonilo Video-Matched Music",
+ "Sonilo API Key": "Sonilo API Key ([Get API Key](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Create a key at platform.sonilo.com. The value is masked and saved in your local config.toml.",
+ "Sonilo Music Prompt": "Music Style (Optional)",
+ "Sonilo Music Prompt Help": "Optional style or mood guidance, up to 2000 characters. Leave empty to let Sonilo match the video automatically.",
+ "Sonilo Multiple Videos Warning": "Sonilo generates music separately for each output video, so multiple videos consume multiple generations.",
+ "Sonilo API Key Required": "Enter a Sonilo API Key to use Sonilo background music.",
+ "Test Sonilo Connection": "Test Sonilo Connection",
+ "Sonilo Connection Test Succeeded": "Sonilo connection succeeded.",
+ "Sonilo Connection Test Failed": "Sonilo connection failed: {error}",
+ "Sonilo BGM Fallback Warning": "Video {index} was generated without Sonilo background music because the music request failed.",
"Custom Background Music File": "Custom Music Path",
"Upload Background Music": "Upload Background Music",
"Upload Background Music Help": "Upload an MP3, M4A, AAC, WAV, FLAC, OGG, OPUS, or WMA file up to 30 MB. It will be stored persistently when video generation starts.",
diff --git a/webui/i18n/es.json b/webui/i18n/es.json
index f13a7501..0f25a4b0 100644
--- a/webui/i18n/es.json
+++ b/webui/i18n/es.json
@@ -105,6 +105,17 @@
"No Background Music": "Sin música de fondo",
"Random Background Music": "Música de fondo aleatoria",
"Custom Background Music": "Música de fondo personalizada",
+ "Sonilo Background Music": "Música Sonilo adaptada al video",
+ "Sonilo API Key": "Clave API de Sonilo ([Obtener clave](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Crea una clave en platform.sonilo.com. El valor se oculta y se guarda en tu config.toml local.",
+ "Sonilo Music Prompt": "Estilo musical (opcional)",
+ "Sonilo Music Prompt Help": "Indicación opcional de estilo o emoción, hasta 2000 caracteres. Déjalo vacío para que Sonilo se adapte al video.",
+ "Sonilo Multiple Videos Warning": "Sonilo genera música por separado para cada video, por lo que varios videos consumen varias generaciones.",
+ "Sonilo API Key Required": "Introduce una clave API de Sonilo para usar su música de fondo.",
+ "Test Sonilo Connection": "Probar conexión con Sonilo",
+ "Sonilo Connection Test Succeeded": "Conexión con Sonilo correcta.",
+ "Sonilo Connection Test Failed": "Falló la conexión con Sonilo: {error}",
+ "Sonilo BGM Fallback Warning": "El video {index} se generó sin música de Sonilo porque falló la solicitud de música.",
"Custom Background Music File": "Ruta de música personalizada",
"Background Music Volume": "Volumen de música de fondo",
"Subtitle Settings": "**Configuración de subtítulos**",
diff --git a/webui/i18n/id.json b/webui/i18n/id.json
index 112270eb..ec560c7c 100644
--- a/webui/i18n/id.json
+++ b/webui/i18n/id.json
@@ -105,6 +105,17 @@
"No Background Music": "Tidak Ada Musik Latar",
"Random Background Music": "Musik Latar Acak",
"Custom Background Music": "Musik Latar Kustom",
+ "Sonilo Background Music": "Musik Sonilo yang sesuai video",
+ "Sonilo API Key": "Kunci API Sonilo ([Dapatkan kunci](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Buat kunci di platform.sonilo.com. Nilai disamarkan dan disimpan di config.toml lokal.",
+ "Sonilo Music Prompt": "Gaya musik (opsional)",
+ "Sonilo Music Prompt Help": "Panduan gaya atau suasana opsional, maksimal 2000 karakter. Kosongkan agar Sonilo menyesuaikan video otomatis.",
+ "Sonilo Multiple Videos Warning": "Sonilo membuat musik terpisah untuk tiap video, sehingga beberapa video memakai beberapa generasi.",
+ "Sonilo API Key Required": "Masukkan Kunci API Sonilo untuk menggunakan musik latar Sonilo.",
+ "Test Sonilo Connection": "Uji koneksi Sonilo",
+ "Sonilo Connection Test Succeeded": "Koneksi Sonilo berhasil.",
+ "Sonilo Connection Test Failed": "Koneksi Sonilo gagal: {error}",
+ "Sonilo BGM Fallback Warning": "Video {index} dibuat tanpa musik Sonilo karena permintaan musik gagal.",
"Custom Background Music File": "Path Musik Kustom",
"Background Music Volume": "Volume Musik Latar",
"Subtitle Settings": "**Pengaturan Subtitle**",
diff --git a/webui/i18n/pt.json b/webui/i18n/pt.json
index 5420eaef..9a72bcac 100644
--- a/webui/i18n/pt.json
+++ b/webui/i18n/pt.json
@@ -103,6 +103,17 @@
"No Background Music": "Sem Música de Fundo",
"Random Background Music": "Música de Fundo Aleatória",
"Custom Background Music": "Música de Fundo Personalizada",
+ "Sonilo Background Music": "Música Sonilo adaptada ao vídeo",
+ "Sonilo API Key": "Chave de API Sonilo ([Obter chave](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Crie uma chave em platform.sonilo.com. O valor fica oculto e é salvo no config.toml local.",
+ "Sonilo Music Prompt": "Estilo musical (opcional)",
+ "Sonilo Music Prompt Help": "Orientação opcional de estilo ou clima, até 2000 caracteres. Deixe vazio para o Sonilo combinar automaticamente com o vídeo.",
+ "Sonilo Multiple Videos Warning": "O Sonilo gera música separada para cada vídeo; vários vídeos consomem várias gerações.",
+ "Sonilo API Key Required": "Insira uma chave de API Sonilo para usar a música de fundo Sonilo.",
+ "Test Sonilo Connection": "Testar conexão Sonilo",
+ "Sonilo Connection Test Succeeded": "Conexão Sonilo bem-sucedida.",
+ "Sonilo Connection Test Failed": "Falha na conexão Sonilo: {error}",
+ "Sonilo BGM Fallback Warning": "O vídeo {index} foi gerado sem música Sonilo porque a solicitação de música falhou.",
"Custom Background Music File": "Caminho da música personalizada",
"Background Music Volume": "Volume da música de fundo",
"Subtitle Settings": "**Configurações de Legendas**",
diff --git a/webui/i18n/ru.json b/webui/i18n/ru.json
index c91e8970..4710217b 100644
--- a/webui/i18n/ru.json
+++ b/webui/i18n/ru.json
@@ -109,6 +109,17 @@
"No Background Music": "Без фоновой музыки",
"Random Background Music": "Случайная фоновая музыка",
"Custom Background Music": "Своя фоновая музыка",
+ "Sonilo Background Music": "Музыка Sonilo под видео",
+ "Sonilo API Key": "API-ключ Sonilo ([Получить ключ](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Создайте ключ на platform.sonilo.com. Значение скрывается и сохраняется в локальном config.toml.",
+ "Sonilo Music Prompt": "Стиль музыки (необязательно)",
+ "Sonilo Music Prompt Help": "Необязательное описание стиля или настроения, до 2000 символов. Оставьте пустым для автоматического подбора.",
+ "Sonilo Multiple Videos Warning": "Sonilo создаёт музыку отдельно для каждого видео, поэтому несколько видео расходуют несколько генераций.",
+ "Sonilo API Key Required": "Введите API-ключ Sonilo для использования фоновой музыки Sonilo.",
+ "Test Sonilo Connection": "Проверить подключение Sonilo",
+ "Sonilo Connection Test Succeeded": "Подключение Sonilo успешно.",
+ "Sonilo Connection Test Failed": "Ошибка подключения Sonilo: {error}",
+ "Sonilo BGM Fallback Warning": "Видео {index} создано без музыки Sonilo из-за ошибки запроса музыки.",
"Custom Background Music File": "Введите путь к файлу фоновой музыки:",
"Upload Background Music": "Загрузить фоновую музыку",
"Upload Background Music Help": "Загрузите файл MP3, M4A, AAC, WAV, FLAC, OGG, OPUS или WMA размером до 30 МБ. Он будет сохранён при запуске создания видео.",
diff --git a/webui/i18n/tr.json b/webui/i18n/tr.json
index f1bfeac3..146892df 100644
--- a/webui/i18n/tr.json
+++ b/webui/i18n/tr.json
@@ -103,6 +103,17 @@
"No Background Music": "Arka Plan Müziği Yok",
"Random Background Music": "Rastgele Arka Plan Müziği",
"Custom Background Music": "Özel Arka Plan Müziği",
+ "Sonilo Background Music": "Videoya uyumlu Sonilo müziği",
+ "Sonilo API Key": "Sonilo API Anahtarı ([Anahtar al](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "platform.sonilo.com adresinden bir anahtar oluşturun. Değer maskelenir ve yerel config.toml dosyasına kaydedilir.",
+ "Sonilo Music Prompt": "Müzik stili (isteğe bağlı)",
+ "Sonilo Music Prompt Help": "En fazla 2000 karakterlik isteğe bağlı stil veya duygu açıklaması. Otomatik eşleştirme için boş bırakın.",
+ "Sonilo Multiple Videos Warning": "Sonilo her çıktı videosu için ayrı müzik üretir; birden fazla video birden fazla üretim harcar.",
+ "Sonilo API Key Required": "Sonilo arka plan müziğini kullanmak için API anahtarını girin.",
+ "Test Sonilo Connection": "Sonilo bağlantısını test et",
+ "Sonilo Connection Test Succeeded": "Sonilo bağlantısı başarılı.",
+ "Sonilo Connection Test Failed": "Sonilo bağlantısı başarısız: {error}",
+ "Sonilo BGM Fallback Warning": "Müzik isteği başarısız olduğu için {index}. video Sonilo arka plan müziği olmadan oluşturuldu.",
"Custom Background Music File": "Özel Müzik Yolu",
"Background Music Volume": "Arka Plan Müziği Seviyesi",
"Subtitle Settings": "**Altyazı Ayarları**",
diff --git a/webui/i18n/vi.json b/webui/i18n/vi.json
index 96efe3b9..cc899225 100644
--- a/webui/i18n/vi.json
+++ b/webui/i18n/vi.json
@@ -105,6 +105,17 @@
"No Background Music": "Không Có Âm Nhạc Nền",
"Random Background Music": "Âm Nhạc Nền Ngẫu Nhiên",
"Custom Background Music": "Âm Nhạc Nền Tùy Chỉnh",
+ "Sonilo Background Music": "Nhạc Sonilo phù hợp video",
+ "Sonilo API Key": "Khóa API Sonilo ([Lấy khóa](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "Tạo khóa tại platform.sonilo.com. Giá trị được che và lưu trong config.toml cục bộ.",
+ "Sonilo Music Prompt": "Phong cách nhạc (không bắt buộc)",
+ "Sonilo Music Prompt Help": "Mô tả phong cách hoặc cảm xúc, tối đa 2000 ký tự. Để trống để Sonilo tự động khớp video.",
+ "Sonilo Multiple Videos Warning": "Sonilo tạo nhạc riêng cho từng video, vì vậy nhiều video sẽ dùng nhiều lượt tạo.",
+ "Sonilo API Key Required": "Nhập Khóa API Sonilo để dùng nhạc nền Sonilo.",
+ "Test Sonilo Connection": "Kiểm tra kết nối Sonilo",
+ "Sonilo Connection Test Succeeded": "Kết nối Sonilo thành công.",
+ "Sonilo Connection Test Failed": "Kết nối Sonilo thất bại: {error}",
+ "Sonilo BGM Fallback Warning": "Video {index} được tạo không có nhạc Sonilo vì yêu cầu tạo nhạc thất bại.",
"Custom Background Music File": "Đường dẫn nhạc tùy chỉnh",
"Background Music Volume": "Âm lượng nhạc nền",
"Subtitle Settings": "**Cài Đặt Phụ Đề**",
diff --git a/webui/i18n/zh.json b/webui/i18n/zh.json
index 2d46147a..75f2ed17 100644
--- a/webui/i18n/zh.json
+++ b/webui/i18n/zh.json
@@ -109,6 +109,17 @@
"No Background Music": "无背景音乐",
"Random Background Music": "随机背景音乐",
"Custom Background Music": "自定义背景音乐",
+ "Sonilo Background Music": "Sonilo 视频匹配配乐",
+ "Sonilo API Key": "Sonilo API Key([点击获取](https://platform.sonilo.com/))",
+ "Sonilo API Key Help": "请前往 platform.sonilo.com 创建 Key。输入内容会以密码形式显示,并保存到本机 config.toml。",
+ "Sonilo Music Prompt": "音乐风格(可选)",
+ "Sonilo Music Prompt Help": "可以填写风格或情绪要求,最多 2000 个字符;留空时由 Sonilo 自动匹配视频。",
+ "Sonilo Multiple Videos Warning": "Sonilo 会为每条输出视频分别生成配乐,生成多条视频会消耗多次额度。",
+ "Sonilo API Key Required": "使用 Sonilo 背景音乐前,请填写 Sonilo API Key。",
+ "Test Sonilo Connection": "测试 Sonilo 连接",
+ "Sonilo Connection Test Succeeded": "Sonilo 连接成功。",
+ "Sonilo Connection Test Failed": "Sonilo 连接失败:{error}",
+ "Sonilo BGM Fallback Warning": "第 {index} 条视频的 Sonilo 配乐生成失败,已降级生成无背景音乐的视频。",
"Custom Background Music File": "请输入自定义背景音乐的文件路径",
"Upload Background Music": "上传背景音乐",
"Upload Background Music Help": "请上传不超过 30 MB 的 MP3、M4A、AAC、WAV、FLAC、OGG、OPUS 或 WMA 文件,点击生成视频时会持久化保存。",