利用 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 punctuation inside Whisper words
Rudy Celekliaee3197ced1a
diff --git a/app/services/subtitle.py b/app/services/subtitle.pyindex 3bde812d..3f3419ee 100644--- a/app/services/subtitle.py+++ b/app/services/subtitle.py@@ -12,6 +12,7 @@ except ImportError:from loguru import loggerfrom app.config import config+from app.models import constfrom app.utils import utilsmodel_size = config.whisper.get("model_size", "large-v3")@@ -132,15 +133,13 @@ def create(is_segmented = Trueseg_end = word.end- # If it contains punctuation, then break the sentence.+ # Accumulate words; only trailing punctuation ends a sentence.seg_text += word.word- if utils.str_contains_punctuation(word.word):- # remove last char- seg_text = seg_text[:-1]- if not seg_text:- continue-+ if word.word.rstrip().endswith(tuple(const.PUNCTUATIONS)):+ # Punctuation inside a word (3.14, 1,000, 12:30) is not a+ # sentence boundary. Remove only actual trailing delimiters.+ seg_text = seg_text.rstrip().rstrip("".join(const.PUNCTUATIONS))recognized(seg_text, seg_start, seg_end)is_segmented = Falsediff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.pyindex 3aeda5a6..09771abf 100644--- a/test/services/test_subtitle.py+++ b/test/services/test_subtitle.py@@ -179,6 +179,56 @@ class TestSubtitleService(unittest.TestCase):self.assertEqual([item[2] for item in items], ["Hello world", "Again"])+ def test_create_preserves_numeric_punctuation_and_sentence_timing(self):+ words = [+ SimpleNamespace(start=0.0, end=0.2, word="Value"),+ SimpleNamespace(start=0.2, end=0.5, word=" 3.14"),+ SimpleNamespace(start=0.5, end=0.8, word=" costs"),+ SimpleNamespace(start=0.8, end=1.1, word=" 1,000"),+ SimpleNamespace(start=1.1, end=1.4, word=" dollars."),+ SimpleNamespace(start=1.5, end=1.8, word="Next!"),+ ]+ fake_model = SimpleNamespace(+ transcribe=lambda *_args, **_kwargs: (+ [SimpleNamespace(start=0.0, end=1.8, words=words)],+ SimpleNamespace(language="en", language_probability=0.99),+ )+ )+ with tempfile.TemporaryDirectory() as tmp_dir:+ output = Path(tmp_dir) / "numeric.srt"+ with patch.object(subtitle, "model", fake_model), patch.object(+ subtitle, "WhisperModel", object()+ ):+ subtitle.create("audio.mp3", str(output))+ items = subtitle.file_to_subtitles(str(output))+ self.assertEqual(+ items,+ [+ (1, "00:00:00,000 --> 00:00:01,400", "Value 3.14 costs 1,000 dollars"),+ (2, "00:00:01,500 --> 00:00:01,800", "Next"),+ ],+ )++ def test_create_removes_only_trailing_sentence_punctuation(self):+ words = [+ SimpleNamespace(start=0.0, end=0.3, word="3.14?!"),+ SimpleNamespace(start=0.4, end=0.7, word="1,000"),+ ]+ fake_model = SimpleNamespace(+ transcribe=lambda *_args, **_kwargs: (+ [SimpleNamespace(start=0.0, end=0.7, words=words)],+ SimpleNamespace(language="en", language_probability=0.99),+ )+ )+ with tempfile.TemporaryDirectory() as tmp_dir:+ output = Path(tmp_dir) / "suffix.srt"+ with patch.object(subtitle, "model", fake_model), patch.object(+ subtitle, "WhisperModel", object()+ ):+ subtitle.create("audio.mp3", str(output))+ items = subtitle.file_to_subtitles(str(output))+ self.assertEqual([item[2] for item in items], ["3.14", "1,000"])+def test_create_word_level_writes_each_whisper_word_with_its_timing(self):"""逐词模式应保留 Whisper 的每个词及其独立起止时间。"""transcribe_kwargs = {}