Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: re-encode sources with reordered video frames
Richie McIlroy1028c8343f5d
diff --git a/apps/render-farm/src/transcode.test.ts b/apps/render-farm/src/transcode.test.tsindex a89825c19..e2258eca2 100644--- a/apps/render-farm/src/transcode.test.ts+++ b/apps/render-farm/src/transcode.test.ts@@ -42,10 +42,10 @@ describe("encodedSeconds", () => {describe("canRemux", () => {const packets = (codec: string, keyframes: number[], end: number) =>[- `codec_name=${codec}|pix_fmt=yuv420p`,+ `codec_name=${codec}|pix_fmt=yuv420p|has_b_frames=0`,...Array.from({ length: end * 10 + 1 }, (_, index) => {const time = index / 10;- return `pts_time=${time.toFixed(6)}|flags=${keyframes.includes(time) ? "K__" : "___"}`;+ return `pts_time=${time.toFixed(6)}|dts_time=${time.toFixed(6)}|flags=${keyframes.includes(time) ? "K__" : "___"}`;}),].join("\n");@@ -65,4 +65,27 @@ describe("canRemux", () => {).toBe(false);expect(canRemux(packets("h264", [], 3))).toBe(false);});++ test("re-encodes reordered frames even with frequent keyframes", () => {+ const source = packets("h264", [0, 1, 2], 3);+ expect(canRemux(source.replace("has_b_frames=0", "has_b_frames=2"))).toBe(+ false,+ );+ expect(canRemux(source.replace("dts_time=0.000000", "dts_time=-0.1"))).toBe(+ false,+ );+ });++ test("unknown stream properties or timestamps cannot bypass re-encoding", () => {+ const source = packets("h264", [0, 1, 2], 3);+ for (const field of ["pix_fmt=yuv420p|", "|has_b_frames=0"]) {+ expect(canRemux(source.replace(field, ""))).toBe(false);+ }+ for (const value of ["N/A", "", "NaN"]) {+ expect(+ canRemux(source.replace("dts_time=0.100000", `dts_time=${value}`)),+ ).toBe(false);+ }+ expect(canRemux(source.replace("dts_time=0.100000|", ""))).toBe(false);+ });});diff --git a/apps/render-farm/src/transcode.ts b/apps/render-farm/src/transcode.tsindex b293a6419..5da6f4d1c 100644--- a/apps/render-farm/src/transcode.ts+++ b/apps/render-farm/src/transcode.ts@@ -65,7 +65,7 @@ export function probeArgs(input: string) {"-select_streams","v:0","-show_entries",- "stream=codec_name,pix_fmt:packet=pts_time,flags",+ "stream=codec_name,pix_fmt,has_b_frames:packet=pts_time,dts_time,flags","-of","compact=p=0:nk=0",input,@@ -80,15 +80,28 @@ export function probeArgs(input: string) {export function canRemux(probe: string) {const codec = probe.match(/codec_name=([^|\n]+)/)?.[1];const pixelFormat = probe.match(/pix_fmt=([^|\n]+)/)?.[1];- if (codec !== "h264" || (pixelFormat && pixelFormat !== "yuv420p")) {+ const reorderedFrames = probe.match(/has_b_frames=([^|\n]+)/)?.[1];+ if (+ codec !== "h264" ||+ pixelFormat !== "yuv420p" ||+ reorderedFrames !== "0"+ ) {return false;}const keyframes: number[] = [];let last = 0;- for (const match of probe.matchAll(/pts_time=([0-9.]+)\|flags=([A-Z_]+)/g)) {- const time = Number(match[1]);- last = Math.max(last, time);- if (match[2]?.includes("K")) keyframes.push(time);+ for (const line of probe.split("\n")) {+ if (!line.startsWith("pts_time=")) continue;+ const packet = Object.fromEntries(+ line.split("|").map((field) => field.split("=")),+ );+ const time = Number(packet.pts_time);+ const decodeTime = Number(packet.dts_time);+ if (!Number.isFinite(time) || time !== decodeTime || time < last) {+ return false;+ }+ last = time;+ if (packet.flags?.includes("K")) keyframes.push(time);}if (keyframes.length === 0) return false;keyframes.sort((a, b) => a - b);