优化接口

This commit is contained in:
zhishi 2026-05-01 18:42:29 +08:00
parent 56aedee011
commit bd60524fde
4 changed files with 116 additions and 121 deletions

View File

@ -90,15 +90,15 @@ export default router.post(
})), })),
), ),
); );
const generateTask = async (item: (typeof storyboardData)[number]) => { const generateTask = async (item: (typeof storyboardData)[number]) => {
const repeloadObj = { const repeloadObj = {
prompt: item.prompt!, prompt: item.prompt!,
size: projectSettingData?.imageQuality as "1K" | "2K" | "4K", size: projectSettingData?.imageQuality as "1K" | "2K" | "4K",
aspectRatio: projectSettingData?.videoRatio as `${number}:${number}`, aspectRatio: projectSettingData?.videoRatio as `${number}:${number}`,
}; };
try {
await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`) const imageCls = await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`).run(
.run(
{ {
referenceList: await getAssetsImageBase64(assetRecord[item.id!] || []), referenceList: await getAssetsImageBase64(assetRecord[item.id!] || []),
...repeloadObj, ...repeloadObj,
@ -109,25 +109,22 @@ export default router.post(
relatedObjects: JSON.stringify(repeloadObj), relatedObjects: JSON.stringify(repeloadObj),
projectId: projectId, projectId: projectId,
}, },
) );
.then(async (imageCls) => { const savePath = `/${projectId}/assets/${scriptId}/${u.uuid()}.jpg`;
const savePath = `/${projectId}/assets/${scriptId}/${u.uuid()}.jpg`; await imageCls.save(savePath);
await imageCls.save(savePath); await u.db("o_storyboard").where("id", item.id).update({
await u.db("o_storyboard").where("id", item.id).update({ filePath: savePath,
filePath: savePath, state: "已完成",
state: "已完成",
});
})
.catch(async (e) => {
await u
.db("o_storyboard")
.where("id", item.id)
.update({
filePath: "",
reason: u.error(e).message,
state: "生成失败",
});
}); });
} catch (e) {
u.db("o_storyboard")
.where("id", item.id)
.update({
filePath: "",
reason: u.error(e).message,
state: "生成失败",
});
}
}; };
// 按 concurrentCount 控制并发数,分批执行;跳过 shouldGenerateImage === 0 的分镜 // 按 concurrentCount 控制并发数,分批执行;跳过 shouldGenerateImage === 0 的分镜
let generateList = []; let generateList = [];

View File

@ -94,55 +94,45 @@ export default router.post(
); );
res.status(200).send(success(tasks.map((t) => ({ videoId: t.videoId, trackId: t.trackId })))); res.status(200).send(success(tasks.map((t) => ({ videoId: t.videoId, trackId: t.trackId }))));
for (const { videoId, videoPath, prompt, duration, images } of tasks) {
// 分批执行3个一批 // 所有任务全部并发后台执行,完全不阻塞任何进程
(async () => { const base64 = await Promise.all(
const batchSize = 3; images.map(async (item) => {
for (let i = 0; i < tasks.length; i += batchSize) { if (!item) return null;
const batch = tasks.slice(i, i + batchSize); return { base64: await u.oss.getImageBase64(item.path), type: item.sources == "audio" ? "audio" : "image" };
await Promise.all( }),
batch.map(async ({ videoId, videoPath, prompt, duration, images }) => { );
// 调用时再转 base64 const relatedObjects = { projectId, videoId, scriptId, type: "视频" };
const base64 = await Promise.all( const aiVideo = u.Ai.Video(model);
images.map(async (item) => { aiVideo
if (!item) return null; .run(
return { base64: await u.oss.getImageBase64(item.path), type: item.sources == "audio" ? "audio" : "image" }; {
}), prompt,
); referenceList: base64.filter(Boolean) as ReferenceList[],
const relatedObjects = { projectId, videoId, scriptId, type: "视频" }; mode: modeData.length > 0 ? modeData : mode,
const aiVideo = u.Ai.Video(model); duration,
return aiVideo aspectRatio: (ratio?.videoRatio as "16:9" | "9:16") || "16:9",
.run( resolution,
{ audio,
prompt, },
referenceList: base64.filter(Boolean) as ReferenceList[], {
mode: modeData.length > 0 ? modeData : mode, projectId,
duration, taskClass: "视频生成",
aspectRatio: (ratio?.videoRatio as "16:9" | "9:16") || "16:9", describe: "根据提示词生成视频",
resolution, relatedObjects: JSON.stringify(relatedObjects),
audio, },
}, )
{ .then(async () => await aiVideo.save(videoPath))
projectId, .then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
taskClass: "视频生成", .catch(async (error: any) => {
describe: "根据提示词生成视频", await u
relatedObjects: JSON.stringify(relatedObjects), .db("o_video")
}, .where("id", videoId)
) .update({
.then(() => aiVideo.save(videoPath)) state: "生成失败",
.then(() => u.db("o_video").where("id", videoId).update({ state: "生成成功" })) errorReason: u.error(error).message,
.catch(async (error: any) => { });
await u });
.db("o_video") }
.where("id", videoId)
.update({
state: "生成失败",
errorReason: u.error(error).message,
});
});
}),
);
}
})();
}, },
); );

