利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(subtitle): atomically publish fresh caption artifacts
RudyCelekli44e6d5e11832
diff --git a/app/services/subtitle.py b/app/services/subtitle.pyindex 3f3419ee..71663877 100644--- a/app/services/subtitle.py+++ b/app/services/subtitle.py@@ -14,6 +14,7 @@ from loguru import loggerfrom app.config import configfrom app.models import constfrom app.utils import utils+from app.utils.subtitle_writer import write_subtitle_filemodel_size = config.whisper.get("model_size", "large-v3")device = config.whisper.get("device", "cpu")@@ -174,8 +175,10 @@ def create(idx += 1sub = "\n".join(lines) + "\n"- with open(subtitle_file, "w", encoding="utf-8") as f:- f.write(sub)+ if not lines:+ logger.warning("transcription produced no subtitle cues")+ return+ write_subtitle_file(subtitle_file, sub)if log_details:logger.info(f"subtitle file created: {subtitle_file}")@@ -349,9 +352,11 @@ def correct(subtitle_file, video_script):corrected = Trueif corrected:- with open(subtitle_file, "w", encoding="utf-8") as fd:- for i, item in enumerate(new_subtitle_items):- fd.write(f"{i + 1}\n{item[1]}\n{item[2]}\n\n")+ content = "".join(+ f"{i + 1}\n{item[1]}\n{item[2]}\n\n"+ for i, item in enumerate(new_subtitle_items)+ )+ write_subtitle_file(subtitle_file, content)logger.info("Subtitle corrected")else:logger.success("Subtitle is correct")diff --git a/app/services/task.py b/app/services/task.pyindex ebdc59dc..183e1765 100644--- a/app/services/task.py+++ b/app/services/task.py@@ -35,6 +35,7 @@ from app.services import (from app.services import upload_postfrom app.services import state as smfrom app.utils import file_security, utils+from app.utils.subtitle_writer import staged_subtitle_file# 发布请求最长可等待数分钟,不能继续占用视频生成任务的并发名额。@@ -589,7 +590,7 @@ def generate_subtitle(task_id, params, video_script, sub_maker, audio_file):if not params.subtitle_enabled:return ""- subtitle_path = path.join(utils.task_dir(task_id), "subtitle.srt")+ final_subtitle_path = path.join(utils.task_dir(task_id), "subtitle.srt")subtitle_provider = config.app.get("subtitle_provider", "edge").strip().lower()logger.info(f"\n\n## generating subtitle, provider: {subtitle_provider}")@@ -609,40 +610,46 @@ def generate_subtitle(task_id, params, video_script, sub_maker, audio_file):is_word_level = getattr(params, "subtitle_display_mode", "sentence") == "word_by_word"- if subtitle_provider == "edge":- voice.create_subtitle(- text=video_script,- sub_maker=sub_maker,- subtitle_file=subtitle_path,- word_level=is_word_level,- )- if not os.path.exists(subtitle_path):- # Edge 字幕偶尔会因为时间轴与文案无法匹配而没有产出文件。这里不能- # 自动切换到 Whisper,否则首次失败会在用户不知情的情况下下载数 GB- # 的模型。只有显式配置 Whisper 时才允许加载模型,Edge 失败则保留- # 无字幕视频并记录原因,避免意外的网络和磁盘开销。- logger.warning(- "edge subtitle generation did not produce a subtitle file; "- "skip subtitles without falling back to whisper"+ # A failed retry must never reuse captions from an earlier narration.+ with staged_subtitle_file(final_subtitle_path) as subtitle_path:+ if subtitle_provider == "edge":+ voice.create_subtitle(+ text=video_script,+ sub_maker=sub_maker,+ subtitle_file=subtitle_path,+ word_level=is_word_level,)- return ""-- if subtitle_provider == "whisper":- subtitle.create(- audio_file=audio_file,- subtitle_file=subtitle_path,- word_level=is_word_level,- )- if not is_word_level:- logger.info("\n\n## correcting subtitle")- subtitle.correct(subtitle_file=subtitle_path, video_script=video_script)+ if not os.path.exists(subtitle_path):+ # Edge 字幕偶尔会因为时间轴与文案无法匹配而没有产出文件。这里不能+ # 自动切换到 Whisper,否则首次失败会在用户不知情的情况下下载数 GB+ # 的模型。只有显式配置 Whisper 时才允许加载模型,Edge 失败则保留+ # 无字幕视频并记录原因,避免意外的网络和磁盘开销。+ logger.warning(+ "edge subtitle generation did not produce a subtitle file; "+ "skip subtitles without falling back to whisper"+ )+ return ""- subtitle_lines = subtitle.file_to_subtitles(subtitle_path)- if not subtitle_lines:- logger.warning(f"subtitle file is invalid: {subtitle_path}")- return ""+ if subtitle_provider == "whisper":+ subtitle.create(+ audio_file=audio_file,+ subtitle_file=subtitle_path,+ word_level=is_word_level,+ )+ if not subtitle.file_to_subtitles(subtitle_path):+ logger.warning("whisper produced no usable subtitle cues")+ return ""+ if not is_word_level:+ logger.info("\n\n## correcting subtitle")+ subtitle.correct(subtitle_file=subtitle_path, video_script=video_script)++ subtitle_lines = subtitle.file_to_subtitles(subtitle_path)+ if not subtitle_lines:+ logger.warning(f"subtitle file is invalid: {subtitle_path}")+ return ""- return subtitle_path+ os.replace(subtitle_path, final_subtitle_path)+ return final_subtitle_pathdef get_video_materials(diff --git a/app/services/voice.py b/app/services/voice.pyindex 45aaeab9..6e0d91b4 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -30,6 +30,7 @@ from openai import OpenAIfrom app.config import configfrom app.utils import utils+from app.utils.subtitle_writer import staged_subtitle_file_DEFAULT_EDGE_TTS_TIMEOUT_SECONDS = 30.0_SILICONFLOW_TTS_TIMEOUT_SECONDS = (10, 300) # connect, read@@ -3219,28 +3220,23 @@ def _match_script_line(script_lines: list[str], current_text: str, sub_index: indef _write_subtitle_items(sub_items: list[str], subtitle_file: str) -> bool:- """- 将已经聚合好的字幕段写入到 SRT 文件,并做一次基本可读性验证。-- 返回值:- - `True`:字幕文件成功落盘且可被 moviepy 解析;- - `False`:字幕文件写入或解析失败。- """+ """Publish a complete, parseable SRT without destroying earlier captions."""try:ensure_file_path_exists(subtitle_file)- with open(subtitle_file, "w", encoding="utf-8") as file:- file.write("\n".join(sub_items) + "\n")-- sbs = subtitles.file_to_subtitles(subtitle_file, encoding="utf-8")- duration = max([tb for ((ta, tb), txt) in sbs]) if sbs else 0+ with staged_subtitle_file(subtitle_file) as staged:+ with open(staged, "w", encoding="utf-8") as file:+ file.write("\n".join(sub_items) + "\n")+ sbs = subtitles.file_to_subtitles(staged, encoding="utf-8")+ if not sbs:+ raise ValueError("subtitle output contains no cues")+ duration = max(tb for ((ta, tb), txt) in sbs)+ os.replace(staged, subtitle_file)logger.info(f"completed, subtitle file created: {subtitle_file}, duration: {duration}")return Trueexcept Exception as e:logger.error(f"failed, error: {str(e)}")- if os.path.exists(subtitle_file):- os.remove(subtitle_file)return Falsediff --git a/app/utils/subtitle_writer.py b/app/utils/subtitle_writer.pynew file mode 100644index 00000000..3743e017--- /dev/null+++ b/app/utils/subtitle_writer.py@@ -0,0 +1,31 @@+"""Stage subtitle artifacts beside their destination before publication."""+from contextlib import contextmanager+import os+import tempfile++from loguru import logger+++@contextmanager+def staged_subtitle_file(destination):+ descriptor, staged = tempfile.mkstemp(+ prefix=".subtitle-", suffix=".srt",+ dir=os.path.dirname(os.path.abspath(destination)),+ )+ os.close(descriptor)+ try:+ yield staged+ finally:+ try:+ os.remove(staged)+ except FileNotFoundError:+ pass+ except OSError as cleanup_error:+ logger.warning(f"failed to remove temporary subtitle: {cleanup_error}")+++def write_subtitle_file(destination, content):+ with staged_subtitle_file(destination) as staged:+ with open(staged, "w", encoding="utf-8") as output:+ output.write(content)+ os.replace(staged, destination)diff --git a/test/services/test_media_srt_publication.py b/test/services/test_media_srt_publication.pynew file mode 100644index 00000000..80e2e1cd--- /dev/null+++ b/test/services/test_media_srt_publication.py@@ -0,0 +1,87 @@+import builtins+from types import SimpleNamespace++import pytest++from app.services import subtitle, task, voice+++CUE = '1\n00:00:00,000 --> 00:00:01,000\nOld transcript\n\n'+++def test_edge_generation_does_not_reuse_previous_task_captions(tmp_path, monkeypatch):+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')+ monkeypatch.setattr(task.utils, 'task_dir', lambda *args, **kwargs: str(tmp_path))+ monkeypatch.setitem(task.config.app, 'subtitle_provider', 'edge')+ params = SimpleNamespace(subtitle_enabled=True, subtitle_display_mode='sentence')+ maker = SimpleNamespace(cues=[], subs=[], offset=[])++ assert task.generate_subtitle('retry', params, 'Different narration.', maker, 'audio.mp3') == ''+ assert destination.read_text(encoding='utf-8') == CUE+ assert list(tmp_path.iterdir()) == [destination]++[email protected]('writer', ['voice', 'correction', 'whisper'])+def test_failed_caption_write_preserves_complete_existing_srt(tmp_path, monkeypatch, writer):+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')+ original = builtins.open++ class PartialWrite:+ def __init__(self, file):+ self.file = file+ def __enter__(self):+ return self+ def __exit__(self, *args):+ self.file.close()+ def write(self, text):+ self.file.write(text[:5])+ self.file.flush()+ raise OSError('disk full during caption write')++ def open_file(filename, mode='r', *args, **kwargs):+ file = original(filename, mode, *args, **kwargs)+ if mode == 'w' and str(filename).endswith('.srt'):+ return PartialWrite(file)+ return file++ monkeypatch.setattr(builtins, 'open', open_file)+ if writer == 'voice':+ assert voice._write_subtitle_items([CUE], str(destination)) is False+ elif writer == 'correction':+ with pytest.raises(OSError):+ subtitle.correct(str(destination), 'Changed transcript.')+ else:+ word = SimpleNamespace(word='Changed', start=0.0, end=1.0)+ segment = SimpleNamespace(words=[word], text='Changed', start=0.0, end=1.0)+ info = SimpleNamespace(language='en', language_probability=1.0)+ model = SimpleNamespace(transcribe=lambda *args, **kwargs: (iter([segment]), info))+ monkeypatch.setattr(subtitle, 'WhisperModel', object())+ monkeypatch.setattr(subtitle, 'model', model)+ with pytest.raises(OSError):+ subtitle.create('local-audio.mp3', str(destination))+ assert destination.read_text(encoding='utf-8') == CUE+ assert list(tmp_path.iterdir()) == [destination]+++def test_subtitle_cleanup_error_does_not_hide_primary_write_failure(tmp_path, monkeypatch):+ from app.utils import subtitle_writer++ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')+ staged = None++ def cannot_cleanup(*args):+ raise PermissionError('temporary captions are busy')++ monkeypatch.setattr(subtitle_writer.os, 'remove', cannot_cleanup)+ with pytest.raises(OSError, match='disk full during caption write'):+ with subtitle_writer.staged_subtitle_file(str(destination)) as staged:+ with builtins.open(staged, 'w', encoding='utf-8') as output:+ output.write('partial captions')+ raise OSError('disk full during caption write')+ assert destination.read_text(encoding='utf-8') == CUE+ assert staged != str(destination)+ with builtins.open(staged, encoding='utf-8') as output:+ assert output.read() == 'partial captions'diff --git a/test/services/test_task.py b/test/services/test_task.pyindex cfe71fc5..6bfc5f35 100644--- a/test/services/test_task.py+++ b/test/services/test_task.py@@ -1112,13 +1112,16 @@ class TestTaskService(unittest.TestCase):shutil.rmtree(task_dir, ignore_errors=True)self.assertTrue(subtitle_path.endswith("subtitle.srt"))+ created_path = create.call_args.kwargs["subtitle_file"]+ self.assertNotEqual(created_path, subtitle_path)+ self.assertEqual(Path(created_path).parent, Path(subtitle_path).parent)create.assert_called_once_with(audio_file=audio_file,- subtitle_file=subtitle_path,+ subtitle_file=created_path,word_level=False,)correct.assert_called_once_with(- subtitle_file=subtitle_path, video_script="Hello world."+ subtitle_file=created_path, video_script="Hello world.")def test_generate_subtitle_uses_whisper_word_timing_without_correction(self):@@ -1169,9 +1172,12 @@ class TestTaskService(unittest.TestCase):shutil.rmtree(task_dir, ignore_errors=True)self.assertTrue(subtitle_path.endswith("subtitle.srt"))+ created_path = create.call_args.kwargs["subtitle_file"]+ self.assertNotEqual(created_path, subtitle_path)+ self.assertEqual(Path(created_path).parent, Path(subtitle_path).parent)create.assert_called_once_with(audio_file=audio_file,- subtitle_file=subtitle_path,+ subtitle_file=created_path,word_level=True,)correct.assert_not_called()