349 lines
15 KiB
Python
349 lines
15 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""AI 一键出片「自动封面」端到端:mock 掉对话/生图网关(无需真实 Key),
|
||
真实跑完向导 4 步,断言:
|
||
1. 确认页出现封面预览行且状态为「AI 封面文案 · 大字发光」
|
||
2. 生成后时间轴第一个元素是「视频封面」图片、时长 3 秒、在片头
|
||
3. 字幕从封面之后才开始(第一条字幕 BeginTime >= 3000ms)
|
||
|
||
用法:python3 scripts/e2e_quickvideo_cover.py
|
||
"""
|
||
import sys, time, json, base64
|
||
from playwright.sync_api import sync_playwright
|
||
|
||
BASE = "https://127.0.0.1:3005"
|
||
APPID = "qv_cover_%d" % int(time.time())
|
||
STAMP = time.strftime("%H:%M:%S")
|
||
|
||
|
||
def log(msg):
|
||
print("[%s] %s" % (STAMP, msg), flush=True)
|
||
|
||
|
||
GUARD = """async () => {
|
||
try {
|
||
const root = await navigator.storage.getDirectory();
|
||
const db = await new Promise((res, rej) => {
|
||
const rq = indexedDB.open('H5VideoLocalDB');
|
||
rq.onupgradeneeded = () => rq.result.createObjectStore('folderHandles');
|
||
rq.onsuccess = () => res(rq.result);
|
||
rq.onerror = () => rej(rq.error);
|
||
});
|
||
const done = await new Promise((res, rej) => {
|
||
const tx = db.transaction('folderHandles', 'readwrite');
|
||
const st = tx.objectStore('folderHandles');
|
||
const rec = { id: 'root', handle: root };
|
||
if (st.keyPath) st.put(rec); else st.put(rec, 'root');
|
||
tx.oncomplete = () => res(true);
|
||
tx.onerror = () => rej(tx.error);
|
||
});
|
||
db.close();
|
||
return done;
|
||
} catch (e) { return String(e); }
|
||
}"""
|
||
|
||
# 1280x720 纯色 PNG(mock 生图返回 b64_json)
|
||
def make_cover_png_b64():
|
||
from PIL import Image
|
||
import io
|
||
im = Image.new("RGB", (1280, 720), (30, 90, 200))
|
||
buf = io.BytesIO()
|
||
im.save(buf, format="PNG")
|
||
return base64.b64encode(buf.getvalue()).decode()
|
||
|
||
|
||
COPY_JSON = json.dumps({
|
||
"segments": [
|
||
{"text": "第一段测试口播内容,介绍这张封面", "image": 1},
|
||
{"text": "第二段测试口播内容继续讲下去", "image": 2},
|
||
{"text": "第三段测试口播内容收尾总结一下", "image": 3},
|
||
]
|
||
}, ensure_ascii=False)
|
||
# 封面文案请求(prompt 带固定标记「生成短视频封面文案」),渲染由本地 canvas 完成
|
||
COVER_COPY_JSON = json.dumps({
|
||
"main": "三步搞定封面",
|
||
"sub": "AI 文案加大字发光描边",
|
||
"tag": "实测有效",
|
||
}, ensure_ascii=False)
|
||
|
||
|
||
def install_mock(page, cover_b64):
|
||
"""对话/生图网关全部本地 mock:按请求内容区分文案请求与封面提示词请求"""
|
||
def handler(route):
|
||
req = route.request
|
||
url = req.url
|
||
if req.method == "OPTIONS":
|
||
return route.fulfill(status=204, headers={
|
||
"access-control-allow-origin": "*",
|
||
"access-control-allow-methods": "POST, OPTIONS",
|
||
"access-control-allow-headers": "content-type, authorization",
|
||
})
|
||
body = req.post_data or ""
|
||
headers = {"access-control-allow-origin": "*", "content-type": "application/json"}
|
||
if "chat/completions" in url:
|
||
# 封面文案请求带固定标记「生成短视频封面文案」,其余是口播文案请求
|
||
if "生成短视频封面文案" in body:
|
||
content = COVER_COPY_JSON
|
||
else:
|
||
content = COPY_JSON
|
||
payload = json.dumps({"choices": [{"message": {"content": content}}]})
|
||
return route.fulfill(status=200, headers=headers, body=payload)
|
||
if "images/generations" in url:
|
||
payload = json.dumps({"data": [{"b64_json": cover_b64}]})
|
||
return route.fulfill(status=200, headers=headers, body=payload)
|
||
return route.fulfill(status=404, headers=headers, body="{}")
|
||
|
||
page.route("**/v1/chat/completions", handler)
|
||
page.route("**/v1/images/generations", handler)
|
||
|
||
|
||
def js_click(page, expr, desc):
|
||
ok = page.evaluate("() => { const e = %s; if (e) { e.click(); return true; } return false; }" % expr)
|
||
log("点 %s: %s" % (desc, ok))
|
||
return ok
|
||
|
||
|
||
def wait_text(page, text, seconds=120, label=""):
|
||
for _ in range(seconds):
|
||
page.wait_for_timeout(1000)
|
||
if page.evaluate("() => document.body.innerText.includes(%s)" % json.dumps(text)):
|
||
log("到位[%s]: %s" % (label, text))
|
||
return True
|
||
log("等待超时[%s]: %s" % (label, text))
|
||
return False
|
||
|
||
|
||
def make_images():
|
||
from PIL import Image, ImageDraw
|
||
out = []
|
||
for i, color in enumerate([(200, 60, 60), (60, 140, 200), (80, 170, 90)], 1):
|
||
p = "/tmp/qvc_img%d.png" % i
|
||
im = Image.new("RGB", (960, 720), color)
|
||
d = ImageDraw.Draw(im)
|
||
d.text((40, 40), "COVER TEST %d" % i, fill=(255, 255, 255))
|
||
im.save(p)
|
||
out.append(p)
|
||
return out
|
||
|
||
|
||
def main():
|
||
imgs = make_images()
|
||
cover_b64 = make_cover_png_b64()
|
||
log("测试图片: %s" % imgs)
|
||
with sync_playwright() as p:
|
||
browser = p.chromium.launch(
|
||
executable_path="/usr/bin/chromium",
|
||
headless=True,
|
||
args=["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage", "--mute-audio",
|
||
"--autoplay-policy=no-user-gesture-required"],
|
||
)
|
||
ctx = browser.new_context(ignore_https_errors=True)
|
||
page = ctx.new_page()
|
||
errors = []
|
||
page.on("pageerror", lambda e: errors.append(str(e)))
|
||
page.on("console", lambda m: errors.append("console:" + m.text[:200]) if m.type == "error" else None)
|
||
page.on("console", lambda m: log("页面日志 %s" % m.text[:220]) if "[qv]" in m.text else None)
|
||
|
||
# 预置 AI 配置(Key 是假的,所有请求都被 route mock 掉)
|
||
page.add_init_script("""
|
||
localStorage.setItem('micut.ai.config.v1', JSON.stringify({
|
||
baseUrl: 'https://ai.88531.cn/v1',
|
||
apiKey: 'e2e-mock-key',
|
||
model: 'mock-model',
|
||
}));
|
||
""")
|
||
install_mock(page, cover_b64)
|
||
|
||
log("打开工程 %s" % APPID)
|
||
page.goto("%s/editor/%s" % (BASE, APPID), wait_until="domcontentloaded", timeout=120000)
|
||
page.wait_for_timeout(4000)
|
||
log("守卫: %s" % page.evaluate(GUARD))
|
||
page.reload(wait_until="domcontentloaded")
|
||
page.wait_for_selector("#h5dsCanvas", timeout=90000)
|
||
try:
|
||
page.wait_for_function("() => document.body.innerText.includes('AI创作')", timeout=90000)
|
||
log("侧栏已就绪")
|
||
except Exception as e:
|
||
log("侧栏等待超时: %s" % e)
|
||
page.wait_for_timeout(3000)
|
||
|
||
js_click(page, """(() => {
|
||
const w = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
|
||
let n;
|
||
while (n = w.nextNode()) {
|
||
const t = (n.textContent || '').trim();
|
||
if (t === 'AI创作' && n.parentElement) return n.parentElement;
|
||
}
|
||
return null;
|
||
})()""", "AI创作")
|
||
page.wait_for_timeout(2500)
|
||
js_click(page, "[...document.querySelectorAll('button')].find(x => (x.innerText||'').includes('一键出片'))",
|
||
"开始一键出片")
|
||
page.wait_for_timeout(3000)
|
||
|
||
# 步骤1:3 张图 + 标题
|
||
fi = page.query_selector("div.semi-modal input[type=file]")
|
||
if not fi:
|
||
fi = page.query_selector("input[type=file][accept='image/*']")
|
||
if not fi:
|
||
log("ERROR: 找不到向导的图片输入框")
|
||
print(json.dumps({"ok": False, "why": "no-file-input"}, ensure_ascii=False))
|
||
return 1
|
||
fi.set_input_files(imgs)
|
||
page.wait_for_timeout(2500)
|
||
page.fill("input[placeholder*='三步搞定']", "封面功能端到端验证")
|
||
page.wait_for_timeout(500)
|
||
|
||
js_click(page, "[...document.querySelectorAll('button')].find(x=>(x.innerText||'').includes('① 识别图片'))", "生成文案")
|
||
got = wait_text(page, "下一步:配置声音", 180, "第2步")
|
||
log("文案生成完成: %s" % got)
|
||
if not got:
|
||
print(json.dumps({"ok": False, "why": "copy-timeout", "errors": errors[-6:]}, ensure_ascii=False))
|
||
return 1
|
||
|
||
js_click(page, "[...document.querySelectorAll('button')].find(x=>(x.innerText||'').includes('下一步:配置声音'))", "下一步")
|
||
page.wait_for_timeout(2000)
|
||
|
||
# 步骤3:合成全部配音(真实 edge-tts)
|
||
js_click(page, "[...document.querySelectorAll('button')].find(x=>(x.innerText||'').includes('合成全部配音'))", "合成全部配音")
|
||
done = wait_text(page, "图片转场", 300, "第4步效果面板")
|
||
log("配音合成完成: %s" % done)
|
||
if not done:
|
||
print(json.dumps({"ok": False, "why": "synth-timeout", "errors": errors[-6:]}, ensure_ascii=False))
|
||
return 1
|
||
page.wait_for_timeout(1500)
|
||
|
||
# 断言 1:确认页封面预览行
|
||
body = page.inner_text(".semi-modal-body")
|
||
has_cover_row = ("视频封面" in body) and ("AI 封面文案" in body) and ("大字发光" in body)
|
||
has_cover_thumb = page.evaluate("""() => {
|
||
const img = [...document.querySelectorAll('.semi-modal img')].find(i => (i.alt||'') === '视频封面');
|
||
return !!(img && img.src && img.src.startsWith('data:image/png'));
|
||
}""")
|
||
has_total = "预计成片" in body
|
||
log("确认页封面行: %s, 缩略图 dataURL: %s" % (has_cover_row, has_cover_thumb))
|
||
page.screenshot(path="/tmp/qvc_step4.png", full_page=False)
|
||
|
||
# 生成
|
||
js_click(page, "[...document.querySelectorAll('button')].find(x=>(x.innerText||'').includes('③ 确认生成视频'))", "确认生成视频")
|
||
page.wait_for_timeout(12000)
|
||
|
||
# 断言 2/3:时间轴数据
|
||
res = page.evaluate("""() => {
|
||
const ed = window.__editor && window.__editor.movie;
|
||
if (!ed) return {err:'no-editor'};
|
||
const d = ed.data || {};
|
||
const els = (d.elements || []).slice().sort((a,b) => (a.startTime||0) - (b.startTime||0));
|
||
const imgs = els.filter(e => e.type === 'image');
|
||
const caps = d.captions || [];
|
||
const f = els[0] || null;
|
||
// 元素本身不存 url,资源经 resourceManage.getResouceById(resourceId) 取
|
||
let resUrl = '';
|
||
const rid = f && f.resourceId;
|
||
if (rid) {
|
||
const mgr = window.__editor.movie.resourceManage;
|
||
try {
|
||
const r = mgr && mgr.getResouceById && mgr.getResouceById(rid);
|
||
if (r) resUrl = r.url || (r.urls && r.urls.url) || '';
|
||
} catch (e) { resUrl = 'ERR:' + e; }
|
||
}
|
||
return {
|
||
title: d.title,
|
||
first: f,
|
||
coverResourceUrl: resUrl.slice(0, 40),
|
||
imgCount: imgs.length,
|
||
elements: els.map(e => ({ name: e.name, type: e.type, t: e.startTime, d: e.duration })),
|
||
captionCount: caps.length,
|
||
caption0: caps[0] ? JSON.parse(JSON.stringify(caps[0])) : null,
|
||
canvas: { w: d.width, h: d.height },
|
||
};
|
||
}""")
|
||
log("工程数据: " + json.dumps(res, ensure_ascii=False)[:2000])
|
||
|
||
f = res.get("first") or {}
|
||
cap0 = res.get("caption0") or {}
|
||
# 字幕在元素数据里以秒存 startTime(addCaptions 的 BeginTime 输入被 SDK 归一化)
|
||
cap_begin = cap0.get("startTime", cap0.get("BeginTime", cap0.get("beginTime")))
|
||
url = res.get("coverResourceUrl") or ((f.get("urls") or {}).get("url") or f.get("url") or "")
|
||
checks = {
|
||
"cover_row_on_step4": has_cover_row,
|
||
"cover_thumb_dataurl": has_cover_thumb,
|
||
"first_is_cover": f.get("name") == "视频封面" and f.get("type") == "image",
|
||
"cover_at_head": (f.get("startTime") is not None) and abs(float(f.get("startTime"))) < 0.01,
|
||
"cover_dur_3": abs(float(f.get("duration") or 0) - 3) < 0.01,
|
||
"cover_has_url": bool(url),
|
||
"cover_url_is_dataurl": str(url).startswith("data:image"),
|
||
# 字幕从封面结束(第 3 秒)起接续
|
||
"caption_after_cover": (cap_begin or 0) >= 3,
|
||
"caption_count_3": res.get("captionCount") == 3,
|
||
}
|
||
log("原始首元素: " + json.dumps({k: v for k, v in f.items() if k in ('name','type','startTime','duration','urls','url','trackIndex')}, ensure_ascii=False)[:800])
|
||
log("原始字幕0: " + json.dumps(cap0, ensure_ascii=False)[:400])
|
||
log("断言: " + json.dumps(checks, ensure_ascii=False))
|
||
log("JS 错误: %s" % (errors[-5:] if errors else "无"))
|
||
|
||
# 播一下确认渲染
|
||
page.evaluate("""() => {
|
||
const m = window.__editor && window.__editor.movie;
|
||
if (m && m.pause) m.pause();
|
||
const st = window.__editor;
|
||
if (st && st.updateMovie) st.updateMovie(1.0);
|
||
}""")
|
||
page.wait_for_timeout(2500)
|
||
page.screenshot(path="/tmp/qvc_playing.png", full_page=False)
|
||
log("截图: /tmp/qvc_step4.png /tmp/qvc_playing.png")
|
||
|
||
# 刷新确认封面持久化
|
||
saved = page.evaluate("""() => {
|
||
const s = window.__editor;
|
||
if (typeof s.saveApp === 'function') { s.saveApp(() => {}); return 'saveApp 已调用'; }
|
||
return '没有 saveApp';
|
||
}""")
|
||
log("手动保存: %s" % saved)
|
||
page.wait_for_timeout(8000)
|
||
page.reload(wait_until="domcontentloaded")
|
||
page.wait_for_selector("#h5dsCanvas", timeout=90000)
|
||
page.wait_for_timeout(12000)
|
||
after = page.evaluate("""() => {
|
||
const d = (window.__editor && window.__editor.movie && window.__editor.movie.data) || {};
|
||
const els = (d.elements || []).slice().sort((a,b) => (a.startTime||0) - (b.startTime||0));
|
||
const f = els[0] || {};
|
||
return { title: d.title, firstName: f.name, firstT: f.startTime, firstD: f.duration,
|
||
els: els.length, caps: (d.captions||[]).length };
|
||
}""")
|
||
log("刷新后: " + json.dumps(after, ensure_ascii=False))
|
||
checks["cover_persist_after_reload"] = (
|
||
after.get("firstName") == "视频封面"
|
||
and after.get("firstT") is not None and abs(float(after.get("firstT"))) < 0.01
|
||
)
|
||
|
||
# 断言:用户输入的标题 → 项目文件名(顶栏 title + 刷新后持久化)
|
||
res_title = res.get("title")
|
||
log("构建后 title: %s" % res_title)
|
||
# AI 出片项目文件名须带【AI】前缀
|
||
checks["title_applied_after_build"] = res_title == "【AI】封面功能端到端验证"
|
||
checks["title_persist_after_reload"] = after.get("title") == "【AI】封面功能端到端验证"
|
||
# 项目列表卡片名(meta.name)
|
||
meta_name = page.evaluate("""async () => {
|
||
try {
|
||
const rq = indexedDB.open('H5VideoLocalDB');
|
||
const db = await new Promise((res, rej) => { rq.onsuccess = () => res(rq.result); rq.onerror = () => rej(rq.error); });
|
||
const id = window.__editor && window.__editor.appid;
|
||
const tx = db.transaction('projects', 'readonly');
|
||
const g = tx.objectStore('projects').get(id);
|
||
const rec = await new Promise((res, rej) => { g.onsuccess = () => res(g.result); g.onerror = () => rej(g.error); });
|
||
return (rec && rec.name) || '';
|
||
} catch (e) { return 'ERR:' + e; }
|
||
}""")
|
||
log("项目卡片名: %s" % meta_name)
|
||
checks["project_card_name"] = meta_name == "【AI】封面功能端到端验证"
|
||
|
||
ok = all(checks.values())
|
||
print(json.dumps({"ok": ok, "checks": checks, "errors": errors[-5:]}, ensure_ascii=False))
|
||
browser.close()
|
||
return 0 if ok else 2
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|