利用 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): never publish a caption file without cues
c020627e053af00af42
diff --git a/app/utils/subtitle_writer.py b/app/utils/subtitle_writer.pyindex 3743e017..8345937b 100644--- a/app/utils/subtitle_writer.py+++ b/app/utils/subtitle_writer.py@@ -24,8 +24,37 @@ def staged_subtitle_file(destination):logger.warning(f"failed to remove temporary subtitle: {cleanup_error}")-def write_subtitle_file(destination, content):+def has_subtitle_cue(content) -> bool:+ """Return True when the body carries at least one timed SRT cue.++ An SRT body without a single ``-->`` timing line holds no caption any player+ can display, so a publication built from it is a failed write rather than a+ valid empty result.+ """+ return any("-->" in line for line in str(content or "").splitlines())+++def write_subtitle_file(destination, content) -> bool:+ """Publish a complete subtitle file without destroying existing captions.++ Every other caption publication site validates the artifact it is about to+ replace: ``voice._write_subtitle_items`` re-parses the staged file and+ ``task.generate_subtitle`` requires parseable cues before ``os.replace``.+ Publishing an empty body would silently delete the captions already stored+ at ``destination``, which is the exact outcome those checks exist to+ prevent, so the same rule is enforced once here for every caller.++ Returns ``True`` after a successful replacement, and ``False`` when nothing+ was published because the body held no cue.+ """+ if not has_subtitle_cue(content):+ logger.warning(+ f"refusing to publish a subtitle file without cues: path={destination}"+ )+ return False+with staged_subtitle_file(destination) as staged:with open(staged, "w", encoding="utf-8") as output:output.write(content)os.replace(staged, destination)+ return Truediff --git a/test/services/test_media_srt_publication.py b/test/services/test_media_srt_publication.pyindex 80e2e1cd..59a60c0c 100644--- a/test/services/test_media_srt_publication.py+++ b/test/services/test_media_srt_publication.py@@ -4,6 +4,7 @@ from types import SimpleNamespaceimport pytestfrom app.services import subtitle, task, voice+from app.utils import subtitle_writerCUE = '1\n00:00:00,000 --> 00:00:01,000\nOld transcript\n\n'@@ -66,8 +67,6 @@ def test_failed_caption_write_preserves_complete_existing_srt(tmp_path, monkeypadef 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@@ -85,3 +84,55 @@ def test_subtitle_cleanup_error_does_not_hide_primary_write_failure(tmp_path, moassert staged != str(destination)with builtins.open(staged, encoding='utf-8') as output:assert output.read() == 'partial captions'++[email protected]('body', ['', ' ', '\n\n', '1\n00:00:01,000\nText\n'])+def test_body_without_a_cue_does_not_replace_published_captions(tmp_path, body):+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')++ assert subtitle_writer.write_subtitle_file(str(destination), body) is False+ assert destination.read_text(encoding='utf-8') == CUE+ assert list(tmp_path.iterdir()) == [destination]+++def test_complete_body_is_still_published(tmp_path):+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')+ replacement = '1\n00:00:00,000 --> 00:00:02,000\nReplacement\n\n'++ assert subtitle_writer.write_subtitle_file(str(destination), replacement) is not False+ assert destination.read_text(encoding='utf-8') == replacement+ assert list(tmp_path.iterdir()) == [destination]++[email protected]('script', ['---', '***', '___', ' ', '[pause: 3s]'])+def test_correction_that_drops_every_cue_preserves_published_captions(tmp_path, script):+ """A script that normalizes to nothing must not delete the transcript.++ whisper ``correct()`` merges cues against the script. When the script+ carries no line to merge against, the corrected body is empty; writing it+ would delete the cues whisper already produced, so the original+ transcription has to survive for ``task.generate_subtitle`` to publish.+ """+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')++ subtitle.correct(str(destination), script)++ assert destination.read_text(encoding='utf-8') == CUE+ assert len(subtitle.file_to_subtitles(str(destination))) == 1+ assert list(tmp_path.iterdir()) == [destination]+++def test_correction_still_publishes_when_the_script_has_lines(tmp_path):+ destination = tmp_path / 'subtitle.srt'+ destination.write_text(CUE, encoding='utf-8')++ subtitle.correct(str(destination), 'New transcript.')++ published = destination.read_text(encoding='utf-8')+ assert '-->' in published+ assert 'New transcript' in published+ assert 'Old transcript' not in published+ assert list(tmp_path.iterdir()) == [destination]