89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import "../type";
|
|
import axios from "axios";
|
|
import { pollTask, validateVideoConfig } from "@/utils/ai/utils";
|
|
|
|
export default async (input: VideoConfig, config: AIConfig) => {
|
|
if (!config.apiKey) throw new Error("缺少API Key");
|
|
|
|
// const { owned, images, hasStartEndType } = validateVideoConfig(input, config);
|
|
const hasStartEndType = input.mode === "startEnd";
|
|
const authorization = "Bearer " + config.apiKey.replace(/^Bearer\s*/i, "").trim();
|
|
const baseUrl = config.baseURL ?? "https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks";
|
|
const images = input.imageBase64 || [];
|
|
|
|
// 构建图片内容
|
|
const imageContent = images.map((base64, index) => {
|
|
const item: Record<string, any> = {
|
|
type: "image_url",
|
|
image_url: { url: base64 },
|
|
};
|
|
if (hasStartEndType) {
|
|
item.role = index === 0 ? "first_frame" : "last_frame";
|
|
} else {
|
|
item.role = "reference_image";
|
|
}
|
|
return item;
|
|
});
|
|
|
|
// 构建请求体
|
|
const requestBody: Record<string, any> = {
|
|
model: config.model,
|
|
content: [{ type: "text", text: input.prompt }, ...imageContent],
|
|
duration: input.duration,
|
|
resolution: input.resolution,
|
|
watermark: false,
|
|
};
|
|
|
|
// 仅当模型支持音频时才添加 generate_audio 字段
|
|
if (typeof input?.audio == "boolean") {
|
|
requestBody.generate_audio = input.audio ?? false;
|
|
}
|
|
|
|
// 创建视频生成任务
|
|
const createResponse = await axios.post(baseUrl, requestBody, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: authorization,
|
|
},
|
|
});
|
|
console.log("%c Line:44 🍡 createResponse", "background:#2eafb0", createResponse.data);
|
|
|
|
const taskId = createResponse.data.id;
|
|
|
|
if (!taskId) throw new Error("视频任务创建失败");
|
|
|
|
// 轮询任务状态
|
|
return await pollTask(async () => {
|
|
const data = await axios.get(`${baseUrl}/${taskId}`, {
|
|
headers: { Authorization: authorization },
|
|
});
|
|
console.log("%c Line:62 🥕 data.data", "background:#e41a6a", data.data);
|
|
|
|
const { status, content, error } = data.data;
|
|
|
|
switch (status) {
|
|
case "succeeded":
|
|
case "completed":
|
|
return { completed: true, url: content?.video_url };
|
|
case "failed":
|
|
case "cancelled":
|
|
case "expired":
|
|
let errorMsg = "";
|
|
try {
|
|
errorMsg = typeof error === "string" ? error : JSON.stringify(error);
|
|
} catch (e) {
|
|
errorMsg = error || "";
|
|
}
|
|
return { completed: false, error: `任务${status}: ${errorMsg}` };
|
|
case "queued":
|
|
case "running":
|
|
case "unknown":
|
|
case "submit":
|
|
case "in_progress":
|
|
return { completed: false };
|
|
default:
|
|
return { completed: false, error: `未知状态: ${status}` };
|
|
}
|
|
});
|
|
};
|