Skip to content

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

fix(subtitle): improve animation reliability

harry0703ff64bcdf2f98
diff --git a/app/models/schema.py b/app/models/schema.py
index 293b7ad8..ed83fc88 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -53,6 +53,24 @@ class VideoFitMode(str, Enum):
contain = "contain"
+SubtitleDisplayMode = Literal["sentence", "word_by_word"]
+SubtitleAnimation = Literal["none", "pop_spring"]
+_SUBTITLE_DISPLAY_MODES = ("sentence", "word_by_word")
+_SUBTITLE_ANIMATIONS = ("none", "pop_spring")
+
+
+def _get_valid_ui_choice(key: str, allowed_values: tuple[str, ...], default: str) -> str:
+ """
+ 读取经过校验的 WebUI 枚举配置,兼容旧用户可能残留的无效值。
+
+ 请求体由 Pydantic 的 Literal 严格校验,拼写错误会返回明确的字段校验错误;
+ 配置文件则需要宽容处理,避免用户升级后因为历史手工配置错误导致整个服务
+ 无法启动。HTTP 状态码由应用统一的校验异常处理器决定,这里不绑定具体数值。
+ """
+ configured_value = config.ui.get(key, default)
+ return configured_value if configured_value in allowed_values else default
+
+
_Config = ConfigDict(
arbitrary_types_allowed=True,
# Note: ensure your key names match renamed V2 parameters if needed
@@ -122,12 +140,12 @@ class VideoParams(BaseModel):
subtitle_position: Optional[str] = config.ui.get(
"subtitle_position", "bottom"
) # top, bottom, center, custom, two_thirds_bottom
- subtitle_display_mode: Optional[str] = config.ui.get(
- "subtitle_display_mode", "sentence"
- ) # sentence, word_by_word
- subtitle_animation: Optional[str] = config.ui.get(
- "subtitle_animation", "none"
- ) # none, pop_spring
+ subtitle_display_mode: SubtitleDisplayMode = _get_valid_ui_choice(
+ "subtitle_display_mode", _SUBTITLE_DISPLAY_MODES, "sentence"
+ )
+ subtitle_animation: SubtitleAnimation = _get_valid_ui_choice(
+ "subtitle_animation", _SUBTITLE_ANIMATIONS, "none"
+ )
custom_position: float = config.ui.get("custom_position", 70.0)
font_name: Optional[str] = "STHeitiMedium.ttc"
text_fore_color: Optional[str] = "#FFFFFF"
@@ -153,10 +171,12 @@ class SubtitleRequest(BaseModel):
bgm_file: Optional[str] = ""
bgm_volume: Optional[float] = 0.2
subtitle_position: Optional[str] = config.ui.get("subtitle_position", "bottom")
- subtitle_display_mode: Optional[str] = config.ui.get(
- "subtitle_display_mode", "sentence"
+ subtitle_display_mode: SubtitleDisplayMode = _get_valid_ui_choice(
+ "subtitle_display_mode", _SUBTITLE_DISPLAY_MODES, "sentence"
+ )
+ subtitle_animation: SubtitleAnimation = _get_valid_ui_choice(
+ "subtitle_animation", _SUBTITLE_ANIMATIONS, "none"
)
- subtitle_animation: Optional[str] = config.ui.get("subtitle_animation", "none")
font_name: Optional[str] = "STHeitiMedium.ttc"
text_fore_color: Optional[str] = "#FFFFFF"
text_background_color: Union[bool, str] = False
diff --git a/app/services/video.py b/app/services/video.py
index 93ea468e..19d3fa0a 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -82,6 +82,9 @@ _MIN_MATERIAL_DIMENSION = 480
# 既能放行仅仅因为取整而略低于阈值的素材,也仍然能挡住真正的低清素材。
_MIN_DIMENSION_TOLERANCE = 10
_DEFAULT_VIDEO_CODEC = "libx264"
+_SUBTITLE_SPRING_DURATION_SECONDS = 0.18
+_MIN_SUBTITLE_SPRING_SCALE = 0.05
+_MAX_SUBTITLE_SPRING_SCALE = 1.35
_SUPPORTED_VIDEO_CODECS = (
"libx264",
"h264_nvenc",
@@ -93,6 +96,83 @@ _SUPPORTED_VIDEO_CODECS = (
_runtime_disabled_video_codecs = set()
+def _get_subtitle_spring_scale(time_seconds: float, duration_seconds: float) -> float:
+ """返回字幕弹跳动画在指定时间点使用的缩放比例。"""
+ if duration_seconds <= 0 or time_seconds >= duration_seconds:
+ return 1.0
+
+ progress = max(0.0, min(time_seconds / duration_seconds, 1.0))
+ scale = 1.0 - math.exp(-6.0 * progress) * math.cos(2.5 * math.pi * progress)
+ return max(
+ _MIN_SUBTITLE_SPRING_SCALE,
+ min(scale, _MAX_SUBTITLE_SPRING_SCALE),
+ )
+
+
+def _scale_subtitle_frame_on_canvas(frame: np.ndarray, scale: float) -> np.ndarray:
+ """
+ 在保持画布尺寸不变的前提下,围绕中心缩放字幕画面或透明蒙版。
+
+ MoviePy 将字幕颜色帧和透明蒙版分开保存。弹跳动画必须对二者使用完全
+ 相同的缩放与裁剪,否则动画首帧会把透明区域当成黑色文字轮廓合成到视频
+ 上。二维数组表示取值为 0~1 的蒙版,三维数组表示 RGB/RGBA 颜色帧。
+ """
+ if frame.ndim not in (2, 3):
+ raise ValueError("subtitle frame must be a 2D mask or 3D color frame")
+
+ height, width = frame.shape[:2]
+ scaled_width = max(1, int(round(width * scale)))
+ scaled_height = max(1, int(round(height * scale)))
+ offset = ((width - scaled_width) // 2, (height - scaled_height) // 2)
+
+ if frame.ndim == 2:
+ # MoviePy 蒙版使用 0~1 浮点数,Pillow 的 L 模式使用 0~255;转换后
+ # 再恢复原始类型和范围,确保 CompositeVideoClip 的透明度语义不变。
+ mask_image = Image.fromarray(
+ np.clip(frame * 255.0, 0, 255).astype(np.uint8)
+ )
+ resized_mask = mask_image.resize(
+ (scaled_width, scaled_height),
+ Image.Resampling.BILINEAR,
+ )
+ mask_canvas = Image.new("L", (width, height), 0)
+ mask_canvas.paste(resized_mask, offset)
+ return (np.asarray(mask_canvas) / 255.0).astype(frame.dtype, copy=False)
+
+ if frame.shape[2] not in (3, 4):
+ raise ValueError("subtitle color frame must use RGB or RGBA channels")
+ color_image = Image.fromarray(frame)
+ resized_color = color_image.resize(
+ (scaled_width, scaled_height),
+ Image.Resampling.BILINEAR,
+ )
+ background = (0, 0, 0, 0) if frame.shape[2] == 4 else (0, 0, 0)
+ color_canvas = Image.new(color_image.mode, (width, height), background)
+ color_canvas.paste(resized_color, offset)
+ return np.asarray(color_canvas).astype(frame.dtype, copy=False)
+
+
+def _apply_subtitle_spring_animation(clip, subtitle_duration: float):
+ """同时缩放字幕颜色帧与蒙版,避免弹跳动画出现黑色首帧。"""
+ animation_duration = min(
+ _SUBTITLE_SPRING_DURATION_SECONDS,
+ max(0.0, subtitle_duration),
+ )
+ if animation_duration <= 0:
+ return clip
+
+ def transform_frame(get_frame, time_seconds):
+ frame = get_frame(time_seconds)
+ scale = _get_subtitle_spring_scale(time_seconds, animation_duration)
+ if scale == 1.0:
+ return frame
+ return _scale_subtitle_frame_on_canvas(frame, scale)
+
+ # apply_to=["mask"] 是修复的关键:MoviePy 默认只处理颜色帧,旧实现因此
+ # 在每条字幕出现时短暂保留原尺寸蒙版,并显示黑色文字轮廓。
+ return clip.transform(transform_frame, apply_to=["mask"])
+
+
def _get_required_video_duration(audio_duration: float) -> float:
"""
返回视频素材拼接的目标时长。
@@ -1240,38 +1320,10 @@ def generate_video(
_clip = _clip.with_end(subtitle_item[0][1])
_clip = _clip.with_duration(duration)
- # Pop-up spring animation effect (scales from 0 -> 1.15 -> 1.0 like a spring)
+ # 弹跳动画只在用户显式选择时启用;默认 none 完全沿用原字幕渲染路径。
anim_type = getattr(params, "subtitle_animation", "none")
if anim_type in ("pop_spring", "spring", "pop"):
- anim_dur = min(0.18, duration) if duration > 0 else 0.18
-
- def _spring_scale(t_rel):
- # t_rel is relative time from clip start (0 to anim_dur)
- p = t_rel / anim_dur
- # Damped spring formula: 1 - exp(-6*p) * cos(6*pi*p)
- return 1.0 - math.exp(-6.0 * p) * math.cos(2.5 * math.pi * p)
-
- def _apply_spring_transform(get_frame, t):
- orig_frame = get_frame(t)
- if anim_dur <= 0:
- return orig_frame
- t_rel = max(0.0, min(t, anim_dur))
- if t_rel >= anim_dur:
- return orig_frame
- scale = _spring_scale(t_rel)
- scale = max(0.05, min(scale, 1.35))
- h, w = orig_frame.shape[:2]
- new_w = max(1, int(round(w * scale)))
- new_h = max(1, int(round(h * scale)))
- im = Image.fromarray(orig_frame)
- resized_im = im.resize((new_w, new_h), Image.Resampling.BILINEAR)
- canvas = Image.new("RGBA", (w, h), (0, 0, 0, 0))
- offset_x = (w - new_w) // 2
- offset_y = (h - new_h) // 2
- canvas.paste(resized_im, (offset_x, offset_y))
- return np.asarray(canvas)
-
- _clip = _clip.transform(_apply_spring_transform)
+ _clip = _apply_subtitle_spring_animation(_clip, duration)
if params.subtitle_position == "bottom":
_clip = _clip.with_position(("center", video_height * 0.95 - _clip.h))
diff --git a/test/services/test_schema.py b/test/services/test_schema.py
index cbde408e..39085c38 100644
--- a/test/services/test_schema.py
+++ b/test/services/test_schema.py
@@ -1,12 +1,14 @@
import sys
import unittest
from pathlib import Path
+from unittest.mock import patch
from pydantic import ValidationError
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
-from app.models.schema import VideoAspect, VideoFitMode, VideoParams
+from app.models import schema
+from app.models.schema import SubtitleRequest, VideoAspect, VideoFitMode, VideoParams
class TestVideoAspect(unittest.TestCase):
@@ -50,6 +52,67 @@ class TestVideoParams(unittest.TestCase):
self.assertEqual(params.video_clip_duration, 1)
self.assertEqual(params.video_count, 1)
+ def test_subtitle_modes_accept_only_supported_api_values(self):
+ """新增字幕参数必须拒绝拼写错误,避免请求成功后静默降级。"""
+ params = VideoParams(
+ video_subject="Coffee",
+ subtitle_display_mode="word_by_word",
+ subtitle_animation="pop_spring",
+ )
+ request = SubtitleRequest(
+ video_script="Coffee",
+ subtitle_display_mode="word_by_word",
+ subtitle_animation="pop_spring",
+ )
+
+ self.assertEqual(params.subtitle_display_mode, "word_by_word")
+ self.assertEqual(params.subtitle_animation, "pop_spring")
+ self.assertEqual(request.subtitle_display_mode, "word_by_word")
+ self.assertEqual(request.subtitle_animation, "pop_spring")
+
+ invalid_cases = (
+ ("subtitle_display_mode", "word-by-word"),
+ ("subtitle_display_mode", "invalid"),
+ ("subtitle_display_mode", None),
+ ("subtitle_animation", "pop-spring"),
+ ("subtitle_animation", "invalid"),
+ ("subtitle_animation", None),
+ )
+ for field_name, value in invalid_cases:
+ with self.subTest(model="VideoParams", field=field_name, value=value):
+ with self.assertRaises(ValidationError):
+ VideoParams(video_subject="Coffee", **{field_name: value})
+ with self.subTest(model="SubtitleRequest", field=field_name, value=value):
+ with self.assertRaises(ValidationError):
+ SubtitleRequest(video_script="Coffee", **{field_name: value})
+
+ def test_invalid_saved_subtitle_mode_falls_back_during_upgrade(self):
+ """旧配置包含无效值时应回退默认值,而不是阻止服务启动。"""
+ with patch.object(
+ schema.config,
+ "ui",
+ {
+ "subtitle_display_mode": "word-by-word",
+ "subtitle_animation": "pop-spring",
+ },
+ ):
+ self.assertEqual(
+ schema._get_valid_ui_choice(
+ "subtitle_display_mode",
+ schema._SUBTITLE_DISPLAY_MODES,
+ "sentence",
+ ),
+ "sentence",
+ )
+ self.assertEqual(
+ schema._get_valid_ui_choice(
+ "subtitle_animation",
+ schema._SUBTITLE_ANIMATIONS,
+ "none",
+ ),
+ "none",
+ )
+
if __name__ == "__main__":
unittest.main()
diff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.py
index 0595bb7a..7b09ed73 100644
--- a/test/services/test_subtitle.py
+++ b/test/services/test_subtitle.py
@@ -81,6 +81,45 @@ class TestSubtitleService(unittest.TestCase):
self.assertEqual([item[2] for item in items], ["Hello world", "Again"])
+ def test_create_word_level_writes_each_whisper_word_with_its_timing(self):
+ """逐词模式应保留 Whisper 的每个词及其独立起止时间。"""
+ transcribe_kwargs = {}
+
+ class _FakeWhisperModel:
+ def __init__(self, **_kwargs):
+ pass
+
+ def transcribe(self, _audio_file, **kwargs):
+ transcribe_kwargs.update(kwargs)
+ words = [
+ SimpleNamespace(start=0.1, end=0.4, word="Hello"),
+ SimpleNamespace(start=0.4, end=0.8, word=" world"),
+ ]
+ segment = SimpleNamespace(start=0.1, end=0.8, words=words)
+ info = SimpleNamespace(language="en", language_probability=0.99)
+ return [segment], info
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "word-level.srt"
+ with patch.object(subtitle, "model", None), patch.object(
+ subtitle,
+ "WhisperModel",
+ _FakeWhisperModel,
+ ):
+ subtitle.create(
+ "audio.mp3",
+ str(subtitle_file),
+ word_level=True,
+ )
+
+ items = subtitle.file_to_subtitles(str(subtitle_file))
+
+ self.assertEqual([item[2] for item in items], ["Hello", "world"])
+ self.assertIs(transcribe_kwargs["word_timestamps"], True)
+ self.assertIs(transcribe_kwargs["vad_filter"], True)
+ self.assertIn("00:00:00,100 --> 00:00:00,400", items[0][1])
+ self.assertIn("00:00:00,400 --> 00:00:00,800", items[1][1])
+
def test_correct_ignores_markdown_separator_lines(self):
"""
Whisper fallback 校正阶段也必须忽略 `---` 这类不可发声脚本行。
diff --git a/test/services/test_task.py b/test/services/test_task.py
index fd7d7771..6d0c491f 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -987,9 +987,12 @@ class TestTaskService(unittest.TestCase):
video_subject="custom audio",
video_script="Hello world.",
subtitle_enabled=True,
+ # 测试整句校正路径时必须显式指定模式,不能继承开发机 WebUI 偏好。
+ subtitle_display_mode="sentence",
)
- def fake_whisper_create(audio_file, subtitle_file):
+ def fake_whisper_create(audio_file, subtitle_file, word_level=False):
+ self.assertFalse(word_level)
Path(subtitle_file).write_text(
"1\n00:00:00,000 --> 00:00:01,000\nHello world.\n\n",
encoding="utf-8",
@@ -1019,12 +1022,69 @@ class TestTaskService(unittest.TestCase):
self.assertTrue(subtitle_path.endswith("subtitle.srt"))
create.assert_called_once_with(
- audio_file=audio_file, subtitle_file=subtitle_path
+ audio_file=audio_file,
+ subtitle_file=subtitle_path,
+ word_level=False,
)
correct.assert_called_once_with(
subtitle_file=subtitle_path, video_script="Hello world."
)
+ def test_generate_subtitle_uses_whisper_word_timing_without_correction(self):
+ """
+ 逐词模式必须把 word_level 传给 Whisper,并跳过按整句文案纠正。
+
+ 若继续执行 correct(),刚生成的逐词条目会被重新聚合,界面虽然选择
+ 逐词显示,最终视频却仍会按句显示。
+ """
+ task_id = "test-custom-audio-whisper-word-subtitle"
+ task_dir = utils.task_dir(task_id)
+ audio_file = os.path.join(task_dir, "custom-audio.mp3")
+ Path(audio_file).write_bytes(b"fake audio")
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="Hello world.",
+ subtitle_enabled=True,
+ subtitle_display_mode="word_by_word",
+ )
+
+ def fake_whisper_create(audio_file, subtitle_file, word_level=False):
+ self.assertTrue(word_level)
+ Path(subtitle_file).write_text(
+ "1\n00:00:00,000 --> 00:00:00,500\nHello\n\n",
+ encoding="utf-8",
+ )
+
+ try:
+ with (
+ patch.object(
+ tm.config,
+ "app",
+ dict(tm.config.app, subtitle_provider="whisper"),
+ ),
+ patch.object(
+ tm.subtitle, "create", side_effect=fake_whisper_create
+ ) as create,
+ patch.object(tm.subtitle, "correct") as correct,
+ ):
+ subtitle_path = tm.generate_subtitle(
+ task_id=task_id,
+ params=params,
+ video_script="Hello world.",
+ sub_maker=None,
+ audio_file=audio_file,
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertTrue(subtitle_path.endswith("subtitle.srt"))
+ create.assert_called_once_with(
+ audio_file=audio_file,
+ subtitle_file=subtitle_path,
+ word_level=True,
+ )
+ correct.assert_not_called()
+
def test_generate_subtitle_skips_edge_provider_without_sub_maker(self):
"""
Edge 字幕依赖 TTS 返回的 sub_maker 时间轴。
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 2176fa1e..20f8d634 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -63,6 +63,60 @@ class TestVideoService(unittest.TestCase):
vd._runtime_disabled_video_codecs.clear()
vd._ffmpeg_encoder_exists.cache_clear()
+ def test_subtitle_spring_animation_keeps_color_and_mask_aligned(self):
+ """
+ 弹跳动画必须同步缩放颜色帧和透明蒙版。
+
+ 旧实现只缩放颜色帧,首帧仍使用原尺寸蒙版,合成后会短暂出现黑色
+ 文字轮廓。使用纯白画面和完整蒙版可以精确比较二者的有效像素区域。
+ """
+ color_frame = vd.np.full((20, 30, 3), 255, dtype=vd.np.uint8)
+ mask_frame = vd.np.ones((20, 30), dtype=float)
+ clip = (
+ ImageClip(color_frame)
+ .with_mask(ImageClip(mask_frame, is_mask=True))
+ .with_duration(1)
+ )
+ animated = vd._apply_subtitle_spring_animation(clip, 1)
+
+ try:
+ initial_color = vd.np.any(animated.get_frame(0) > 0, axis=2)
+ initial_mask = animated.mask.get_frame(0) > 0
+ vd.np.testing.assert_array_equal(initial_color, initial_mask)
+ self.assertLess(initial_color.sum(), color_frame.shape[0] * color_frame.shape[1])
+
+ # 动画结束后必须精确恢复原始尺寸,避免长字幕持续模糊或缩放。
+ settled_color = animated.get_frame(
+ vd._SUBTITLE_SPRING_DURATION_SECONDS
+ )
+ settled_mask = animated.mask.get_frame(
+ vd._SUBTITLE_SPRING_DURATION_SECONDS
+ )
+ vd.np.testing.assert_array_equal(settled_color, color_frame)
+ vd.np.testing.assert_array_equal(settled_mask, mask_frame)
+ finally:
+ vd.close_clip(animated)
+ vd.close_clip(clip)
+
+ def test_subtitle_spring_scale_handles_time_boundaries(self):
+ """零时长、负时间和动画结束点都不能产生除零或非法缩放比例。"""
+ duration = vd._SUBTITLE_SPRING_DURATION_SECONDS
+
+ self.assertEqual(vd._get_subtitle_spring_scale(0, duration), 0.05)
+ self.assertEqual(vd._get_subtitle_spring_scale(-1, duration), 0.05)
+ self.assertEqual(vd._get_subtitle_spring_scale(duration, duration), 1.0)
+ self.assertEqual(vd._get_subtitle_spring_scale(1, 0), 1.0)
+
+ def test_scale_subtitle_frame_rejects_unsupported_shapes(self):
+ """异常通道或维度应明确失败,避免把损坏帧继续交给视频编码器。"""
+ with self.assertRaisesRegex(ValueError, "2D mask or 3D color"):
+ vd._scale_subtitle_frame_on_canvas(vd.np.zeros((8,)), 0.5)
+ with self.assertRaisesRegex(ValueError, "RGB or RGBA"):
+ vd._scale_subtitle_frame_on_canvas(
+ vd.np.zeros((8, 8, 2), dtype=vd.np.uint8),
+ 0.5,
+ )
+
def test_fit_clip_cover_fills_portrait_canvas_without_black_bars(self):
source_color = [17, 34, 51]
source = ImageClip(
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index 9b3f5fcd..9b65b3ad 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -1245,6 +1245,66 @@ class TestVoiceService(unittest.TestCase):
self.assertNotIn("---", subtitle_content)
self.assertNotIn("00:00:00,000 --> 00:00:00,000", subtitle_content)
+ def test_create_subtitle_word_level_preserves_edge_cue_timing(self):
+ """Edge TTS 的细粒度 cue 应逐项写入,不能再被按标点聚合。"""
+ sub_maker = SimpleNamespace(
+ cues=[
+ SimpleNamespace(
+ content="人工智能",
+ start=timedelta(seconds=0.1),
+ end=timedelta(seconds=0.8),
+ ),
+ SimpleNamespace(
+ content="正在",
+ start=timedelta(seconds=0.9),
+ end=timedelta(seconds=1.2),
+ ),
+ ]
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "word-level.srt"
+ vs.create_subtitle(
+ sub_maker=sub_maker,
+ text="人工智能正在发展。",
+ subtitle_file=str(subtitle_file),
+ word_level=True,
+ )
+ subtitle_content = subtitle_file.read_text(encoding="utf-8")
+
+ self.assertIn("00:00:00,100 --> 00:00:00,800", subtitle_content)
+ self.assertIn("人工智能", subtitle_content)
+ self.assertIn("00:00:00,900 --> 00:00:01,200", subtitle_content)
+ self.assertIn("正在", subtitle_content)
+
+ def test_create_subtitle_word_level_falls_back_to_provider_granularity(self):
+ """
+ 旧版 SubMaker 没有 cue 时,应保留语音服务返回的原始时间粒度。
+
+ ElevenLabs、Fish Audio 等服务可能只返回短语或整句时间轴。此时不能
+ 按字符平均拆分并伪造逐词精度,否则字幕会逐渐偏离真实语音。
+ """
+ sub_maker = SimpleNamespace(
+ cues=[],
+ subs=["Hello world"],
+ # 旧版 SubMaker 的 offset 使用 100 纳秒为单位的整数时间戳。
+ offset=[(2_000_000, 11_000_000)],
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "word-level-fallback.srt"
+ vs.create_subtitle(
+ sub_maker=sub_maker,
+ text="Hello world",
+ subtitle_file=str(subtitle_file),
+ word_level=True,
+ )
+ subtitle_content = subtitle_file.read_text(encoding="utf-8")
+
+ self.assertEqual(subtitle_content.count(" --> "), 1)
+ self.assertIn("00:00:00,200 --> 00:00:01,100", subtitle_content)
+ self.assertIn("Hello world", subtitle_content)
+
def test_create_subtitle_ignores_markdown_underscore_marks(self):
"""
`_` 常被用户用作 Markdown 强调标记,但 TTS 返回的 cue 通常不包含
diff --git a/webui/Main.py b/webui/Main.py
index 22efd3de..95bed140 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -2291,8 +2291,12 @@ def reset_subtitle_settings():
st.session_state["subtitle_enabled_checkbox"] = defaults["subtitle_enabled"]
_set_stable_widget_value("font_name_select", defaults["font_name"])
_set_stable_widget_value("subtitle_position_select", defaults["subtitle_position"])
- _set_stable_widget_value("subtitle_display_mode_select", defaults.get("subtitle_display_mode", "sentence"))
- _set_stable_widget_value("subtitle_animation_select", defaults.get("subtitle_animation", "none"))
+ _set_stable_widget_value(
+ "subtitle_display_mode_select", defaults["subtitle_display_mode"]
+ )
+ _set_stable_widget_value(
+ "subtitle_animation_select", defaults["subtitle_animation"]
+ )
st.session_state["custom_position_input"] = str(defaults["custom_position"])
st.session_state["font_color_picker"] = defaults["text_fore_color"]
st.session_state["font_size_slider"] = defaults["font_size"]
@@ -5718,7 +5722,7 @@ def _render_audio_settings(panel, params):
# Provider 下拉只负责选择自动配音服务;无配音已经由上方模式控制,
# 不再作为 TTS Provider 混入列表,避免两个入口表达同一状态。
tts_servers = [
- ("azure-tts-v1", "Azure TTS V1"),
+ ("azure-tts-v1", "Azure TTS V1 (Edge TTS)"),
("azure-tts-v2", "Azure TTS V2"),
("siliconflow", "SiliconFlow TTS"),
("gemini-tts", "Google Gemini TTS"),
@@ -6256,7 +6260,8 @@ def _render_subtitle_settings(panel, params):
(tr("Single Word (Word by Word)"), "word_by_word"),
]
saved_display_mode = config.ui.get(
- "subtitle_display_mode", DEFAULT_SUBTITLE_SETTINGS.get("subtitle_display_mode", "sentence")
+ "subtitle_display_mode",
+ DEFAULT_SUBTITLE_SETTINGS["subtitle_display_mode"],
)
saved_mode_idx = 0
for i, (_, mode_val) in enumerate(subtitle_display_modes):
@@ -6271,10 +6276,13 @@ def _render_subtitle_settings(panel, params):
format_func=lambda value: dict(
(v, label) for label, v in subtitle_display_modes
).get(value, value),
+ help=tr("Word-by-word Timing Help"),
disabled=subtitle_settings_disabled,
)
params.subtitle_display_mode = selected_display_mode
- _set_runtime_config("ui", "subtitle_display_mode", params.subtitle_display_mode)
+ _set_runtime_config(
+ "ui", "subtitle_display_mode", params.subtitle_display_mode
+ )
# Subtitle Animation (None vs Pop Spring)
subtitle_animations = [
@@ -6282,7 +6290,8 @@ def _render_subtitle_settings(panel, params):
(tr("Pop Up (Spring)"), "pop_spring"),
]
saved_anim = config.ui.get(
- "subtitle_animation", DEFAULT_SUBTITLE_SETTINGS.get("subtitle_animation", "none")
+ "subtitle_animation",
+ DEFAULT_SUBTITLE_SETTINGS["subtitle_animation"],
)
saved_anim_idx = 0
for i, (_, anim_val) in enumerate(subtitle_animations):
@@ -6300,7 +6309,9 @@ def _render_subtitle_settings(panel, params):
disabled=subtitle_settings_disabled,
)
params.subtitle_animation = selected_anim
- _set_runtime_config("ui", "subtitle_animation", params.subtitle_animation)
+ _set_runtime_config(
+ "ui", "subtitle_animation", params.subtitle_animation
+ )
if params.subtitle_position == "custom":
saved_custom_position = config.ui.get(
diff --git a/webui/i18n/de.json b/webui/i18n/de.json
index 3926f236..f4375322 100644
--- a/webui/i18n/de.json
+++ b/webui/i18n/de.json
@@ -170,6 +170,13 @@
"Top": "Oben",
"Center": "Mittig",
"Bottom": "Unten (empfohlen)",
+ "2/3 from Bottom": "Oberes Drittel",
+ "Display Mode": "Untertitelanzeige",
+ "Sentence by Sentence": "Ganzer Satz",
+ "Single Word (Word by Word)": "Wort für Wort",
+ "Word-by-word Timing Help": "Die wortgenaue Zeitsteuerung funktioniert mit Azure TTS V1 (Edge TTS) oder Whisper am besten. Andere Sprachanbieter können auf Wortgruppen oder ganze Sätze zurückfallen.",
+ "Subtitle Animation": "Einblendanimation",
+ "Pop Up (Spring)": "Federndes Einblenden",
"Custom": "Benutzerdefiniert",
"Custom Position (% from top)": "Benutzerdefinierte Position (% von oben)",
"Please enter a value between 0 and 100": "Bitte geben Sie einen Wert zwischen 0 und 100 ein",
diff --git a/webui/i18n/en.json b/webui/i18n/en.json
index e0979909..fadc729b 100644
--- a/webui/i18n/en.json
+++ b/webui/i18n/en.json
@@ -177,13 +177,14 @@
"Top": "Top",
"Center": "Center",
"Bottom": "Bottom (Recommended)",
- "2/3 from Bottom": "2/3 from Bottom",
- "Display Mode": "Display Mode",
- "Sentence by Sentence": "Sentence by Sentence",
- "Single Word (Word by Word)": "Single Word (Word by Word)",
- "Subtitle Animation": "Subtitle Animation",
+ "2/3 from Bottom": "Upper third",
+ "Display Mode": "Subtitle display",
+ "Sentence by Sentence": "Whole sentence",
+ "Single Word (Word by Word)": "Word by word",
+ "Word-by-word Timing Help": "Word-by-word timing is most accurate with Azure TTS V1 (Edge TTS) or Whisper. Other voice providers may fall back to phrase or sentence timing.",
+ "Subtitle Animation": "Entrance animation",
"None": "None",
- "Pop Up (Spring)": "Pop Up (Spring)",
+ "Pop Up (Spring)": "Spring pop",
"Custom": "Custom",
"Custom Position (% from top)": "Custom Position (% from top)",
"Please enter a value between 0 and 100": "Please enter a value between 0 and 100",
@@ -485,4 +486,4 @@
"OFox Vendor Volcengine": "Volcano Engine (Mainland China)",
"OFox Vendor Auto": "Auto (gateway load balancing)"
}
-}
\ No newline at end of file
+}
diff --git a/webui/i18n/es.json b/webui/i18n/es.json
index 09d279a9..94361bac 100644
--- a/webui/i18n/es.json
+++ b/webui/i18n/es.json
@@ -167,6 +167,13 @@
"Top": "Arriba",
"Center": "Centro",
"Bottom": "Abajo (Recomendado)",
+ "2/3 from Bottom": "Tercio superior",
+ "Display Mode": "Visualización de subtítulos",
+ "Sentence by Sentence": "Frase completa",
+ "Single Word (Word by Word)": "Palabra por palabra",
+ "Word-by-word Timing Help": "La sincronización palabra por palabra es más precisa con Azure TTS V1 (Edge TTS) o Whisper. Otros proveedores de voz pueden usar frases o grupos de palabras.",
+ "Subtitle Animation": "Animación de entrada",
+ "Pop Up (Spring)": "Aparición con rebote",
"Custom": "Personalizada",
"Custom Position (% from top)": "Posición personalizada (% desde la parte superior)",
"Please enter a value between 0 and 100": "Introduce un valor entre 0 y 100",
diff --git a/webui/i18n/fr.json b/webui/i18n/fr.json
index fb58c0db..12c7baf6 100644
--- a/webui/i18n/fr.json
+++ b/webui/i18n/fr.json
@@ -167,6 +167,13 @@
"Top": "Haut",
"Center": "Centre",
"Bottom": "Bas (Recommandé)",
+ "2/3 from Bottom": "Tiers supérieur",
+ "Display Mode": "Affichage des sous-titres",
+ "Sentence by Sentence": "Phrase entière",
+ "Single Word (Word by Word)": "Mot par mot",
+ "Word-by-word Timing Help": "La synchronisation mot à mot est plus précise avec Azure TTS V1 (Edge TTS) ou Whisper. Les autres services vocaux peuvent revenir à des groupes de mots ou à des phrases entières.",
+ "Subtitle Animation": "Animation d’entrée",
+ "Pop Up (Spring)": "Apparition avec rebond",
"Custom": "Personnalisé",
"Custom Position (% from top)": "Position personnalisée (% depuis le haut)",
"Please enter a value between 0 and 100": "Veuillez saisir une valeur entre 0 et 100",
diff --git a/webui/i18n/id.json b/webui/i18n/id.json
index 26d50d73..d7029a7b 100644
--- a/webui/i18n/id.json
+++ b/webui/i18n/id.json
@@ -167,6 +167,13 @@
"Top": "Atas",
"Center": "Tengah",
"Bottom": "Bawah (Disarankan)",
+ "2/3 from Bottom": "Sepertiga atas",
+ "Display Mode": "Tampilan subtitel",
+ "Sentence by Sentence": "Kalimat utuh",
+ "Single Word (Word by Word)": "Kata demi kata",
+ "Word-by-word Timing Help": "Sinkronisasi kata demi kata paling akurat dengan Azure TTS V1 (Edge TTS) atau Whisper. Penyedia suara lain dapat kembali ke frasa atau kalimat utuh.",
+ "Subtitle Animation": "Animasi masuk",
+ "Pop Up (Spring)": "Muncul memantul",
"Custom": "Kustom",
"Custom Position (% from top)": "Posisi Kustom (% dari atas)",
"Please enter a value between 0 and 100": "Silakan masukkan nilai antara 0 dan 100",
diff --git a/webui/i18n/it.json b/webui/i18n/it.json
index fa223294..84259326 100644
--- a/webui/i18n/it.json
+++ b/webui/i18n/it.json
@@ -167,6 +167,13 @@
"Top": "In alto",
"Center": "Al centro",
"Bottom": "In basso (consigliato)",
+ "2/3 from Bottom": "Terzo superiore",
+ "Display Mode": "Visualizzazione sottotitoli",
+ "Sentence by Sentence": "Frase intera",
+ "Single Word (Word by Word)": "Parola per parola",
+ "Word-by-word Timing Help": "La sincronizzazione parola per parola è più precisa con Azure TTS V1 (Edge TTS) o Whisper. Gli altri servizi vocali possono usare gruppi di parole o frasi intere.",
+ "Subtitle Animation": "Animazione di ingresso",
+ "Pop Up (Spring)": "Comparsa con rimbalzo",
"Custom": "Personalizzata",
"Custom Position (% from top)": "Posizione personalizzata (% dall'alto)",
"Please enter a value between 0 and 100": "Inserisci un valore compreso tra 0 e 100",
diff --git a/webui/i18n/ko.json b/webui/i18n/ko.json
index 770012d5..91cc5675 100644
--- a/webui/i18n/ko.json
+++ b/webui/i18n/ko.json
@@ -167,6 +167,13 @@
"Top": "상단",
"Center": "중앙",
"Bottom": "하단 (권장)",
+ "2/3 from Bottom": "화면 위쪽 1/3",
+ "Display Mode": "자막 표시 방식",
+ "Sentence by Sentence": "문장 전체",
+ "Single Word (Word by Word)": "단어별 표시",
+ "Word-by-word Timing Help": "단어별 타이밍은 Azure TTS V1 (Edge TTS) 또는 Whisper에서 가장 정확합니다. 다른 음성 서비스에서는 구문이나 문장 단위로 표시될 수 있습니다.",
+ "Subtitle Animation": "자막 등장 애니메이션",
+ "Pop Up (Spring)": "탄력 있게 나타나기",
"Custom": "커스텀",
"Custom Position (% from top)": "커스텀 위치 (상단에서 %)",
"Please enter a value between 0 and 100": "0에서 100 사이의 값을 입력하세요",
diff --git a/webui/i18n/pt.json b/webui/i18n/pt.json
index 498361f0..62489c4a 100644
--- a/webui/i18n/pt.json
+++ b/webui/i18n/pt.json
@@ -167,6 +167,13 @@
"Top": "Superior",
"Center": "Centralizar",
"Bottom": "Inferior (Recomendado)",
+ "2/3 from Bottom": "Terço superior",
+ "Display Mode": "Exibição das legendas",
+ "Sentence by Sentence": "Frase completa",
+ "Single Word (Word by Word)": "Palavra por palavra",
+ "Word-by-word Timing Help": "A sincronização palavra por palavra é mais precisa com Azure TTS V1 (Edge TTS) ou Whisper. Outros provedores de voz podem usar grupos de palavras ou frases completas.",
+ "Subtitle Animation": "Animação de entrada",
+ "Pop Up (Spring)": "Entrada com efeito elástico",
"Custom": "Personalizada",
"Custom Position (% from top)": "Posição personalizada (% a partir do topo)",
"Please enter a value between 0 and 100": "Por favor, insira um valor entre 0 e 100",
diff --git a/webui/i18n/ru.json b/webui/i18n/ru.json
index d10861af..362e4110 100644
--- a/webui/i18n/ru.json
+++ b/webui/i18n/ru.json
@@ -167,6 +167,13 @@
"Top": "Сверху",
"Center": "По центру",
"Bottom": "Снизу (рекомендуется)",
+ "2/3 from Bottom": "Верхняя треть",
+ "Display Mode": "Отображение субтитров",
+ "Sentence by Sentence": "Целое предложение",
+ "Single Word (Word by Word)": "По словам",
+ "Word-by-word Timing Help": "Пословная синхронизация наиболее точна с Azure TTS V1 (Edge TTS) или Whisper. Другие голосовые сервисы могут использовать фразы или целые предложения.",
+ "Subtitle Animation": "Анимация появления",
+ "Pop Up (Spring)": "Упругое появление",
"Custom": "Своя",
"Font Size": "Размер шрифта",
"Font Color": "Текст",
diff --git a/webui/i18n/tr.json b/webui/i18n/tr.json
index d5be5835..b3c6fa3d 100644
--- a/webui/i18n/tr.json
+++ b/webui/i18n/tr.json
@@ -165,6 +165,13 @@
"Top": "Üst",
"Center": "Orta",
"Bottom": "Alt (Önerilen)",
+ "2/3 from Bottom": "Üst üçte bir",
+ "Display Mode": "Altyazı gösterimi",
+ "Sentence by Sentence": "Tam cümle",
+ "Single Word (Word by Word)": "Kelime kelime",
+ "Word-by-word Timing Help": "Kelime bazlı zamanlama Azure TTS V1 (Edge TTS) veya Whisper ile en doğru sonucu verir. Diğer ses sağlayıcıları kelime gruplarına veya tam cümlelere dönebilir.",
+ "Subtitle Animation": "Giriş animasyonu",
+ "Pop Up (Spring)": "Yaylanarak görünme",
"Custom": "Özel",
"Custom Position (% from top)": "Özel Konum (üstten %)",
"Please enter a value between 0 and 100": "Lütfen 0 ile 100 arasında bir değer girin",
diff --git a/webui/i18n/vi.json b/webui/i18n/vi.json
index 7267db4f..eebb4996 100644
--- a/webui/i18n/vi.json
+++ b/webui/i18n/vi.json
@@ -167,6 +167,13 @@
"Top": "Trên",
"Center": "Giữa",
"Bottom": "Dưới (Được Khuyến Nghị)",
+ "2/3 from Bottom": "Một phần ba phía trên",
+ "Display Mode": "Cách hiển thị phụ đề",
+ "Sentence by Sentence": "Cả câu",
+ "Single Word (Word by Word)": "Từng từ",
+ "Word-by-word Timing Help": "Căn thời gian từng từ chính xác nhất với Azure TTS V1 (Edge TTS) hoặc Whisper. Các dịch vụ giọng nói khác có thể chuyển sang hiển thị theo cụm từ hoặc cả câu.",
+ "Subtitle Animation": "Hiệu ứng xuất hiện",
+ "Pop Up (Spring)": "Bật lên đàn hồi",
"Custom": "Tùy chỉnh",
"Custom Position (% from top)": "Vị trí tùy chỉnh (% từ phía trên)",
"Please enter a value between 0 and 100": "Vui lòng nhập giá trị từ 0 đến 100",
diff --git a/webui/i18n/zh.json b/webui/i18n/zh.json
index 508ddad3..3418b0f9 100644
--- a/webui/i18n/zh.json
+++ b/webui/i18n/zh.json
@@ -177,13 +177,14 @@
"Top": "顶部",
"Center": "中间",
"Bottom": "底部(推荐)",
- "2/3 from Bottom": "距底部 2/3 (上部 1/3)",
- "Display Mode": "显示模式",
- "Sentence by Sentence": "按句显示",
- "Single Word (Word by Word)": "逐词单字跳显 (Word by Word)",
- "Subtitle Animation": "字幕动画",
+ "2/3 from Bottom": "画面上方三分之一",
+ "Display Mode": "字幕显示方式",
+ "Sentence by Sentence": "整句显示",
+ "Single Word (Word by Word)": "逐词显示",
+ "Word-by-word Timing Help": "逐词显示在 Azure TTS V1(Edge TTS)或 Whisper 下最准确;其他语音服务缺少逐词时间轴时,可能按短语或整句显示。",
+ "Subtitle Animation": "字幕入场动画",
"None": "无动画",
- "Pop Up (Spring)": "弹性弹出 (Pop Up Spring)",
+ "Pop Up (Spring)": "弹跳出现",
"Custom": "自定义位置(70,表示离顶部70%的位置)",
"Custom Position (% from top)": "自定义位置(距顶部百分比)",
"Please enter a value between 0 and 100": "请输入 0 到 100 之间的数值",
@@ -485,4 +486,4 @@
"OFox Vendor Volcengine": "火山引擎(中国大陆)",
"OFox Vendor Auto": "自动分发(网关调度)"
}
-}
\ No newline at end of file
+}