Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): project caption track into hold-extended output time

Richie McIlroyd8a1ec8c2ea9
diff --git a/apps/desktop/src/routes/editor/CaptionsTab.tsx b/apps/desktop/src/routes/editor/CaptionsTab.tsx
index cb78dd7b6..a1efaa89f 100644
--- a/apps/desktop/src/routes/editor/CaptionsTab.tsx
+++ b/apps/desktop/src/routes/editor/CaptionsTab.tsx
@@ -289,6 +289,8 @@ export function CaptionsTab(props: {
recordingSegments,
timeline.transitions ?? [],
sourceRange,
+ "outgoing",
+ timeline.textSegments,
);
const end = mapEditedTimeToSource(
timelineSegment.end,
@@ -296,6 +298,8 @@ export function CaptionsTab(props: {
recordingSegments,
timeline.transitions ?? [],
sourceRange,
+ "outgoing",
+ timeline.textSegments,
);
if (start !== null) source.start = start;
if (end !== null) source.end = end;
diff --git a/apps/desktop/src/routes/editor/TranscriptPage.tsx b/apps/desktop/src/routes/editor/TranscriptPage.tsx
index 6ba60d595..37536cf51 100644
--- a/apps/desktop/src/routes/editor/TranscriptPage.tsx
+++ b/apps/desktop/src/routes/editor/TranscriptPage.tsx
@@ -90,6 +90,7 @@ export function TranscriptPanel() {
project.timeline?.segments ?? [],
recordingSegments(),
project.timeline?.transitions ?? [],
+ project.timeline?.textSegments,
),
);
diff --git a/apps/desktop/src/routes/editor/captions-export.ts b/apps/desktop/src/routes/editor/captions-export.ts
index 1e6fd5d56..423616f4b 100644
--- a/apps/desktop/src/routes/editor/captions-export.ts
+++ b/apps/desktop/src/routes/editor/captions-export.ts
@@ -8,6 +8,7 @@ import {
mapCaptionsToEditedTimeline,
} from "./captions";
import type { ClipTransition } from "./clip-transitions";
+import type { TextSegment } from "./text";
export type CaptionExportFormat = "srt" | "vtt";
@@ -109,12 +110,14 @@ export function createCaptionExportCues(
timelineSegments: TimelineSegment[],
recordingSegments: SegmentRecordings[],
transitions: ClipTransition[] = [],
+ textSegments?: readonly TextSegment[],
): CaptionExportCue[] {
return mapCaptionsToEditedTimeline(
segments,
timelineSegments,
recordingSegments,
transitions,
+ textSegments,
)
.map(cueFromCaptionSegment)
.filter((cue): cue is CaptionExportCue => cue !== null)
diff --git a/apps/desktop/src/routes/editor/captions.ts b/apps/desktop/src/routes/editor/captions.ts
index 15b44451b..fc48cf50f 100644
--- a/apps/desktop/src/routes/editor/captions.ts
+++ b/apps/desktop/src/routes/editor/captions.ts
@@ -15,6 +15,7 @@ import { type ClipTransition, clipTimelineOffsets } from "./clip-transitions";
import type { TextSegment } from "./text";
import {
effectiveToOutput,
+ effectiveToOutputEnd,
heldTimeBefore,
holdWindows,
} from "./timeline-holds";
@@ -172,6 +173,7 @@ export function mapCaptionsToEditedTimeline(
timelineSegments: TimelineSegment[],
recordingSegments: SegmentRecordings[],
transitions: ClipTransition[] = [],
+ textSegments?: readonly TextSegment[],
): CaptionSegment[] {
const sanitizedSegments = rawSegments.map(clampCaptionSegmentWords);
@@ -185,6 +187,20 @@ export function mapCaptionsToEditedTimeline(
transitions,
);
+ // The edit-list mappings live in the gapless recording-flow domain; the
+ // rendered track is compared against the output clock, which includes
+ // fullscreen-text holds. Land every projected range back in output time
+ // so captions after (or across) a hold stay on their spoken content.
+ const holds = holdWindows(textSegments);
+ const holdAdjusted = (range: MappedTimeRange): MappedTimeRange =>
+ holds.length === 0
+ ? range
+ : {
+ ...range,
+ start: effectiveToOutput(holds, range.start),
+ end: effectiveToOutputEnd(holds, range.end),
+ };
+
const result: CaptionSegment[] = [];
for (const caption of sanitizedSegments) {
@@ -196,16 +212,16 @@ export function mapCaptionsToEditedTimeline(
word.end,
mapping,
);
+ if (!wordMapped) return [];
- return wordMapped
- ? [
- {
- text: word.text,
- start: wordMapped.start,
- end: wordMapped.end,
- },
- ]
- : [];
+ const adjusted = holdAdjusted(wordMapped);
+ return [
+ {
+ text: word.text,
+ start: adjusted.start,
+ end: adjusted.end,
+ },
+ ];
});
if (mappedWords.length === 0) {
@@ -228,17 +244,17 @@ export function mapCaptionsToEditedTimeline(
caption.end,
mapping,
);
+ if (!mappedRange) return [];
- return mappedRange
- ? [
- {
- ...caption,
- start: mappedRange.start,
- end: mappedRange.end,
- words: caption.words,
- },
- ]
- : [];
+ const adjusted = holdAdjusted(mappedRange);
+ return [
+ {
+ ...caption,
+ start: adjusted.start,
+ end: adjusted.end,
+ words: caption.words,
+ },
+ ];
});
mappedCaptionSegments.forEach((segment, index) => {
@@ -310,6 +326,7 @@ export function deriveCaptionTrackSegments(
recordingSegments: SegmentRecordings[],
previousTrack: CaptionTrackSegment[] = [],
transitions: ClipTransition[] = [],
+ textSegments?: readonly TextSegment[],
): CaptionTrackSegment[] {
const overridesBySourceId = new Map<string, CaptionTrackOverrides>();
for (const segment of previousTrack) {
@@ -331,6 +348,7 @@ export function deriveCaptionTrackSegments(
timelineSegments,
recordingSegments,
transitions,
+ textSegments,
);
return mapped
@@ -461,6 +479,7 @@ export function applyCaptionResultToProject<
segments: TimelineSegment[];
captionSegments?: CaptionTrackSegment[] | null;
transitions?: ClipTransition[] | null;
+ textSegments?: TextSegment[] | null;
} & Record<string, unknown>)
| null;
},
@@ -503,6 +522,7 @@ export function applyCaptionResultToProject<
recordingSegments,
timeline.captionSegments ?? [],
timeline.transitions ?? [],
+ timeline.textSegments ?? [],
);
}
@@ -725,6 +745,82 @@ if (import.meta.vitest) {
expect(result[0]?.words?.[2]?.end).toBeCloseTo(1.5);
});
+ it("projects captions into hold-extended output time", () => {
+ const fullscreenText = (start: number, end: number): TextSegment =>
+ ({
+ start,
+ end,
+ enabled: true,
+ layout: "fullscreen",
+ }) as TextSegment;
+ const identityTimeline: TimelineSegment[] = [
+ { start: 0, end: 10, timescale: 1, recordingSegment: 0 },
+ ];
+ const recordings = [{ display: { duration: 10 } } as SegmentRecordings];
+
+ // Speech after the hold lands after the inserted pause.
+ const after = mapCaptionsToEditedTimeline(
+ [
+ {
+ id: "caption",
+ start: 4,
+ end: 5,
+ text: "later",
+ words: [{ text: "later", start: 4, end: 5 }],
+ },
+ ],
+ identityTimeline,
+ recordings,
+ [],
+ [fullscreenText(2, 5)],
+ );
+ expect(after).toHaveLength(1);
+ expect(after[0]?.start).toBeCloseTo(7);
+ expect(after[0]?.end).toBeCloseTo(8);
+ expect(after[0]?.words?.[0]?.start).toBeCloseTo(7);
+ expect(after[0]?.words?.[0]?.end).toBeCloseTo(8);
+
+ // A caption ending exactly where the hold begins stays before it.
+ const before = mapCaptionsToEditedTimeline(
+ [{ id: "caption", start: 1, end: 2, text: "before", words: [] }],
+ identityTimeline,
+ recordings,
+ [],
+ [fullscreenText(2, 5)],
+ );
+ expect(before[0]?.start).toBeCloseTo(1);
+ expect(before[0]?.end).toBeCloseTo(2);
+
+ // A caption crossing the hold spans the inserted time: the word
+ // before the pause stays put, the word after resumes with the
+ // recording.
+ const across = mapCaptionsToEditedTimeline(
+ [
+ {
+ id: "caption",
+ start: 1,
+ end: 3,
+ text: "across it",
+ words: [
+ { text: "across", start: 1, end: 2 },
+ { text: "it", start: 2, end: 3 },
+ ],
+ },
+ ],
+ identityTimeline,
+ recordings,
+ [],
+ [fullscreenText(2, 5)],
+ );
+ expect(across).toHaveLength(1);
+ expect(across[0]?.start).toBeCloseTo(1);
+ expect(across[0]?.end).toBeCloseTo(6);
+ expect(across[0]?.words?.[0]?.start).toBeCloseTo(1);
+ expect(across[0]?.words?.[0]?.end).toBeCloseTo(2);
+ expect(across[0]?.words?.[1]?.start).toBeCloseTo(5);
+ expect(across[0]?.words?.[1]?.end).toBeCloseTo(6);
+ });
+
it("splits captions without word timing across retained timeline ranges", () => {
const result = mapCaptionsToEditedTimeline(
[
@@ -832,6 +928,32 @@ if (import.meta.vitest) {
expect(rederived.find((s) => s.id === "capA")?.fontSizeOverride).toBe(42);
});
+
+ it("derives the render track in hold-extended output time", () => {
+ const identity: TimelineSegment[] = [
+ { start: 0, end: 10, timescale: 1, recordingSegment: 0 },
+ ];
+
+ const track = deriveCaptionTrackSegments(
+ sourceSegments,
+ identity,
+ recordings,
+ [],
+ [],
+ [
+ {
+ start: 3,
+ end: 5,
+ enabled: true,
+ layout: "fullscreen",
+ } as TextSegment,
+ ],
+ );
+
+ expect(track.find((s) => s.id === "capA")?.start).toBeCloseTo(1);
+ expect(track.find((s) => s.id === "capB")?.start).toBeCloseTo(8);
+ expect(track.find((s) => s.id === "capB")?.end).toBeCloseTo(9);
+ });
});
describe("mapEditedTimeToSource", () => {
diff --git a/apps/desktop/src/routes/editor/context.ts b/apps/desktop/src/routes/editor/context.ts
index ccdc51e33..30e55787a 100644
--- a/apps/desktop/src/routes/editor/context.ts
+++ b/apps/desktop/src/routes/editor/context.ts
@@ -1653,7 +1653,12 @@ export const [EditorContextProvider, useEditorContext] = createContextProvider(
`${transition.segmentIndex}|${transition.type}|${transition.duration}`,
)
.join(",");
- return `${captionsSig}@@${timelineSig}@@${transitionSig}`;
+ // Fullscreen-text holds shift the projected track's output
+ // times, so moving/resizing one must re-derive too.
+ const holdSig = holdWindows(timeline.textSegments)
+ .map(([start, end]) => `${start}|${end}`)
+ .join(",");
+ return `${captionsSig}@@${timelineSig}@@${transitionSig}@@${holdSig}`;
},
() => {
const timeline = project.timeline;
@@ -1665,6 +1670,7 @@ export const [EditorContextProvider, useEditorContext] = createContextProvider(
captionRecordingSegments,
timeline.captionSegments ?? [],
timeline.transitions ?? [],
+ timeline.textSegments,
);
setProject(
"timeline",
diff --git a/apps/desktop/src/routes/editor/timeline-holds.ts b/apps/desktop/src/routes/editor/timeline-holds.ts
index 5c8abafc7..552f23a83 100644
--- a/apps/desktop/src/routes/editor/timeline-holds.ts
+++ b/apps/desktop/src/routes/editor/timeline-holds.ts
@@ -57,3 +57,19 @@ export function effectiveToOutput(
}
return output;
}
+
+// effectiveToOutput for range *end* timestamps: an end landing exactly on a
+// hold boundary binds to the content before the pause instead of jumping past
+// it, so a range that finishes right where a hold begins doesn't stretch
+// across the inserted time.
+export function effectiveToOutputEnd(
+ holds: HoldWindow[],
+ effective: number,
+): number {
+ let output = effective;
+ for (const [start, end] of holds) {
+ if (output > start) output += end - start;
+ else break;
+ }
+ return output;
+}