mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(engine): read a video's length from its packets when headers state none (#7233)
* fix(engine): read a video's length from its packets when its headers state none * fix(engine): ignore the cut first line of a long packet scan and count N/A as a frame * chore(engine): trim the packet scan comments and put its doc back on the function * style(engine): format the packet duration test
This commit is contained in:
@@ -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");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1023,8 +1023,12 @@ export async function extractMediaMetadata(filePath: string): Promise<VideoMetad
|
||||
? await probeDecodedAlphaPlane(filePath, alphaDecoder)
|
||||
: undefined;
|
||||
|
||||
const containerDuration = output?.format.duration ? parseFloat(output.format.duration) : 0;
|
||||
let containerDuration = output?.format.duration ? parseFloat(output.format.duration) : 0;
|
||||
const streamDuration = videoStream.duration ? parseFloat(videoStream.duration) : 0;
|
||||
// Recorder output (WebM/Matroska written to a pipe) carries no duration header: read it off the packets.
|
||||
if (!(containerDuration > 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<number | undefined> {
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user