refactor: dedupe google video size parsing

This commit is contained in:
Peter Steinberger
2026-04-06 22:03:22 +01:00
parent c03071d36c
commit 79a84f070d

View File

@@ -25,19 +25,12 @@ function resolveConfiguredGoogleVideoBaseUrl(req: VideoGenerationRequest): strin
return configured ? normalizeGoogleApiBaseUrl(configured) : undefined;
}
function resolveAspectRatio(params: {
aspectRatio?: string;
size?: string;
}): "16:9" | "9:16" | undefined {
const direct = params.aspectRatio?.trim();
if (direct === "16:9" || direct === "9:16") {
return direct;
}
const size = params.size?.trim();
if (!size) {
function parseVideoSize(size: string | undefined): { width: number; height: number } | undefined {
const trimmed = size?.trim();
if (!trimmed) {
return undefined;
}
const match = /^(\d+)x(\d+)$/u.exec(size);
const match = /^(\d+)x(\d+)$/u.exec(trimmed);
if (!match) {
return undefined;
}
@@ -46,7 +39,22 @@ function resolveAspectRatio(params: {
if (!Number.isFinite(width) || !Number.isFinite(height)) {
return undefined;
}
return width >= height ? "16:9" : "9:16";
return { width, height };
}
function resolveAspectRatio(params: {
aspectRatio?: string;
size?: string;
}): "16:9" | "9:16" | undefined {
const direct = params.aspectRatio?.trim();
if (direct === "16:9" || direct === "9:16") {
return direct;
}
const parsedSize = parseVideoSize(params.size);
if (!parsedSize) {
return undefined;
}
return parsedSize.width >= parsedSize.height ? "16:9" : "9:16";
}
function resolveResolution(params: {
@@ -59,17 +67,11 @@ function resolveResolution(params: {
if (params.resolution === "1080P") {
return "1080p";
}
const size = params.size?.trim();
if (!size) {
const parsedSize = parseVideoSize(params.size);
if (!parsedSize) {
return undefined;
}
const match = /^(\d+)x(\d+)$/u.exec(size);
if (!match) {
return undefined;
}
const width = Number.parseInt(match[1] ?? "", 10);
const height = Number.parseInt(match[2] ?? "", 10);
const maxEdge = Math.max(width, height);
const maxEdge = Math.max(parsedSize.width, parsedSize.height);
return maxEdge >= 1920 ? "1080p" : maxEdge >= 1280 ? "720p" : undefined;
}