mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(producer): export sizes an unsized phone video as it plays, not as stored (#7228)
* fix(producer): export sizes an unsized phone video as it plays, not as stored * fix(engine): keep the stored-size check on the video layer path
This commit is contained in:
@@ -381,6 +381,7 @@ export { readWebGlVendorInfoFromCanvas } from "./utils/readWebGlVendorInfoFromCa
|
||||
|
||||
export {
|
||||
extractMediaMetadata,
|
||||
displaySize,
|
||||
extractVideoMetadata,
|
||||
extractFinalVideoFrameTimestamp,
|
||||
extractAudioMetadata,
|
||||
|
||||
@@ -77,6 +77,63 @@ describe("extractMediaMetadata", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("a phone clip's display size", () => {
|
||||
afterEach(() => {
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
async function probed(stream: Record<string, unknown>) {
|
||||
const { spawn } = createSpawnSpy([
|
||||
{
|
||||
kind: "exit",
|
||||
code: 0,
|
||||
stdout: JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
codec_type: "video",
|
||||
codec_name: "hevc",
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
r_frame_rate: "30/1",
|
||||
avg_frame_rate: "30/1",
|
||||
...stream,
|
||||
},
|
||||
],
|
||||
format: { duration: "3" },
|
||||
}),
|
||||
},
|
||||
]);
|
||||
vi.resetModules();
|
||||
vi.doMock("child_process", () => ({ spawn }));
|
||||
const { extractMediaMetadata: mocked, displaySize } = await import("./ffprobe.js");
|
||||
const dir = mkdtempSync(`${tmpdir()}/hf-turned-`);
|
||||
try {
|
||||
writeFileSync(`${dir}/clip.mov`, "");
|
||||
return displaySize(await mocked(`${dir}/clip.mov`));
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
it("is portrait for an iPhone clip stored landscape with a -90 display matrix", async () => {
|
||||
const shown = await probed({ side_data_list: [{ rotation: -90 }] });
|
||||
expect(shown).toEqual({ width: 1080, height: 1920 });
|
||||
});
|
||||
|
||||
it("reads an older muxer's rotate tag, and keeps a half turn's size", async () => {
|
||||
expect(await probed({ tags: { rotate: "270" } })).toEqual({ width: 1080, height: 1920 });
|
||||
expect(await probed({ side_data_list: [{ rotation: 180 }] })).toEqual({
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
});
|
||||
});
|
||||
|
||||
it("widens anamorphic pixels, then turns, as Chrome lays the element out", async () => {
|
||||
const shown = await probed({ sample_aspect_ratio: "4:3", side_data_list: [{ rotation: 90 }] });
|
||||
expect(shown).toEqual({ width: 1080, height: 2560 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMediaMetadata nb_frames duration cross-check", () => {
|
||||
afterEach(() => {
|
||||
vi.resetModules();
|
||||
|
||||
@@ -225,6 +225,7 @@ export interface VideoMetadata {
|
||||
colorRange?: string;
|
||||
pixelFormat?: string;
|
||||
sampleAspectRatio?: number;
|
||||
rotation?: number;
|
||||
/** Decoded frame count from the video stream's `nb_frames`. Omitted when the
|
||||
* container does not surface a reliable count (still images, malformed
|
||||
* streams, or muxes that require `-count_packets` to populate). Callers
|
||||
@@ -274,6 +275,7 @@ interface FFProbeStream {
|
||||
color_primaries?: string;
|
||||
color_space?: string;
|
||||
tags?: Record<string, string>;
|
||||
side_data_list?: { rotation?: number }[];
|
||||
disposition?: { attached_pic?: number };
|
||||
}
|
||||
|
||||
@@ -855,6 +857,24 @@ export function parseFrameRate(frameRateStr: string | undefined): number {
|
||||
return rounded > 0 ? rounded : 0.01;
|
||||
}
|
||||
|
||||
function displayRotation(stream: FFProbeStream): { rotation?: number } {
|
||||
const turn = Number(
|
||||
stream.side_data_list?.find((data) => data.rotation !== undefined)?.rotation ??
|
||||
stream.tags?.rotate ??
|
||||
0,
|
||||
);
|
||||
return Number.isFinite(turn) && turn % 360 !== 0 ? { rotation: turn } : {};
|
||||
}
|
||||
|
||||
export function displaySize(
|
||||
metadata: Pick<VideoMetadata, "width" | "height" | "sampleAspectRatio" | "rotation">,
|
||||
): { width: number; height: number } {
|
||||
const width = Math.round(metadata.width * (metadata.sampleAspectRatio ?? 1));
|
||||
return Math.abs(metadata.rotation ?? 0) % 180 === 90
|
||||
? { width: metadata.height, height: width }
|
||||
: { width, height: metadata.height };
|
||||
}
|
||||
|
||||
export function parseSampleAspectRatio(value: string | undefined): number | undefined {
|
||||
const [num, den] = (value ?? "").split(":").map(Number);
|
||||
if (!num || !den || !Number.isFinite(num) || !Number.isFinite(den)) return undefined;
|
||||
@@ -1052,6 +1072,7 @@ export async function extractMediaMetadata(filePath: string): Promise<VideoMetad
|
||||
colorRange: videoStream.color_range,
|
||||
pixelFormat,
|
||||
...(sampleAspectRatio === undefined ? {} : { sampleAspectRatio }),
|
||||
...displayRotation(videoStream),
|
||||
frames,
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -1327,6 +1327,19 @@ describe("collectVideoMetadataHints", () => {
|
||||
{ id: "video2", width: 1080, height: 1920 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("sizes a phone's portrait clip as it plays, not as it is stored", () => {
|
||||
const stored = { width: 1920, height: 1080, durationSeconds: 3 };
|
||||
expect(
|
||||
collectVideoMetadataHints([
|
||||
{ videoId: "portrait", metadata: { ...stored, rotation: -90 } },
|
||||
{ videoId: "upside-down", metadata: { ...stored, rotation: 180 } },
|
||||
]),
|
||||
).toEqual([
|
||||
{ id: "portrait", width: 1080, height: 1920 },
|
||||
{ id: "upside-down", width: 1920, height: 1080 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveRenderWorkerCount", () => {
|
||||
|
||||
@@ -109,6 +109,7 @@ import {
|
||||
getDrawElementVerificationDetails,
|
||||
augmentProtocolTimeoutError,
|
||||
augmentPageNavigationTimeoutError,
|
||||
displaySize,
|
||||
type MotionBlurOptions,
|
||||
} from "@hyperframes/engine";
|
||||
import {
|
||||
@@ -954,10 +955,7 @@ export function collectVideoReadinessSkipIds(
|
||||
|
||||
interface ExtractedVideoReadinessInput {
|
||||
videoId: string;
|
||||
metadata: {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
metadata: Parameters<typeof displaySize>[0];
|
||||
}
|
||||
|
||||
function hasUsableVideoDimensions(metadata: ExtractedVideoReadinessInput["metadata"]) {
|
||||
@@ -974,11 +972,7 @@ export function collectVideoMetadataHints(
|
||||
): CaptureVideoMetadataHint[] {
|
||||
return extractedVideos
|
||||
.filter((video) => hasUsableVideoDimensions(video.metadata))
|
||||
.map((video) => ({
|
||||
id: video.videoId,
|
||||
width: video.metadata.width,
|
||||
height: video.metadata.height,
|
||||
}))
|
||||
.map((video) => ({ id: video.videoId, ...displaySize(video.metadata) }))
|
||||
.sort((a, b) => a.id.localeCompare(b.id));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user