Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix: time-slice single-group phrase captions into lines

parse-nip3a48e54db1b1
diff --git a/src/lib/captioning/annotationsFromCaptions.test.ts b/src/lib/captioning/annotationsFromCaptions.test.ts
index 31679f7f5..bbf26fed2 100644
--- a/src/lib/captioning/annotationsFromCaptions.test.ts
+++ b/src/lib/captioning/annotationsFromCaptions.test.ts
@@ -24,18 +24,23 @@ describe("groupPhraseCaptionSegmentsIntoLines", () => {
expect(lines[0]!.endSec).toBeLessThanOrEqual(0.62);
});
- it("wraps a single phrase into multiple visual lines without inventing extra timing regions", () => {
+ it("slices a single merged phrase into timed caption lines by word bounds", () => {
const lines = groupPhraseCaptionSegmentsIntoLines(
[{ startSec: 0, endSec: 1, text: "alpha beta gamma delta" }],
2,
2,
);
- expect(lines).toHaveLength(1);
+ expect(lines).toHaveLength(2);
expect(lines[0]).toMatchObject({
startSec: 0,
+ endSec: 0.5,
+ text: "alpha beta",
+ });
+ expect(lines[1]).toMatchObject({
+ startSec: 0.5,
endSec: 1,
- text: "alpha beta\ngamma delta",
+ text: "gamma delta",
});
});
});
diff --git a/src/lib/captioning/annotationsFromCaptions.ts b/src/lib/captioning/annotationsFromCaptions.ts
index ca82b0477..0f6dc2af4 100644
--- a/src/lib/captioning/annotationsFromCaptions.ts
+++ b/src/lib/captioning/annotationsFromCaptions.ts
@@ -459,11 +459,42 @@ export function groupPhraseCaptionSegmentsIntoLines(
for (const group of groups) {
if (group.length === 1) {
const only = group[0]!;
- out.push({
- startSec: only.startSec,
- endSec: only.endSec,
- text: wrapCaptionTextByWordBounds(only.text, minWords, maxWords),
- });
+ const wrapped = wrapCaptionTextByWordBounds(only.text, minWords, maxWords).trim();
+ if (!wrapped) continue;
+ const lineTexts = wrapped
+ .split("\n")
+ .map((t) => t.trim())
+ .filter(Boolean);
+ const n = lineTexts.length;
+ const rawDur = only.endSec - only.startSec;
+ if (n > 1 && rawDur < n * WORD_SPLIT_MIN_SPAN_SEC) {
+ out.push({
+ startSec: only.startSec,
+ endSec: only.endSec,
+ text: lineTexts.join(" "),
+ });
+ continue;
+ }
+ const dur = Math.max(rawDur, WORD_SPLIT_MIN_SPAN_SEC * n);
+ if (n <= 1) {
+ out.push({
+ startSec: only.startSec,
+ endSec: only.endSec,
+ text: lineTexts[0] ?? wrapped,
+ });
+ continue;
+ }
+ for (let i = 0; i < n; i++) {
+ const startSec = only.startSec + (dur * i) / n;
+ const boundary = only.startSec + (dur * (i + 1)) / n;
+ const endSec =
+ i === n - 1 ? only.endSec : Math.max(startSec + WORD_SPLIT_MIN_SPAN_SEC, boundary);
+ out.push({
+ startSec,
+ endSec,
+ text: lineTexts[i]!,
+ });
+ }
continue;
}