Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: measure imported Loom frame rates from video packets

Richie McIlroy4d8c14388f8a
diff --git a/apps/media-server/src/__tests__/loom-importer/parsing.test.ts b/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
index c91b4d938..a210bf23d 100644
--- a/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
+++ b/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
@@ -171,7 +171,7 @@ describe("media decisions", () => {
expect(chooseContainer(codecs)).toBe(expected);
});
- test("uses the measured packet rate when the average rate is missing", () => {
+ test("prefers the measured packet rate over container rates", () => {
expect(
resolveFps(
{
@@ -182,11 +182,24 @@ describe("media decisions", () => {
20,
),
).toBe(30);
+ expect(
+ resolveFps(
+ {
+ avg_frame_rate: "120/1",
+ r_frame_rate: "120/1",
+ nb_read_packets: "7749",
+ },
+ 258.304,
+ ),
+ ).toBe(30);
});
- test("rejects implausible nominal rates", () => {
+ test("falls back to plausible container rates", () => {
expect(resolveFps({ r_frame_rate: "1000/1" }, 0)).toBeNull();
expect(resolveFps({ avg_frame_rate: "30000/1001" }, 10)).toBe(29.97);
+ expect(resolveFps({ avg_frame_rate: "0/0", r_frame_rate: "25/1" }, 0)).toBe(
+ 25,
+ );
});
test("maps only the first video and audio stream", () => {
diff --git a/apps/media-server/src/loom-importer/media.ts b/apps/media-server/src/loom-importer/media.ts
index 93f822718..cc0689a50 100644
--- a/apps/media-server/src/loom-importer/media.ts
+++ b/apps/media-server/src/loom-importer/media.ts
@@ -63,6 +63,7 @@ interface ProbeStream {
avg_frame_rate?: string;
r_frame_rate?: string;
nb_read_packets?: string;
+ duration?: string;
channels?: number;
sample_rate?: string;
}
@@ -121,16 +122,16 @@ export function resolveFps(
stream: ProbeStream,
duration: number,
): number | null {
- const average = parseRate(stream.avg_frame_rate);
- if (average > 0 && average <= 240) return Math.round(average * 100) / 100;
+ const round = (value: number) => Math.round(value * 100) / 100;
const packets = Number(stream.nb_read_packets);
if (packets > 0 && duration > 0) {
const measured = packets / duration;
- if (measured > 0 && measured <= 240)
- return Math.round(measured * 100) / 100;
+ if (measured > 0 && measured <= 240) return round(measured);
+ }
+ for (const rate of [stream.avg_frame_rate, stream.r_frame_rate]) {
+ const value = parseRate(rate);
+ if (value > 0 && value <= 240) return round(value);
}
- const nominal = parseRate(stream.r_frame_rate);
- if (nominal > 0 && nominal <= 240) return Math.round(nominal * 100) / 100;
return null;
}
@@ -146,7 +147,7 @@ export async function probeOutput(
"error",
"-count_packets",
"-show_entries",
- "format=duration,bit_rate:stream=codec_type,codec_name,width,height,avg_frame_rate,r_frame_rate,nb_read_packets,channels,sample_rate",
+ "format=duration,bit_rate:stream=codec_type,codec_name,width,height,avg_frame_rate,r_frame_rate,nb_read_packets,duration,channels,sample_rate",
"-of",
"json",
path,
@@ -163,7 +164,7 @@ export async function probeOutput(
throw new Error("Imported Loom video has no readable video stream");
if (!Number.isFinite(duration) || duration <= 0)
throw new Error("Imported Loom video has no readable duration");
- const fps = resolveFps(video, duration);
+ const fps = resolveFps(video, Number(video.duration) || duration);
if (!fps) throw new Error("Imported Loom video has no readable frame rate");
const fileSize = (await stat(path)).size;
return {