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(transcript): load projects whose transcript ends before it starts

EtienneLescot66aad0a8879f
diff --git a/src/lib/ai-edition/document/migrate.test.ts b/src/lib/ai-edition/document/migrate.test.ts
index 98c0585fb..6b0e3aef4 100644
--- a/src/lib/ai-edition/document/migrate.test.ts
+++ b/src/lib/ai-edition/document/migrate.test.ts
@@ -698,3 +698,84 @@ describe("dropping trims anchored to audio", () => {
).not.toThrow();
});
});
+
+// ─── Transcripts written end-before-start ────────────────────────────────────
+// The transcription core before a9fdd97ff clamped an end to the audio length but not its
+// start, so a hallucination past the end of the audio was stored inverted. One such word
+// failed the schema and hid the whole project from the list.
+
+describe("repairing inverted transcript timings", () => {
+ const createdAt = "2024-01-01T00:00:00.000Z";
+ const inverted = { startSec: 13.5, endSec: 6.7626875 };
+
+ function docWith(start: number, end: number) {
+ const transcript = {
+ assetId: "vid",
+ language: "auto",
+ segments: [
+ {
+ id: "seg_1",
+ kind: "speech",
+ startSec: start,
+ endSec: end,
+ text: "very",
+ wordIds: ["word_1"],
+ },
+ {
+ id: "seg_2",
+ kind: "speech",
+ startSec: start,
+ endSec: end,
+ text: "good",
+ wordIds: ["word_2"],
+ },
+ ],
+ words: [
+ { id: "word_1", segmentId: "seg_1", startSec: start, endSec: end, text: "very" },
+ { id: "word_2", segmentId: "seg_2", startSec: start, endSec: end, text: "good" },
+ ],
+ };
+ return {
+ schemaVersion: 7,
+ project: { id: "p", title: "t", createdAt, updatedAt: createdAt },
+ assets: [{ id: "vid", kind: "video", label: "v", originalPath: "/v.mp4", cameraTrack: null }],
+ transcript,
+ transcripts: [transcript],
+ timeline: {
+ clips: [],
+ gaps: [],
+ trimRanges: [],
+ muteRanges: [],
+ speedRanges: [],
+ captionRanges: [],
+ },
+ annotations: [],
+ zoomRanges: [],
+ audioTracks: [],
+ legacyEditor: null,
+ };
+ }
+
+ it("raises the end to the start, keeps every word, and parses again", () => {
+ const broken = docWith(inverted.startSec, inverted.endSec);
+ expect(() => documentSchema.parse(broken)).toThrow("endSec must be greater than or equal");
+ const doc = documentSchema.parse(migrateRawDocumentToCurrent(broken));
+ for (const transcript of [doc.transcript, ...doc.transcripts]) {
+ expect(transcript?.words.map((w) => [w.id, w.text, w.startSec, w.endSec])).toEqual([
+ ["word_1", "very", 13.5, 13.5],
+ ["word_2", "good", 13.5, 13.5],
+ ]);
+ expect(transcript?.segments.map((s) => [s.wordIds, s.endSec])).toEqual([
+ [["word_1"], 13.5],
+ [["word_2"], 13.5],
+ ]);
+ }
+ });
+
+ it("is idempotent, and returns the document untouched when nothing is inverted", () => {
+ const clean = docWith(1, 2);
+ expect(migrateRawDocumentToCurrent(clean)).toBe(clean);
+ const repaired = migrateRawDocumentToCurrent(docWith(inverted.startSec, inverted.endSec));
+ expect(migrateRawDocumentToCurrent(repaired)).toBe(repaired);
+ });
+});
diff --git a/src/lib/ai-edition/document/transcribe.test.ts b/src/lib/ai-edition/document/transcribe.test.ts
index 0ca221a8c..43a31feea 100644
--- a/src/lib/ai-edition/document/transcribe.test.ts
+++ b/src/lib/ai-edition/document/transcribe.test.ts
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { STT_NATIVE_EXTRACTION_UNAVAILABLE } from "../../../../electron/stt/transcriptionContract";
-import { type AxcutDocument, axcutSchemaVersion } from "../schema";
+import { type AxcutDocument, axcutSchemaVersion, transcriptSchema } from "../schema";
import { transcribeAsset } from "./transcribe";
vi.mock("@/components/video-editor/projectPersistence", () => ({
@@ -224,3 +224,25 @@ describe("transcribeAsset native extraction", () => {
expect(rendererMock).not.toHaveBeenCalled();
});
});
+
+describe("transcribeAsset timing", () => {
+ it("never writes a segment or word that ends before it starts", async () => {
+ // The shape an end clamped to the audio length but not its start produced: a
+ // hallucination at 13.5 s in 6.76 s of sound. Stored as is, it made the whole
+ // project unreadable.
+ transcribeMock.mockResolvedValueOnce({
+ segments: [
+ { startSec: 13.5, endSec: 6.7626875, text: "very" },
+ { startSec: 13.5, endSec: 6.7626875, text: "good" },
+ ],
+ granularity: "word",
+ detectedLanguage: "en",
+ });
+ const transcript = await transcribeAsset(makeDoc(), "asset_1");
+ expect(() => transcriptSchema.parse(transcript)).not.toThrow();
+ expect(transcript.words.map((w) => [w.text, w.startSec, w.endSec])).toEqual([
+ ["very", 13.5, 13.5],
+ ["good", 13.5, 13.5],
+ ]);
+ });
+});
diff --git a/src/lib/ai-edition/document/transcribe.ts b/src/lib/ai-edition/document/transcribe.ts
index 3300b0caf..697a5a387 100644
--- a/src/lib/ai-edition/document/transcribe.ts
+++ b/src/lib/ai-edition/document/transcribe.ts
@@ -126,7 +126,10 @@ export async function transcribeAsset(
const tokens = seg.text.trim().split(/\s+/).filter(Boolean);
if (tokens.length === 0) continue;
- const wordDuration = (seg.endSec - seg.startSec) / tokens.length;
+ // Never end before the start: the schema rejects it, and one such word made the whole
+ // project unreadable (see `raiseInvertedTranscriptEnds`, which repairs old files).
+ const segEndSec = Math.max(seg.endSec, seg.startSec);
+ const wordDuration = (segEndSec - seg.startSec) / tokens.length;
for (let w = 0; w < tokens.length; w++) {
const wordId = `word_${words.length + 1}`;
const startSec = seg.startSec + w * wordDuration;
@@ -145,7 +148,7 @@ export async function transcribeAsset(
id: segId,
kind: "speech" as const,
startSec: seg.startSec,
- endSec: seg.endSec,
+ endSec: segEndSec,
text: seg.text,
wordIds,
});
diff --git a/src/lib/ai-edition/schema/index.ts b/src/lib/ai-edition/schema/index.ts
index 06b147d6b..b92ea4c00 100644
--- a/src/lib/ai-edition/schema/index.ts
+++ b/src/lib/ai-edition/schema/index.ts
@@ -934,9 +934,60 @@ function dropAudioAnchoredTrims(raw: unknown): unknown {
return { ...doc, timeline: { ...timeline, trimRanges: kept } };
}
+/**
+ * Raise to its start the end of every transcript segment or word stored BEFORE its start.
+ *
+ * The transcription core before a9fdd97ff (transformers.js, removed 2026-07-05) clamped a
+ * timestamp's end to the audio length but not its start, so a Whisper hallucination past the
+ * end of the audio ("very good" at 13.5 s in 6.76 s of sound) was written as [13.5, 6.76].
+ * The schema rejects that, and a single such word made the whole project unreadable: it
+ * vanished from the project list.
+ *
+ * Raising the end loses nothing: the text, any edit made to it, and every id a segment or a
+ * caption refers to all survive. The word becomes a zero-length point at its start, which in
+ * the known case lies past the media, so it plays and shows nowhere.
+ *
+ * No `schemaVersion` bump, like `dropAudioAnchoredTrims`: the document comes back untouched
+ * when there is nothing to repair. Runs on RAW, untrusted input, so every read is guarded.
+ */
+function raiseInvertedTranscriptEnds(raw: unknown): unknown {
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return raw;
+ const doc = raw as Record<string, unknown>;
+ let repaired = false;
+ const repairSpans = (spans: unknown): unknown => {
+ if (!Array.isArray(spans)) return spans;
+ return spans.map((entry) => {
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) return entry;
+ const span = entry as Record<string, unknown>;
+ if (typeof span.startSec !== "number" || typeof span.endSec !== "number") return entry;
+ if (span.endSec >= span.startSec) return entry;
+ repaired = true;
+ return { ...span, endSec: span.startSec };
+ });
+ };
+ const repairTranscript = (value: unknown): unknown => {
+ if (!value || typeof value !== "object" || Array.isArray(value)) return value;
+ const transcript = value as Record<string, unknown>;
+ return {
+ ...transcript,
+ segments: repairSpans(transcript.segments),
+ words: repairSpans(transcript.words),
+ };
+ };
+ const transcript = repairTranscript(doc.transcript);
+ const transcripts = Array.isArray(doc.transcripts)
+ ? doc.transcripts.map(repairTranscript)
+ : doc.transcripts;
+ return repaired ? { ...doc, transcript, transcripts } : raw;
+}
+
export function migrateRawDocumentToCurrent(raw: unknown): unknown {
- return dropAudioAnchoredTrims(
- upgradeV6DocumentToV7(upgradeV5DocumentToV6(upgradeV4DocumentToV5(upgradeV3DocumentToV4(raw)))),
+ return raiseInvertedTranscriptEnds(
+ dropAudioAnchoredTrims(
+ upgradeV6DocumentToV7(
+ upgradeV5DocumentToV6(upgradeV4DocumentToV5(upgradeV3DocumentToV4(raw))),
+ ),
+ ),
);
}