diff --git a/packages/engine/src/utils/ffprobe.test.ts b/packages/engine/src/utils/ffprobe.test.ts index 75425de0ef..0de060fc99 100644 --- a/packages/engine/src/utils/ffprobe.test.ts +++ b/packages/engine/src/utils/ffprobe.test.ts @@ -2348,3 +2348,69 @@ describe("the audio tail probe's read interval", () => { for (const interval of intervals) expect(interval.split("%")[0]).toMatch(/^\d+(\.\d+)?$/); }); }); + +describe("a video whose headers state no duration", () => { + it("is measured from its packets, so a recorder's WebM or Matroska is not refused", async () => { + const dir = mkdtempSync(resolve(tmpdir(), "hf-no-duration-")); + const file = resolve(dir, "recording.mkv"); + writeFileSync(file, "x"); + const { spawn } = createSpawnSpy([ + { + kind: "exit", + code: 0, + stdout: JSON.stringify({ + streams: [{ codec_type: "video", width: 32, height: 24, avg_frame_rate: "10/1" }], + format: {}, + }), + }, + { + kind: "exit", + code: 0, + stdout: "0.000000,0.100000\n2.800000,0.100000\n2.700000,0.100000\n", + }, + ]); + vi.resetModules(); + vi.doMock("child_process", () => ({ spawn })); + const { extractMediaMetadata: mocked, resolvePlayableVideoDuration } = + await import("./ffprobe.js"); + try { + const metadata = await mocked(file); + expect(resolvePlayableVideoDuration(metadata)).toBeCloseTo(2.9); + } finally { + rmSync(dir, { recursive: true, force: true }); + vi.resetModules(); + vi.doUnmock("child_process"); + } + }); + + it("ignores the half line a long film's output is cut at, and counts a packet with no stated duration as one frame", async () => { + const file = resolve(mkdtempSync(resolve(tmpdir(), "hf-no-duration-long-")), "long.mkv"); + writeFileSync(file, "x"); + const packets = (n: number) => + Array.from({ length: n }, (_, i) => `${(i / 30).toFixed(6)},N/A`).join("\n") + "\n"; + // The cut must land just after a decimal point, so its leftover digits read as a huge time. + let frames = 5000; + while (packets(frames).at(-(64 * 1024) - 1) !== ".") frames++; + const { spawn } = createSpawnSpy([ + { + kind: "exit", + code: 0, + stdout: JSON.stringify({ + streams: [{ codec_type: "video", width: 32, height: 24, avg_frame_rate: "30/1" }], + format: {}, + }), + }, + { kind: "exit", code: 0, stdout: packets(frames) }, + ]); + vi.resetModules(); + vi.doMock("child_process", () => ({ spawn })); + const { extractMediaMetadata: mocked, resolvePlayableVideoDuration } = + await import("./ffprobe.js"); + try { + expect(resolvePlayableVideoDuration(await mocked(file))).toBeCloseTo(frames / 30, 1); + } finally { + vi.resetModules(); + vi.doUnmock("child_process"); + } + }); +}); diff --git a/packages/engine/src/utils/ffprobe.ts b/packages/engine/src/utils/ffprobe.ts index 4282b39c65..b05d5616d5 100644 --- a/packages/engine/src/utils/ffprobe.ts +++ b/packages/engine/src/utils/ffprobe.ts @@ -1023,8 +1023,12 @@ export async function extractMediaMetadata(filePath: string): Promise 0) && !(streamDuration > 0) && !isPng) { + containerDuration = (await scanPacketDuration(filePath, fps)) ?? 0; + } const parsedStreamStart = videoStream.start_time ? parseFloat(videoStream.start_time) : 0; const streamStart = Number.isFinite(parsedStreamStart) ? parsedStreamStart : 0; // `nb_frames` is populated by the container demuxer; a muxer that requires @@ -1118,6 +1122,35 @@ export function readStoredFinalFrame( }); } +const PACKET_TAIL_CHARS = 64 * 1024; + +/** The video stream's length from its packets (last end time), for a file whose headers state none. */ +async function scanPacketDuration(filePath: string, fps: number): Promise { + try { + const stdout = await runFfprobe( + filePath, + [ + ...["-select_streams", "v:0", "-show_entries", "packet=pts_time,duration_time"], + ...["-of", "csv=p=0"], + ], + undefined, + { retainTail: true, maxChars: PACKET_TAIL_CHARS }, + ); + const lines = stdout.split("\n"); + // A tail cut at the cap starts partway through a line, and its number would read as a huge time. + if (stdout.length >= PACKET_TAIL_CHARS) lines.shift(); + const frame = fps > 0 ? 1 / fps : 0; + const ends = lines + .map((line) => line.trim().split(",").map(Number)) + .filter(([pts]) => Number.isFinite(pts)) + .map(([pts, duration]) => pts! + (Number.isFinite(duration) ? duration! : frame)); + return ends.length ? Math.max(...ends) : undefined; + } catch { + // expected: a file ffprobe cannot scan keeps its "no duration" verdict. + return undefined; + } +} + /** * `duration_time` of the stream's last packet (by presentation time), read from the same final * second the final-frame probe reads; undefined when ffprobe reports none.