利用 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): preserve body timecodes across caption consumers
Rudy Celekli6007797295f7
diff --git a/app/services/subtitle.py b/app/services/subtitle.pyindex 71663877..5bb48b67 100644--- a/app/services/subtitle.py+++ b/app/services/subtitle.py@@ -1,6 +1,5 @@import jsonimport os.path-import reimport tempfileimport threadingfrom timeit import default_timer as timer@@ -14,6 +13,7 @@ from loguru import loggerfrom app.config import configfrom app.models import constfrom app.utils import utils+from app.utils.subtitle_reader import file_to_subtitles as file_to_subtitlesfrom app.utils.subtitle_writer import write_subtitle_filemodel_size = config.whisper.get("model_size", "large-v3")@@ -183,37 +183,6 @@ def create(logger.info(f"subtitle file created: {subtitle_file}")-def file_to_subtitles(filename):- if not filename or not os.path.isfile(filename):- return []-- times_texts = []- current_times = None- current_text = ""- index = 0- with open(filename, "r", encoding="utf-8") as f:- for line in f:- times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)- # Once a cue has started, timestamps belong to its text until the- # blank-line separator; they must not overwrite the cue's timing.- if times and current_times is None:- current_times = line- elif line.strip() == "" and current_times:- index += 1- times_texts.append((index, current_times.strip(), current_text.strip()))- current_times, current_text = None, ""- elif current_times:- current_text += line-- # Flush the final block. SRT files whose last subtitle is not followed by a- # trailing blank line never hit the blank-line branch above, so without this- # the last subtitle would be silently dropped.- if current_times:- index += 1- times_texts.append((index, current_times.strip(), current_text.strip()))- return times_texts--def transcribe_audio_bytes(audio_bytes: bytes) -> str:"""Transcribe in-memory WAV audio and remove every temporary artifact."""if not isinstance(audio_bytes, bytes) or not audio_bytes:diff --git a/app/services/video.py b/app/services/video.pyindex 9b4a5b05..7371f195 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -43,6 +43,7 @@ from app.models.schema import (from app.services import bgm as bgm_servicefrom app.services.utils import video_effectsfrom app.utils import file_security, logging_utils, utils+from app.utils.subtitle_reader import moviepy_subtitlesclass SubClippedVideoClip:def __init__(@@ -1779,7 +1780,7 @@ def generate_video(if params.subtitle_enabled and subtitle_path and os.path.exists(subtitle_path):sub = clip_stack.enter_context(SubtitlesClip(- subtitles=subtitle_path,+ subtitles=moviepy_subtitles(subtitle_path),encoding="utf-8",make_textclip=make_textclip,)diff --git a/app/services/voice.py b/app/services/voice.pyindex f69c0b97..0e12c486 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -24,13 +24,13 @@ import requestsfrom edge_tts import SubMakerfrom edge_tts.srt_composer import Subtitlefrom loguru import logger-from moviepy.video.tools import subtitlesfrom moviepy.audio.io.AudioFileClip import AudioFileClipfrom openai import OpenAIfrom app.config import configfrom app.services import bgmfrom app.utils import utils+from app.utils.subtitle_reader import moviepy_subtitlesfrom app.utils.subtitle_writer import staged_subtitle_file_DEFAULT_EDGE_TTS_TIMEOUT_SECONDS = 30.0@@ -3458,7 +3458,7 @@ def _write_subtitle_items(sub_items: list[str], subtitle_file: str) -> bool: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")+ sbs = moviepy_subtitles(staged)if not sbs:raise ValueError("subtitle output contains no cues")duration = max(tb for ((ta, tb), txt) in sbs)diff --git a/app/utils/subtitle_reader.py b/app/utils/subtitle_reader.pynew file mode 100644index 00000000..c5f95a31--- /dev/null+++ b/app/utils/subtitle_reader.py@@ -0,0 +1,45 @@+"""Read SRT cue boundaries before adapting them to MoviePy timing tuples."""+import os+import re+++def file_to_subtitles(filename):+ if not filename or not os.path.isfile(filename):+ return []++ times_texts = []+ current_times = None+ current_text = ""+ index = 0+ with open(filename, "r", encoding="utf-8") as f:+ for line in f:+ times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)+ # Once a cue has started, timestamps belong to its text until the+ # blank-line separator; they must not overwrite the cue's timing.+ if times and current_times is None:+ current_times = line+ elif line.strip() == "" and current_times:+ index += 1+ times_texts.append((index, current_times.strip(), current_text.strip()))+ current_times, current_text = None, ""+ elif current_times:+ current_text += line++ # Flush the final block. SRT files whose last subtitle is not followed by a+ # trailing blank line never hit the blank-line branch above, so without this+ # the last subtitle would be silently dropped.+ if current_times:+ index += 1+ times_texts.append((index, current_times.strip(), current_text.strip()))+ return times_texts+++def moviepy_subtitles(filename):+ """Convert only cue headers; timestamp-bearing text remains caption text."""+ from moviepy.tools import convert_to_seconds++ cues = []+ for _index, timing, text in file_to_subtitles(filename):+ start, end = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", timing)+ cues.append(((convert_to_seconds(start), convert_to_seconds(end)), text))+ return cuesdiff --git a/test/services/test_caption_timecodes.py b/test/services/test_caption_timecodes.pynew file mode 100644index 00000000..719c9223--- /dev/null+++ b/test/services/test_caption_timecodes.py@@ -0,0 +1,29 @@+"""Native caption publication and final rendering must retain spoken timecodes."""++import pytest+from edge_tts import SubMaker++from app.services import subtitle, voice+++CAPTIONS = [+ "Jump to the recording.",+ "Jump to 00:01:23,456 in the recording.",+ "Show 00:01:23,456 --> 00:01:25,456 in the log.",+]++[email protected]("text", CAPTIONS)+def test_speech_caption_publication_preserves_timecodes(tmp_path, text):+ maker = SubMaker()+ maker.feed({+ "type": "SentenceBoundary", "offset": 0,+ "duration": 8000000, "text": text,+ })+ destination = tmp_path / "speech.srt"+ voice.create_subtitle(maker, text, str(destination), word_level=True)++ assert destination.is_file()+ assert subtitle.file_to_subtitles(str(destination)) == [+ (1, "00:00:00,000 --> 00:00:00,800", text)+ ]diff --git a/test/services/test_video_project.py b/test/services/test_video_project.pyindex 2989066b..a6cdde8a 100644--- a/test/services/test_video_project.py+++ b/test/services/test_video_project.py@@ -582,3 +582,61 @@ def test_dash_external_segments_cannot_escape_source_snapshot(native):project.render()assert project.status()["failed_stage"] == "opening:scene"assert project.metadata()["last_successful_export"] is None++[email protected]("text", [+ "Jump to the recording.",+ "Jump to 00:01:23,456 in the recording.",+ "Show 00:01:23,456 --> 00:01:25,456 in the log.",+])+def test_final_caption_render_keeps_body_timecodes_and_duration(tmp_path, text):+ from moviepy import VideoFileClip++ from app.config import config+ from app.models.schema import VideoAspect, VideoParams+ from app.services import video+ from app.utils.subtitle_writer import write_subtitle_file++ ffmpeg = shutil.which("ffmpeg")+ if not ffmpeg:+ pytest.skip("native FFmpeg executable is unavailable")+ font = Path(config.root_dir) / "resource/fonts/Charm-Regular.ttf"+ if not font.is_file():+ pytest.skip("native caption font is unavailable")++ base = [ffmpeg, "-nostdin", "-v", "error", "-y", "-threads", "1"]+ media = tmp_path / "white.mp4"+ audio = tmp_path / "narration.wav"+ subprocess.run([+ *base, "-f", "lavfi", "-i", "color=c=white:s=1080x1080:r=10:d=0.8",+ "-c:v", "libx264", "-pix_fmt", "yuv420p", str(media),+ ], check=True, timeout=30)+ subprocess.run([+ *base, "-f", "lavfi", "-i", "sine=frequency=220:duration=0.8",+ "-ar", "44100", str(audio),+ ], check=True, timeout=30)+ artifact = tmp_path / "accepted.srt"+ assert write_subtitle_file(+ str(artifact), "1\n00:00:00,100 --> 00:00:00,700\n" + text + "\n\n"+ )+ output = tmp_path / "captioned.mp4"+ params = VideoParams(+ video_subject="owned-caption", video_aspect=VideoAspect.square,+ subtitle_enabled=True, subtitle_position="center",+ font_name="Charm-Regular.ttf", font_size=36,+ text_fore_color="#000000", stroke_width=0, text_background_color=False,+ bgm_type="", bgm_volume=0, voice_volume=1, n_threads=1,+ )+ previous = config.app.copy()+ try:+ config.app.update({"video_codec": "libx264", "video_clip_concurrency": 1})+ assert video.generate_video(+ str(media), str(audio), str(artifact), str(output), params+ )+ with VideoFileClip(str(output), audio=False) as rendered:+ assert 0.7 <= rendered.duration <= 0.9+ frame = rendered.get_frame(0.4)+ assert int((frame.mean(axis=2) < 180).sum()) > 100+ finally:+ config.app.clear()+ config.app.update(previous)