nex_animation/frontend/src/views/tabs/Delivery.vue

308 lines
19 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<script setup lang="ts">
/**
* 导出与成本(图2 节点 ⑫ compose → export)
* 出片走任务队列:合成失败可重试,成片留档 + 成本台账 + 生成记录(可复现提示词)
*/
import { computed, onMounted, reactive, ref } from 'vue'
import * as api from '../../api'
import { notify, pctx, refresh } from '../../project'
import { presetLabel, presets, store } from '../../store'
import { fmtAgo, fmtBytes, fmtMs, fmtMoney, fmtSeconds, fmtTime, statusClass, statusLabel } from '../../format'
import { fromExport } from '../../media'
import { openPreview } from '../../preview'
import MediaThumb from '../../components/MediaThumb.vue'
import FSelect from '../../components/FSelect.vue'
import AutoRefresh from '../../components/AutoRefresh.vue'
const error = ref('')
const busy = ref('')
const exportList = ref<any[]>([])
const costs = ref<any>(null)
const costItems = ref<any[]>([])
const generations = ref<any[]>([])
const report = ref<any>(null)
const timelines = ref<any[]>([])
const showAllCost = ref(false)
const composeForm = reactive({ presets: ['web_1080p'] as string[], name: '', subtitle_mode: '', timeline_id: '' as string, priority: 2 })
const currency = computed(() => costs.value?.currency || 'CNY')
const busyExports = computed(() => exportList.value.filter((e) => ['queued', 'running', 'pending'].includes(e.status)))
const succeeded = computed(() => exportList.value.filter((e) => e.status === 'succeeded'))
const presetHints: Record<string, string> = {
draft_720p: '1280×720 · CRF28 · veryfast(试片最快)',
vertical_720p: '720×1280 · CRF24 · medium(9:16 竖屏试片)',
web_1080p: '1920×1080 · CRF21 · medium(平台投放默认)',
vertical_1080p: '1080×1920 · CRF21 · medium(9:16 竖屏投放)',
hq_1080p: '1920×1080 · CRF18 · slow(高质量)',
uhd_4k: '3840×2160 · CRF18 · slow(超清,耗时最长)'
}
async function load(silent = true) {
if (!pctx.id) return
if (!silent) busy.value = '读取交付数据'
try {
const [ex, cs, gen, rep, tl] = await Promise.all([
api.get(`/api/projects/${pctx.id}/exports`),
api.get(`/api/projects/${pctx.id}/costs`),
api.get(`/api/projects/${pctx.id}/generations`),
api.get(`/api/projects/${pctx.id}/timeline/report`).catch(() => null),
api.get(`/api/projects/${pctx.id}/timelines`).catch(() => ({ timelines: [] }))
])
exportList.value = ex.exports || []
costs.value = cs.cost
// /costs 把计费明细放在顶层 items,不在 cost 里;之前取 cs.cost.items 永远是空的
costItems.value = cs.items || []
generations.value = gen.generations || []
report.value = rep?.report || null
timelines.value = tl.timelines || []
error.value = ''
} catch (exc: any) { error.value = exc.message }
finally { busy.value = '' }
}
async function run(label: string, fn: () => Promise<any>, message?: (res: any) => string) {
busy.value = label
error.value = ''
try {
const res = await fn()
const text = message ? message(res) : ''
if (text) notify(text)
await Promise.all([load(), refresh()])
return res
} catch (exc: any) { error.value = exc.message; return null }
finally { busy.value = '' }
}
/** 出片:多选预设时依次入队(同一时间线复用,避免逐个点击等页面刷新) */
async function compose() {
const chosen = composeForm.presets.length ? composeForm.presets : ['web_1080p']
busy.value = chosen.length > 1 ? `依次合成 ${chosen.length} 个预设` : '合成导出中'
error.value = ''
const done: string[] = []
const queued: number[] = []
const bad: string[] = []
try {
for (const preset of chosen) {
try {
const r = await api.post(`/api/projects/${pctx.id}/compose`, {
preset,
name: chosen.length === 1 ? composeForm.name.trim() : '',
subtitle_mode: composeForm.subtitle_mode || null,
timeline_id: composeForm.timeline_id ? Number(composeForm.timeline_id) : null,
priority: composeForm.priority
})
const list = r?.exports || []
const ok = list.filter((e: any) => e.status === 'succeeded')
if (ok.length) done.push(`${presetLabel[preset] || preset}:${ok[0].name}(${fmtMs(ok[0].duration_ms)} · ${fmtBytes(ok[0].size_bytes)})`)
else if (!list.length) queued.push(r?.task_id)
else bad.push(`${presetLabel[preset] || preset}:${list[0]?.error_message || statusLabel(list[0]?.status)}`)
} catch (exc: any) { bad.push(`${presetLabel[preset] || preset}:${exc.message}`) }
}
await Promise.all([load(), refresh()])
const parts: string[] = []
if (done.length) parts.push(done.join(';'))
if (queued.length) parts.push(`${queued.length} 个合成任务已入队(#${queued.join('、#')}),等待 worker 执行`)
if (bad.length) parts.push(`未完成 ${bad.length} 个:${bad.join(';')}`)
if (parts.length) notify(parts.join('。'))
} finally { busy.value = '' }
}
/** 导出成片 → 统一预览队列(点任意一行都能 ←/→ 翻所有成片) */
const exportItems = computed<any[]>(() => exportList.value.map((rec) => fromExport(rec)).filter(Boolean))
function previewExport(rec: any) {
const at = exportItems.value.findIndex((m) => m.raw?.id === rec.id)
openPreview(exportItems.value, at < 0 ? 0 : at)
}
const retryExport = (rec: any) => run('重试导出', () => api.post(`/api/tasks/${rec.task_id}/retry`, {}), () => `合成任务 #${rec.task_id} 已重新入队`)
function togglePreset(p: string) {
const at = composeForm.presets.indexOf(p)
if (at >= 0) composeForm.presets.splice(at, 1)
else composeForm.presets.push(p)
}
function download(rec: any) {
return api.href(`/api/exports/${rec.id}/download`)
}
const shownCostItems = computed(() => (showAllCost.value ? costItems.value : costItems.value.slice(0, 12)))
onMounted(() => { void load(false) })
</script>
<template>
<div class="tabstack">
<div class="between" style="margin-bottom:12px">
<div class="row">
<span class="chip">成片 {{ succeeded.length }} / {{ exportList.length }}</span>
<span class="chip">累计成本 {{ fmtMoney(costs?.total_cost, currency) }}</span>
<span class="chip">生成 {{ costs?.generations?.count ?? 0 }} 次 / {{ (costs?.generations?.seconds ?? 0).toFixed(1) }}s</span>
<span class="chip">待结算预估 {{ fmtMoney(costs?.estimated_pending, currency) }}</span>
<span v-if="report" class="tag" :class="report.ready ? 'ok' : 'bad'">{{ report.ready ? '时间线可导出' : '时间线有阻塞' }}</span>
</div>
<div class="row">
<span v-if="busy" class="muted"><span class="spin"></span> {{ busy }}…</span>
<AutoRefresh :busy="!!busy" :every="busyExports.length ? 4000 : 15000" :paused="!!busy" @refresh="load()" />
</div>
</div>
<p v-if="error" class="error" style="margin-bottom:12px">{{ error }}</p>
<p v-if="report && !report.ready" class="error" style="margin-bottom:12px">
时间线尚未就绪,导出会被拒绝(409):<span v-for="(x, i) in report.issues.filter((y: any) => y.level === 'error')" :key="i">{{ x.message }} </span>
<router-link class="link" :to="`/projects/${pctx.id}/timeline`">前往时间线修复 ↗</router-link></p>
<!-- ===== 出片 ===== -->
<div class="grid2">
<section class="panel">
<div class="panel-head"><div><small class="eyebrow">PIPELINE · 导出成片</small><h2>合成导出</h2></div></div>
<div class="chipbar">
<span class="chipbar-label">出片预设(可多选,依次入队)</span>
<button v-for="p in (store.meta?.presets || presets)" :key="p" type="button" class="chip"
:class="{ on: composeForm.presets.includes(p) }"
:aria-pressed="composeForm.presets.includes(p)"
:title="presetHints[p]"
@click="togglePreset(p)">{{ presetLabel[p] || p }}</button>
</div>
<div class="frow">
<label class="fld"><span>时间线</span><FSelect v-model="composeForm.timeline_id">
<option value="">当前时间线</option>
<option v-for="t in timelines" :key="t.id" :value="String(t.id)">v{{ t.version }} · {{ t.name }}({{ fmtMs(t.total_duration_ms) }})</option>
</FSelect></label>
<label class="fld"><span>字幕</span><FSelect v-model="composeForm.subtitle_mode">
<option value="">跟随时间线设置(默认内嵌软字幕轨)</option>
<option value="embed">内嵌软字幕轨(可开关)</option>
<option value="burn">烧录进画面(硬字幕)</option>
<option value="both">烧录 + 内嵌</option>
<option value="sidecar">只出外挂 SRT</option>
<option value="off">不输出字幕</option>
</FSelect></label>
<label class="fld narrow"><span>优先级</span><input v-model.number="composeForm.priority" type="number" min="0" max="9" /></label>
<label class="fld"><span>成片名称(可选)</span><input v-model="composeForm.name" placeholder="留空自动命名:项目名-时间线-时间戳" /></label>
</div>
<div class="actions">
<button class="primary" :disabled="!!busy || !report || !report.ready || !composeForm.presets.length" @click="compose"
:title="report && report.ready ? `将依次合成 ${composeForm.presets.length} 个预设` : '时间线未就绪时不允许出片'">▶ 开始合成导出{{ composeForm.presets.length > 1 ? `(${composeForm.presets.length} 个预设)` : '' }}</button>
<router-link class="link" :to="`/projects/${pctx.id}/timeline`">先检查时间线 ↗</router-link>
</div>
<p v-if="store.health?.ffmpeg_capabilities && !store.health.ffmpeg_capabilities.subtitle_burn" class="linehint">
⚠ 本机 ffmpeg 缺 libass(无 subtitles/ass 滤镜),烧录模式会自动降级为内嵌软字幕轨;
装好完整版后设 FFMPEG_BIN 指向它即可。当前使用:{{ store.health.ffmpeg_capabilities.ffmpeg_path || '未探测' }}。</p>
</section>
<section class="panel">
<div class="panel-head"><div><small class="eyebrow">COST LEDGER</small><h2>成本台账</h2></div>
<span class="muted">{{ costs?.records ?? 0 }} 条计费记录</span></div>
<div class="stats" style="grid-template-columns:repeat(2,1fr);margin-bottom:12px">
<div class="stat"><small>已发生成本</small><b>{{ fmtMoney(costs?.total_cost, currency) }}</b></div>
<div class="stat"><small>待生成预估</small><b>{{ fmtMoney(costs?.estimated_pending, currency) }}</b></div>
</div>
<div class="tablebox" style="max-height:170px">
<table class="tbl">
<thead><tr><th>维度</th><th>取值</th><th class="r">金额</th><th class="r">记录数</th></tr></thead>
<tbody>
<tr v-for="row in (costs?.by_kind || [])" :key="'k' + row.key">
<td class="muted">任务类型</td><td>{{ statusLabel(row.key) }}</td>
<td class="r mono">{{ fmtMoney(row.cost, currency) }}</td><td class="r mono">{{ row.count }}</td></tr>
<tr v-for="row in (costs?.by_provider || [])" :key="'p' + row.key">
<td class="muted">Provider</td><td class="mono">{{ row.key }}</td>
<td class="r mono">{{ fmtMoney(row.cost, currency) }}</td><td class="r mono">{{ row.count }}</td></tr>
<tr v-for="row in (costs?.by_model || [])" :key="'m' + row.key">
<td class="muted">模型</td><td class="mono">{{ row.key }}</td>
<td class="r mono">{{ fmtMoney(row.cost, currency) }}</td><td class="r mono">{{ row.count }}</td></tr>
<tr v-if="!costs?.by_model?.length"><td colspan="4" class="muted" style="text-align:center;padding:16px;line-height:1.7">
暂无计费明细:本项目使用自建模型,但未在模型管理中心配置单价,服务端因此不会写入计费记录;
「待生成预估」只统计排队中 / 执行中 / 已规划的任务,全部跑完后自然归零。<br />
可量化的投入见下方<b>生成记录</b>({{ costs?.generations?.count ?? 0 }} 次调用 / {{ (costs?.generations?.seconds ?? 0).toFixed(1) }} 秒)。
<router-link class="link" to="/models">去配置模型单价 ↗</router-link></td></tr>
</tbody>
</table>
</div>
</section>
</div>
<!-- ===== 成片列表 ===== -->
<section class="panel">
<div class="panel-head"><div><small class="eyebrow">EXPORTS</small><h2 title="点「▶ 预览」在统一预览器中播放,多份成片可用左右方向键连续比对、Esc 关闭;出片参数 H.264 high profile · yuv420p · AAC 48kHz 双声道 · faststart,逐预设调整 CRF 与 x264 preset。">成片记录</h2></div>
<span class="muted">媒体由后端 <span class="mono">/media</span> 直出,下载走 <span class="mono">/api/exports/:id/download</span></span></div>
<div class="tablebox" style="max-height:420px">
<table class="tbl">
<thead><tr><th class="c">#</th><th>名称 / 预设</th><th>状态</th><th class="c">规格</th><th class="r">时长</th><th class="r">大小</th><th class="r">耗时</th><th>校验和</th><th class="r">操作</th></tr></thead>
<tbody>
<tr v-for="rec in exportList" :key="rec.id">
<td class="c mono">{{ rec.id }}</td>
<td><div class="row" style="align-items:flex-start;gap:9px;flex-wrap:nowrap">
<MediaThumb :item="exportItems[exportItems.findIndex((m) => m.raw?.id === rec.id)]" size="xs" width="60px"
:queue="exportItems" :index="exportItems.findIndex((m) => m.raw?.id === rec.id)" :radius="5" />
<div class="grow"><b>{{ rec.name }}</b><div class="muted mono">{{ rec.preset }} · timeline#{{ rec.timeline_id }} · task#{{ rec.task_id || '—' }}</div>
<div v-if="rec.error_message" class="error">{{ rec.error_message }}</div></div></div></td>
<td><span :class="statusClass(rec.status)">{{ statusLabel(rec.status) }}</span>
<div v-if="rec.subtitle_burned" class="subline" style="display:block"><span class="tag info">已烧字幕</span></div></td>
<td class="c mono">{{ rec.width || '?' }}×{{ rec.height || '?' }}<span v-if="rec.fps"> @{{ rec.fps }}</span></td>
<td class="r mono">{{ fmtMs(rec.duration_ms) }}</td>
<td class="r mono">{{ fmtBytes(rec.size_bytes) }}</td>
<td class="r mono muted">{{ rec.elapsed_ms ? (rec.elapsed_ms / 1000).toFixed(1) + 's' : '—' }}</td>
<td class="mono muted" :title="rec.checksum ? '完整校验和为文件 SHA-256 前 32 位(上传超过 16MB 时按流式截断计算)' : '未记录校验和'">{{ (rec.checksum || '').slice(0, 10) || '—' }}</td>
<td class="r">
<div class="row" style="justify-content:flex-end">
<button v-if="rec.status === 'succeeded'" class="outline tiny" @click="previewExport(rec)">▶ 预览</button>
<a v-if="rec.status === 'succeeded'" class="link" :href="download(rec)">下载 ↗</a>
<button v-else-if="rec.task_id && ['failed', 'cancelled'].includes(rec.status)" class="outline tiny" @click="retryExport(rec)">重试</button>
<span v-else class="muted">—</span>
</div></td>
</tr>
<tr v-if="!exportList.length"><td colspan="9" class="muted" style="text-align:center;padding:22px">还没有成片。先在时间线页通过交付预检,再回到这里出片。</td></tr>
</tbody>
</table>
</div>
</section>
<!-- ===== 生成记录 ===== -->
<section class="panel">
<div class="panel-head"><div><small class="eyebrow">GENERATION LOG</small><h2>生成记录(可复现)</h2></div>
<div class="row"><span class="muted">{{ generations.length }} 条</span>
<button class="outline tiny" @click="showAllCost = !showAllCost">{{ showAllCost ? '收起明细' : '展开计费明细' }}</button></div></div>
<div class="tablebox" style="max-height:360px">
<table class="tbl">
<thead><tr><th class="c">#</th><th>镜头 / 类型</th><th>Provider / 模型</th><th>提示词</th><th class="r">时长</th><th class="r">成本</th><th class="c">尝试</th><th class="c">时间</th></tr></thead>
<tbody>
<tr v-for="g in generations" :key="g.id">
<td class="c mono">{{ g.id }}</td>
<td class="mono">shot#{{ g.shot_id || '—' }}<div class="muted">{{ statusLabel(g.kind) }}</div></td>
<td class="mono muted">{{ g.provider }}/{{ g.model }}</td>
<td class="wrap">{{ (g.prompt || '').slice(0, 130) }}<span v-if="(g.prompt || '').length > 130">…</span>
<div v-if="g.params && Object.keys(g.params).length" class="muted mono">{{ JSON.stringify(g.params).slice(0, 120) }}</div>
<div v-if="g.asset_paths?.length" class="muted mono">参考素材 {{ g.asset_paths.length }} 个 · job {{ g.provider_job_id || '—' }}</div></td>
<td class="r mono">{{ fmtSeconds(g.duration_seconds) }}</td>
<td class="r mono">{{ fmtMoney(g.cost, g.currency || currency) }}</td>
<td class="c mono">{{ g.attempts }}</td>
<td class="c mono muted" :title="fmtTime(g.created_at)">{{ fmtAgo(g.created_at) }}</td>
</tr>
<tr v-if="!generations.length"><td colspan="8" class="muted" style="text-align:center;padding:22px">暂无生成记录。</td></tr>
</tbody>
</table>
</div>
<div v-if="showAllCost && costItems.length" class="tablebox" style="max-height:280px;margin-top:12px">
<table class="tbl">
<thead><tr><th class="c">#</th><th>类型</th><th>Provider / 模型</th><th class="r">数量</th><th class="r">单价</th><th class="r">金额</th><th class="c">时间</th></tr></thead>
<tbody>
<tr v-for="it in shownCostItems" :key="it.id">
<td class="c mono">{{ it.id }}</td><td>{{ statusLabel(it.kind) }}</td>
<td class="mono muted">{{ it.provider }}/{{ it.model }}</td>
<td class="r mono">{{ it.quantity }} {{ it.unit }}</td>
<td class="r mono">{{ fmtMoney(it.unit_price, it.currency || currency) }}</td>
<td class="r mono">{{ fmtMoney(it.cost, it.currency || currency) }}</td>
<td class="c mono muted">{{ fmtTime(it.created_at) }}</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</template>
<style scoped>
.subline { display: block; margin-top: 3px; }
.th-sub { font-weight: 400; letter-spacing: 0; color: var(--c-text-faint); font-size: 10px; margin-top: 2px }
</style>