View File

@ -84,37 +84,34 @@ export default router.post(
videoTrackId: trackId, videoTrackId: trackId,
}); });
res.status(200).send(success(videoId)); res.status(200).send(success(videoId));
(async () => { const relatedObjects = {
try { projectId,
const relatedObjects = { videoId,
scriptId,
type: "视频",
};
const aiVideo = u.Ai.Video(model);
aiVideo
.run(
{
prompt,
referenceList: base64.filter(Boolean) as ReferenceList[],
mode: modeData.length > 0 ? modeData : mode,
duration,
aspectRatio: (ratio?.videoRatio as "16:9" | "9:16") || "16:9",
resolution,
audio,
},
{
projectId, projectId,
videoId, taskClass: "视频生成",
scriptId, describe: "根据提示词生成视频",
type: "视频", relatedObjects: JSON.stringify(relatedObjects),
}; },
const aiVideo = u.Ai.Video(model); )
.then(async () => await aiVideo.save(videoPath))
console.log("%c Line:47 🍩 modeData", "background:#93c0a4", modeData); .then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
await aiVideo.run( .catch(async (error: any) => {
{
prompt,
referenceList: base64.filter(Boolean) as ReferenceList[],
mode: modeData.length > 0 ? modeData : mode,
duration,
aspectRatio: (ratio?.videoRatio as "16:9" | "9:16") || "16:9",
resolution,
audio,
},
{
projectId,
taskClass: "视频生成",
describe: "根据提示词生成视频",
relatedObjects: JSON.stringify(relatedObjects),
},
);
await aiVideo.save(videoPath);
await u.db("o_video").where("id", videoId).update({ state: "生成成功" });
} catch (error: any) {
await u await u
.db("o_video") .db("o_video")
.where("id", videoId) .where("id", videoId)
@ -122,7 +119,6 @@ export default router.post(
state: "生成失败", state: "生成失败",
errorReason: u.error(error).message, errorReason: u.error(error).message,
}); });
} });
})();
}, },
); );

View File

@ -152,11 +152,12 @@ async function withTaskRecord<T>(
const taskRecord = await u.task(projectId, taskClass, model, { describe: describe, content: relatedObjects }); const taskRecord = await u.task(projectId, taskClass, model, { describe: describe, content: relatedObjects });
try { try {
const result = await fn(modelName, false, 0); const result = await fn(modelName, false, 0);
taskRecord(1); taskRecord(1);
return result; return result;
} catch (e) { } catch (e) {
taskRecord(-1, u.error(e).message); taskRecord(-1, u.error(e).message);
throw e; throw new Error(u.error(e).message);
} }
} }
@ -258,9 +259,11 @@ class AiImage {
return this; return this;
}; };
if (taskRecord) { if (taskRecord) {
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec); await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
return this;
} }
return exec(modelName); await exec(modelName);
return this;
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);
@ -294,18 +297,24 @@ class AiVideo {
} }
async run(input: VideoConfig, taskRecord?: TaskRecord) { async run(input: VideoConfig, taskRecord?: TaskRecord) {
const modelName = await resolveModelName(this.key); const modelName = await resolveModelName(this.key);
const exec = async (mn: `${string}:${string}`) => { try {
const fn = await getVendorTemplateFn("videoRequest", mn); const exec = async (mn: `${string}:${string}`) => {
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input); const fn = await getVendorTemplateFn("videoRequest", mn);
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
this.result = await fn(input); this.result = await fn(input);
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
};
if (taskRecord) {
await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
return this;
}
await exec(modelName);
return this; return this;
}; } catch (e) {
if (taskRecord) { throw e;
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
} }
return exec(modelName);
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);
@ -321,16 +330,19 @@ class AiAudio {
async run(input: VideoConfig, taskRecord?: TaskRecord) { async run(input: VideoConfig, taskRecord?: TaskRecord) {
const modelName = await resolveModelName(this.key); const modelName = await resolveModelName(this.key);
const exec = async (mn: `${string}:${string}`) => { const exec = async (mn: `${string}:${string}`) => {
const fn = await getVendorTemplateFn("ttsRequest", mn); try {
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input); const fn = await getVendorTemplateFn("ttsRequest", mn);
this.result = await fn(input); await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result); this.result = await fn(input);
return this;
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
return this;
} catch (e) {}
}; };
if (taskRecord) { if (taskRecord) {
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec); return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
} }
return exec(modelName); return await exec(modelName);
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);