Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(video-subtitles): keep the space between merged subtitle clauses

Max Freedom Pollard2bb95544bd7c
diff --git a/skills/ppt-master/scripts/tests/test_video_subtitles.py b/skills/ppt-master/scripts/tests/test_video_subtitles.py
new file mode 100644
index 00000000..09a40421
--- /dev/null
+++ b/skills/ppt-master/scripts/tests/test_video_subtitles.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+"""Unit tests for video_subtitles.py cue splitting and clause merging."""
+
+from __future__ import annotations
+
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+
+SCRIPTS_DIR = Path(__file__).resolve().parents[1]
+if str(SCRIPTS_DIR) not in sys.path:
+ sys.path.insert(0, str(SCRIPTS_DIR))
+
+import video_subtitles # noqa: E402
+
+
+PAGE_SRT = """1
+00:00:00,000 --> 00:00:04,000
+We plan, we build, we ship.
+
+2
+00:00:04,000 --> 00:00:08,000
+Design first, then measure, then iterate.
+"""
+
+
+class ClauseMergeTests(unittest.TestCase):
+ def test_merged_clauses_keep_the_space_that_separated_them(self) -> None:
+ self.assertEqual(
+ video_subtitles._split_sentence("We plan, we build, we ship.", 20),
+ ["We plan, we build,", "we ship."],
+ )
+ self.assertEqual(
+ video_subtitles._split_sentence(
+ "Alpha, beta, gamma, delta and epsilon.", 20
+ ),
+ ["Alpha, beta, gamma,", "delta and epsilon."],
+ )
+
+ def test_cjk_clauses_merge_without_inventing_a_space(self) -> None:
+ self.assertEqual(
+ video_subtitles._split_sentence(
+ "第一步是调研,第二步是设计,第三步是交付。", 20
+ ),
+ ["第一步是调研,第二步是设计,", "第三步是交付。"],
+ )
+
+ def test_short_sentence_is_returned_unsplit(self) -> None:
+ self.assertEqual(
+ video_subtitles._split_sentence("We ship, we learn.", 20),
+ ["We ship, we learn."],
+ )
+
+ def test_every_cue_is_a_trimmed_slice_within_the_character_budget(self) -> None:
+ sentences = (
+ "We plan, we build, we ship.",
+ "Design first, then measure, then iterate on the result.",
+ "Alpha, beta, gamma, delta and epsilon.",
+ "第一步是调研,第二步是设计,第三步是交付。",
+ "One extraordinarily long unbreakable token: supercalifragilistic.",
+ )
+ for max_chars in (3, 8, 20):
+ for sentence in sentences:
+ lines = video_subtitles._split_sentence(sentence, max_chars)
+ with self.subTest(sentence=sentence, max_chars=max_chars):
+ self.assertTrue(lines)
+ for line in lines:
+ self.assertIn(line, " ".join(sentence.split()))
+ self.assertEqual(line, line.strip())
+ self.assertLessEqual(
+ sum(not c.isspace() for c in line), max_chars
+ )
+ self.assertEqual(
+ "".join(c for line in lines for c in line if not c.isspace()),
+ "".join(c for c in sentence if not c.isspace()),
+ )
+
+
+class FrozenTranscriptTests(unittest.TestCase):
+ def test_page_local_srt_text_reaches_the_transcript_with_its_spacing(self) -> None:
+ with tempfile.TemporaryDirectory() as tmp:
+ subtitle_dir = Path(tmp) / "audio"
+ subtitle_dir.mkdir()
+ (subtitle_dir / "01-cover.srt").write_text(PAGE_SRT, encoding="utf-8")
+ self.assertEqual(
+ video_subtitles._frozen_transcript_lines(subtitle_dir, 20),
+ [
+ "We plan, we build,",
+ "we ship.",
+ "Design first,",
+ "then measure,",
+ "then iterate.",
+ ],
+ )
diff --git a/skills/ppt-master/scripts/video_subtitles.py b/skills/ppt-master/scripts/video_subtitles.py
index 62e6b02d..84c4ff1c 100644
--- a/skills/ppt-master/scripts/video_subtitles.py
+++ b/skills/ppt-master/scripts/video_subtitles.py
@@ -112,72 +112,88 @@ def _parse_srt(path: Path) -> list[SubtitleCue]:
return cues
-def _display_length(text: str) -> int:
- return sum(not character.isspace() for character in text)
+def _trim_span(text: str, start: int, end: int) -> tuple[int, int]:
+ """Return the span with leading and trailing whitespace removed."""
+ while start < end and text[start].isspace():
+ start += 1
+ while end > start and text[end - 1].isspace():
+ end -= 1
+ return start, end
-def _hard_split(text: str, max_chars: int) -> list[str]:
- """Split an overlong clause without dropping characters."""
- output: list[str] = []
- remaining = text.strip()
- while _display_length(remaining) > max_chars:
+def _display_length(text: str, start: int, end: int) -> int:
+ return sum(not character.isspace() for character in text[start:end])
+
+
+def _hard_split_span(
+ text: str,
+ span: tuple[int, int],
+ max_chars: int,
+) -> list[tuple[int, int]]:
+ """Split an overlong clause span without dropping characters."""
+ start, end = _trim_span(text, *span)
+ parts: list[tuple[int, int]] = []
+ while _display_length(text, start, end) > max_chars:
visible = 0
- split_at = 0
- whitespace_split = 0
- for index, character in enumerate(remaining, 1):
- if character.isspace():
- whitespace_split = index
+ split_at = start
+ whitespace_split = start
+ for index in range(start, end):
+ if text[index].isspace():
+ whitespace_split = index + 1
continue
visible += 1
if visible > max_chars:
break
- split_at = index
- if whitespace_split and whitespace_split <= split_at:
+ split_at = index + 1
+ if whitespace_split > start and whitespace_split <= split_at:
split_at = whitespace_split
- if split_at <= 0:
+ if split_at <= start:
raise ValueError("Unable to split an overlong subtitle clause")
- output.append(remaining[:split_at].strip())
- remaining = remaining[split_at:].strip()
- if remaining:
- output.append(remaining)
- return output
+ part = _trim_span(text, start, split_at)
+ if part[0] < part[1]:
+ parts.append(part)
+ start = _trim_span(text, split_at, end)[0]
+ part = _trim_span(text, start, end)
+ if part[0] < part[1]:
+ parts.append(part)
+ return parts
def _split_sentence(text: str, max_chars: int) -> list[str]:
"""Keep one sentence unless its display length requires clause splitting."""
sentence = re.sub(r"\s+", " ", text).strip()
- if _display_length(sentence) <= max_chars:
+ if _display_length(sentence, 0, len(sentence)) <= max_chars:
return [sentence]
- clauses: list[str] = []
+ clauses: list[tuple[int, int]] = []
start = 0
for index, character in enumerate(sentence):
if character not in _CLAUSE_END:
continue
- clause = sentence[start:index + 1].strip()
- if clause:
+ clause = _trim_span(sentence, start, index + 1)
+ if clause[0] < clause[1]:
clauses.append(clause)
start = index + 1
- tail = sentence[start:].strip()
- if tail:
+ tail = _trim_span(sentence, start, len(sentence))
+ if tail[0] < tail[1]:
clauses.append(tail)
atoms = [
part
for clause in clauses
- for part in _hard_split(clause, max_chars)
+ for part in _hard_split_span(sentence, clause, max_chars)
]
- output: list[str] = []
+ merged: list[tuple[int, int]] = []
for atom in atoms:
- if not output:
- output.append(atom)
+ if not merged:
+ merged.append(atom)
continue
- candidate = f"{output[-1]}{atom}"
- if _display_length(candidate) <= max_chars:
- output[-1] = candidate
+ candidate = (merged[-1][0], atom[1])
+ if _display_length(sentence, *candidate) <= max_chars:
+ merged[-1] = candidate
else:
- output.append(atom)
- return output
+ merged.append(atom)
+ return [sentence[span_start:span_end] for span_start, span_end in merged]
def _page_subtitle_paths(subtitle_dir: Path) -> list[Path]: