From e8e3dc1c53bec10ea8ab245a3a082f348cff7e9e Mon Sep 17 00:00:00 2001 From: Simon Mavec Date: Sat, 4 Jul 2026 08:11:03 +0200 Subject: [PATCH] Fix decoder crash when rendering multiple segments Chunks from each clip start their timestamps at 0, so concatenating segments (or repeating one) makes the timestamps jump backwards. Chrome's VideoDecoder doesn't like that and closes the codec, which then spams "decode on a closed codec" errors and stalls the render. Rewrite the timestamps into one increasing sequence before decoding. --- src/lib.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.ts b/src/lib.ts index 8022f49..9b52ccb 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -84,6 +84,22 @@ export const record = async ( onProgress: (progress: number) => unknown ) => new Promise((resolve) => { + // The chunks are concatenated across segments and repeats, so their + // original per-file timestamps reset backwards at every boundary. A + // non-monotonic timestamp makes VideoDecoder error out and close the + // codec, after which every further decode() throws InvalidStateError. + // Rewrite the timestamps into one strictly increasing sequence. + chunks = chunks.map((chunk, i) => { + const data = new Uint8Array(chunk.byteLength); + chunk.copyTo(data); + return new EncodedVideoChunk({ + type: chunk.type, + timestamp: (i * 1e6) / FPS, + duration: 1e6 / FPS, + data, + }); + }); + const canvas = document.createElement("canvas"); canvas.width = settings.width; canvas.height = settings.height;