feat(video-from-script): 严格并行提交批次限制并增强额度错误检测

- 引入 `isQuotaError` 函数,统一检测 API 额度不足错误
- Kling 生成器改为严格分批提交并等待每批轮询完成,限制并发 ≤ 5
- phase-videos 重构为分批提交+轮询模式,支持额度不足时提前终止
- 提取 `applyPollResult` 和 `pollOpts` 工具函数,减少代码重复
- 新增批量提交的进度日志,显示当前批次和总数
This commit is contained in:
2026-05-16 22:44:09 +08:00
parent 44ab03e81c
commit 4495ea8af1
2 changed files with 138 additions and 117 deletions

View File

@@ -8,6 +8,30 @@
const path = require('path')
const { saveManifest, ensureDir, log, getManifestDir } = require('./pipeline-utils')
function isQuotaError(msg) {
if (!msg) return false
const s = msg.toLowerCase()
return /quota|limit|exceed|insufficient|余额|额度|超限|rate.?limit|too.?many/.test(s)
}
function applyPollResult(item, val, dir) {
if (val.ok && val.result?.file) {
item.video = path.relative(dir, val.result.file).replace(/\\/g, '/')
item.videoDuration = val.result.duration
item.status = 'done'
delete item.videoTaskId
} else if (val.item) {
if (val.isTaskFailure) {
item.status = 'failed'
item.error = val.error || '视频生成未返回文件'
delete item.videoTaskId
} else {
log('videos', ` item ${item.id} 生成超时(保留 taskId 待恢复): ${val.error}`)
item.status = 'pending'
}
}
}
async function phaseVideos(manifest, manifestPath, options) {
const dir = getManifestDir(manifestPath)
const videosDir = path.join(dir, 'videos')
@@ -16,10 +40,6 @@ async function phaseVideos(manifest, manifestPath, options) {
const accountConfig = options.accountConfig || {}
const videoModel = manifest.videoModel || accountConfig.videoModel || 'veo3-fast-frames'
// 筛选需要生视频的 item
// done — 正常流程,图片已确认且已上传
// pending / failed — 重试场景agent 只需将 item 设为 pending 即可触发再生
// 前提:有 url图片已上传+ videoPrompt且 confirmed 未被显式拒绝
const videoCandidates = manifest.items.filter(it => {
if (it.confirmed === false) return false
if (!it.url || !it.videoPrompt) return false
@@ -44,7 +64,6 @@ async function phaseVideos(manifest, manifestPath, options) {
console.log()
}
// 已有视频(本地或 OSS且状态为 done 的跳过,其余清理视频引用但保留 taskId 供恢复
const items = []
for (const it of videoCandidates) {
if (it.video || it.videoUrl) {
@@ -57,7 +76,6 @@ async function phaseVideos(manifest, manifestPath, options) {
}
if (items.length === 0) { log('videos', '无待处理 item跳过'); return }
// 选择生成器
let Api, pollFn
const modelLower = videoModel.toLowerCase()
if (modelLower.includes('grok')) {
@@ -72,6 +90,11 @@ async function phaseVideos(manifest, manifestPath, options) {
}
const ratio = manifest.format || '9:16'
const pollOpts = (item) => ({
outputDir: videosDir, aspectRatio: ratio,
imageUrl: item.url, lastFrameUrl: item.lastFrameUrl || '',
})
log('videos', `${items.length} 个, 模型: ${videoModel}`)
// Phase 1: 恢复已有任务(有 videoTaskId 的 item
@@ -92,12 +115,7 @@ async function phaseVideos(manifest, manifestPath, options) {
recovered.map(async (item) => {
try {
log('videos', ` 恢复 item ${item.id}: ${item.videoTaskId}`)
const result = await pollFn(item.videoTaskId, item.videoPrompt, {
outputDir: videosDir,
aspectRatio: ratio,
imageUrl: item.url,
lastFrameUrl: item.lastFrameUrl || '',
})
const result = await pollFn(item.videoTaskId, item.videoPrompt, pollOpts(item))
if (result.file) {
item.video = path.relative(dir, result.file).replace(/\\/g, '/')
item.videoDuration = result.duration
@@ -106,8 +124,7 @@ async function phaseVideos(manifest, manifestPath, options) {
log('videos', ` item ${item.id} 恢复成功`)
}
} catch (err) {
const isFail = err.isTaskFailure === true
if (isFail) {
if (err.isTaskFailure === true) {
log('videos', ` item ${item.id} 恢复失败(任务失败): ${err.message},将重新提交`)
delete item.videoTaskId
needSubmit.push(item)
@@ -123,14 +140,16 @@ async function phaseVideos(manifest, manifestPath, options) {
if (needSubmit.length === 0) { log('videos', '全部通过恢复完成'); return }
// Phase 2: 提交新任务(并发 5Kling 最大并发
// Phase 2+3: 分批提交+轮询(严格并发 ≤ 5等一批完成再提交下一批
const concurrency = 5
log('videos', `提交 ${needSubmit.length} 个新任务(并发: ${concurrency}...`)
const submitted = []
for (let i = 0; i < needSubmit.length; i += concurrency) {
const batch = needSubmit.slice(i, i + concurrency)
const batchResults = await Promise.allSettled(
const batch = needSubmit.slice(i, i + concurrency).filter(item => !item.videoTaskId)
if (batch.length === 0) continue
// 提交本批
const submitResults = await Promise.allSettled(
batch.map(async (item) => {
const extraOpts = item.lastFrameUrl
? { aspectRatio: ratio, lastFrameUrl: item.lastFrameUrl, mode: 'pro' }
@@ -143,65 +162,52 @@ async function phaseVideos(manifest, manifestPath, options) {
}
})
)
for (const r of batchResults) {
const submitted = []
let hitQuota = false
for (const r of submitResults) {
const val = r.status === 'fulfilled' ? r.value : { item: null, taskId: null, error: r.reason }
submitted.push(val)
if (val.item && val.taskId) {
val.item.videoTaskId = val.taskId
}
}
saveManifest(manifestPath, manifest)
}
// Phase 3: 轮询新任务
const pending = submitted.filter(s => s.taskId)
if (pending.length === 0) {
log('videos', '所有任务提交失败')
for (const s of submitted) {
if (s.item) { s.item.status = 'failed'; s.item.error = s.error || '提交失败' }
}
saveManifest(manifestPath, manifest)
return
}
log('videos', `等待 ${pending.length} 个视频生成...`)
const pollResults = await Promise.allSettled(
pending.map(async ({ item, taskId }) => {
try {
const result = await pollFn(taskId, item.videoPrompt, {
outputDir: videosDir,
aspectRatio: ratio,
imageUrl: item.url,
lastFrameUrl: item.lastFrameUrl || '',
})
return { item, result, ok: true }
} catch (err) {
return { item, error: err.message, ok: false, isTaskFailure: err.isTaskFailure === true }
}
})
)
for (const r of pollResults) {
const val = r.status === 'fulfilled'
? r.value
: { ok: false, error: r.reason?.message || String(r.reason), isTaskFailure: r.reason?.isTaskFailure === true }
if (val.ok && val.result.file) {
val.item.video = path.relative(dir, val.result.file).replace(/\\/g, '/')
val.item.videoDuration = val.result.duration
val.item.status = 'done'
delete val.item.videoTaskId
} else if (val.item) {
if (val.isTaskFailure) {
} else if (val.item && !val.taskId) {
val.item.status = 'failed'
val.item.error = val.error || '视频生成未返回文件'
delete val.item.videoTaskId
} else {
log('videos', ` item ${val.item.id} 生成超时(保留 taskId 待恢复): ${val.error}`)
val.item.status = 'pending'
val.item.error = val.error || '提交失败'
if (isQuotaError(val.error)) hitQuota = true
}
}
saveManifest(manifestPath, manifest)
if (hitQuota) {
log('videos', ` ⚠️ 额度不足,停止提交(跳过剩余 ${needSubmit.length - i - batch.length} 个任务)`)
break
}
// 轮询本批
const pending = submitted.filter(s => s.taskId)
if (pending.length === 0) continue
const end = Math.min(i + concurrency, needSubmit.length)
log('videos', ` [${i + 1}-${end}/${needSubmit.length}] 等待 ${pending.length} 个视频生成...`)
const pollResults = await Promise.allSettled(
pending.map(async ({ item, taskId }) => {
try {
const result = await pollFn(taskId, item.videoPrompt, pollOpts(item))
return { item, result, ok: true }
} catch (err) {
return { item, error: err.message, ok: false, isTaskFailure: err.isTaskFailure === true }
}
})
)
for (const r of pollResults) {
const val = r.status === 'fulfilled'
? r.value
: { ok: false, error: r.reason?.message || String(r.reason), isTaskFailure: r.reason?.isTaskFailure === true }
applyPollResult(val.item || {}, val, dir)
}
saveManifest(manifestPath, manifest)
}
// 上传视频到 OSS