ai-chatbot/lib/video-canvas/video-upscale-contract.ts

34 lines
1.0 KiB
TypeScript

import { z } from "zod";
export const videoUpscaleSchema = z.object({
url: z
.string()
.url()
.refine((v) => /^https?:\/\//.test(v)),
resolution: z.enum(["1080P", "2K", "4K"]),
});
export type VideoUpscaleInput = z.infer<typeof videoUpscaleSchema>;
export function upscaleDimensions(
width: number,
height: number,
resolution: VideoUpscaleInput["resolution"]
) {
if (![width, height].every((v) => Number.isInteger(v) && v > 0))
throw Error("视频尺寸无效");
const [long, short] =
resolution === "4K"
? [3840, 2160]
: resolution === "2K"
? [2560, 1440]
: [1920, 1080];
const ratio = Math.min(
(width >= height ? long : short) / width,
(width >= height ? short : long) / height
);
if (ratio <= 1) throw Error("源视频已达到所选分辨率,请选择更高分辨率");
if (ratio > 4) throw Error("单次 AI 超分最大 4 倍,请先选择较低目标分辨率");
return {
width: Math.floor((width * ratio) / 2) * 2,
height: Math.floor((height * ratio) / 2) * 2,
};
}