Spaces:
Paused
Paused
Surf commited on
Commit ·
44d2dac
1
Parent(s): d565337
暮归江南 · v3 步骤2:黑屏修复——WebGL 上下文丢失恢复、逐帧校验与重渲染、最低可用 H.264 Level、后台节流规避、缩短片头黑场;附验证脚本
Browse files- README.md +7 -0
- src/jiangnan/JiangnanApp.tsx +44 -9
- src/jiangnan/engine/Film.ts +115 -9
- src/jiangnan/engine/post.ts +35 -10
- src/jiangnan/engine/timeline.ts +2 -1
- src/jiangnan/export/covers.ts +12 -11
- src/jiangnan/export/frame.ts +169 -0
- src/jiangnan/export/livegen.ts +7 -11
- src/jiangnan/export/mp4.ts +78 -32
- tools/jiangnan/lose_context.py +39 -0
- tools/jiangnan/scan_black.py +35 -0
- tools/jiangnan/shots.py +25 -0
README.md
CHANGED
|
@@ -208,6 +208,13 @@ tools/jiangnan/ # 旁白生成、字体子集化、实况图
|
|
| 208 |
### H.6 进度日志(v3)
|
| 209 |
|
| 210 |
- **步骤 1** ✅ 写好 v3 优化计划(本节),推送到 Space。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
---
|
| 213 |
|
|
|
|
| 208 |
### H.6 进度日志(v3)
|
| 209 |
|
| 210 |
- **步骤 1** ✅ 写好 v3 优化计划(本节),推送到 Space。
|
| 211 |
+
- **步骤 2(黑屏修复)** ✅
|
| 212 |
+
- 先用无头 Chromium 逐秒扫描原版全片(`tools/jiangnan/scan_black.py`):除 t=0 与 t=86 的设计黑场外没有黑帧,证明黑屏不是场景本身的问题,而是导出链路在真实显卡上遇到的问题(上下文丢失 / 编码 Level),与 H.2 的分析一致。
|
| 213 |
+
- `engine/Film.ts`:监听 `webglcontextlost / webglcontextrestored`;恢复后作废并重新烘焙全部 173 张 PMREM 天光、重建后期链路、重置阴影取景与阴影贴图,不再删除属于旧上下文的 GPU 对象;新增 `isContextLost()`、`waitForContext()`、`rebuildPost()`、`setShadowRes()`;播放循环在上下文丢失期间暂停,恢复后提示并重绘。
|
| 214 |
+
- 新增 `export/frame.ts`(MP4 / 封面 / 实况图共用的「校验式抓帧」):每帧渲染后缩成 32×18 读回测最大亮度;在不该黑的时刻(`fadeBlack < 0.97`)若为全黑 → 等待上下文恢复(最多 15 s)→ 自动降低超采样倍率 → 重新渲染同一帧,最多 4 次;仍失败则中止并给出明确错误,而不是输出黑视频。抓帧前先把合成画布填黑,避免上下文丢失时 `drawImage` 画不出东西而让上一帧「冒充」本帧。用 `MessageChannel` 让出事件循环,避开后台标签页对 `setTimeout` 的 1 s 节流。
|
| 215 |
+
- `export/mp4.ts`:H.264 Level 按分辨率 × 帧率 × 码率取**最低够用值**(720p30→3.1、1080p30→4.0、1080p60→4.2、2K30→5.0),High→Main→Baseline 依次探测,VBR;导出前先跑一帧探针确认渲染链路;结果返回编码器 / 码率 / 逐帧校验统计,页面显示「N 帧全部通过,无黑帧」。
|
| 216 |
+
- `engine/timeline.ts`:开头黑场 1.6 s → 0.9 s,结尾 1.4 s → 1.2 s(淡入淡出保留)。
|
| 217 |
+
- 验证(`tools/jiangnan/lose_context.py`,无头 Chromium + SwiftShader):① 播放器:渲染亮度 73 → 强制 `loseContext()` 后亮度 0、`isContextLost()=true` → `restoreContext()` 后 `waitForContext()` 返回 true,重新渲染第 1 帧和第 2 帧亮度均为 73(与丢失前一致);② 导出抓帧:连续抓 6 帧并在第 3 帧前注入上下文丢失,6 帧亮度 119/123/130/128/123/73,`contextLosses=1`、自动降级到 1.5× 超采样、**0 黑帧进入输出**(PASS)。改动后全片逐秒扫描:除 t=0 外无黑帧。
|
| 218 |
|
| 219 |
---
|
| 220 |
|
src/jiangnan/JiangnanApp.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import { Film, Quality, renderIds } from './engine/Film'
|
|
| 7 |
import { DURATION, SHOTS, shotAt } from './engine/timeline'
|
| 8 |
import { canvasToBlob, COVER_DESIGNS, Ratio, RATIOS, renderCover } from './export/covers'
|
| 9 |
import { generateLivePhotos, LIVE_CLIPS, LiveResult } from './export/livegen'
|
|
|
|
| 10 |
import { exportMp4, exportSupport } from './export/mp4'
|
| 11 |
import { drawOverlay, loadFonts } from './player/overlay'
|
| 12 |
import { strToU8, zipSync, Zippable } from 'fflate'
|
|
@@ -89,6 +90,10 @@ export default function JiangnanApp() {
|
|
| 89 |
const quality: Quality = (q.get('q') as Quality) || (isMobile() ? 'medium' : 'high')
|
| 90 |
const film = new Film(canvas, quality)
|
| 91 |
filmRef.current = film
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
const w = Number(q.get('w') || 0), h = Number(q.get('h') || 0)
|
| 93 |
// automation hooks (used by the QA scripts)
|
| 94 |
const win = window as unknown as Record<string, unknown>
|
|
@@ -101,9 +106,33 @@ export default function JiangnanApp() {
|
|
| 101 |
},
|
| 102 |
cover: async (key: string, ratio: Ratio) => {
|
| 103 |
const d = COVER_DESIGNS.find((x) => x.key === key)!
|
| 104 |
-
const c = renderCover(film, d, ratio)
|
| 105 |
return blobB64(await canvasToBlob(c))
|
| 106 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
live: async (w: number, h: number, frames: number) => {
|
| 108 |
const r = await generateLivePhotos(film, () => {}, { cancelled: false }, { w, h, frames, clips: 1 })
|
| 109 |
return blobB64(r.zip)
|
|
@@ -241,7 +270,7 @@ export default function JiangnanApp() {
|
|
| 241 |
const loop = () => {
|
| 242 |
raf = requestAnimationFrame(loop)
|
| 243 |
const film = filmRef.current
|
| 244 |
-
if (!film || !film.ready || busyRef.current) return
|
| 245 |
const c = clockRef.current
|
| 246 |
let t = now()
|
| 247 |
if (c.playing && t >= DURATION) {
|
|
@@ -433,15 +462,17 @@ type Busy = <T>(fn: () => Promise<T>) => Promise<T>
|
|
| 433 |
|
| 434 |
const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy }: { film: FilmRef; audio: React.MutableRefObject<{ buf: AudioBuffer | null }>; ready: boolean; withBusy: Busy }) {
|
| 435 |
const [orient, setOrient] = useState<Aspect>('landscape')
|
| 436 |
-
const [res, setRes] = useState<720 | 1080>(1080)
|
| 437 |
const [fps, setFps] = useState<24 | 30 | 60>(30)
|
| 438 |
const [subs, setSubs] = useState(true)
|
| 439 |
const [prog, setProg] = useState<{ p: number; label: string } | null>(null)
|
| 440 |
-
const [out, setOut] = useState<{ url: string; name: string; size: number; note: string } | null>(null)
|
| 441 |
const [err, setErr] = useState('')
|
| 442 |
const sig = useRef({ cancelled: false })
|
| 443 |
const sup = useMemo(() => exportSupport(), [])
|
| 444 |
-
const
|
|
|
|
|
|
|
| 445 |
const start = async () => {
|
| 446 |
if (!film.current) return
|
| 447 |
setErr('')
|
|
@@ -454,7 +485,9 @@ const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy
|
|
| 454 |
onProgress: (p, info) => setProg({ p, label: `${info.stage} ${info.frame}/${info.total} 帧${info.eta > 1 ? ` · 预计还需 ${Math.ceil(info.eta)} 秒` : ''}` }),
|
| 455 |
})
|
| 456 |
const name = `暮归江南_${orient === 'landscape' ? '横屏' : '竖屏'}_${dims.w}x${dims.h}_${fps}fps.mp4`
|
| 457 |
-
|
|
|
|
|
|
|
| 458 |
})
|
| 459 |
} catch (e) {
|
| 460 |
setErr((e as Error).message || String(e))
|
|
@@ -475,6 +508,7 @@ const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy
|
|
| 475 |
</div>
|
| 476 |
<div className="jn-label">清晰度</div>
|
| 477 |
<div className="jn-opt">
|
|
|
|
| 478 |
<button className={`jn-btn ${res === 1080 ? 'on' : ''}`} onClick={() => setRes(1080)}>1080p</button>
|
| 479 |
<button className={`jn-btn ${res === 720 ? 'on' : ''}`} onClick={() => setRes(720)}>720p(更快)</button>
|
| 480 |
</div>
|
|
@@ -509,7 +543,7 @@ const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy
|
|
| 509 |
{prog && (
|
| 510 |
<div className="jn-progress">
|
| 511 |
<div className="jn-bar"><i style={{ width: `${(prog.p * 100).toFixed(1)}%` }} /></div>
|
| 512 |
-
<small>{prog.label}(导出期间请
|
| 513 |
</div>
|
| 514 |
)}
|
| 515 |
{err && <div className="jn-err">导出失败:{err}</div>}
|
|
@@ -520,9 +554,10 @@ const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy
|
|
| 520 |
<a className="jn-btn primary" href={out.url} download={out.name}>保存 {out.name}({(out.size / 1048576).toFixed(1)} MB)</a>
|
| 521 |
<span className="jn-note" style={{ margin: 0 }}>{out.note}</span>
|
| 522 |
</div>
|
|
|
|
| 523 |
</div>
|
| 524 |
)}
|
| 525 |
-
<p className="jn-note">提示:1080p 60fps 共 {Math.round(DURATION * 60)} 帧,独立显卡电脑约 2–4 分钟;手机建议导出 720p。iPhone 上导出后点「保存」→「存储视频」。</p>
|
| 526 |
</section>
|
| 527 |
)
|
| 528 |
})
|
|
@@ -546,7 +581,7 @@ const CoverSection = memo(function CoverSection({ film, ready, withBusy }: { fil
|
|
| 546 |
setProg(i / jobs.length)
|
| 547 |
await new Promise((r) => setTimeout(r, 0))
|
| 548 |
const { r, d } = jobs[i]
|
| 549 |
-
const c = renderCover(film.current!, d, r)
|
| 550 |
const blob = await canvasToBlob(c)
|
| 551 |
out.push({ key: `${r}-${d.key}`, ratio: r, name: `暮归江南_封面_${r.replace(':', 'x')}_${d.name}.jpg`, url: URL.createObjectURL(blob), blob })
|
| 552 |
}
|
|
|
|
| 7 |
import { DURATION, SHOTS, shotAt } from './engine/timeline'
|
| 8 |
import { canvasToBlob, COVER_DESIGNS, Ratio, RATIOS, renderCover } from './export/covers'
|
| 9 |
import { generateLivePhotos, LIVE_CLIPS, LiveResult } from './export/livegen'
|
| 10 |
+
import { createGrabber } from './export/frame'
|
| 11 |
import { exportMp4, exportSupport } from './export/mp4'
|
| 12 |
import { drawOverlay, loadFonts } from './player/overlay'
|
| 13 |
import { strToU8, zipSync, Zippable } from 'fflate'
|
|
|
|
| 90 |
const quality: Quality = (q.get('q') as Quality) || (isMobile() ? 'medium' : 'high')
|
| 91 |
const film = new Film(canvas, quality)
|
| 92 |
filmRef.current = film
|
| 93 |
+
film.onContextEvent = (state) => {
|
| 94 |
+
if (state === 'lost') say('显卡资源被系统回收,正在恢复画面…')
|
| 95 |
+
else { say('画面已恢复'); dirtyRef.current = true }
|
| 96 |
+
}
|
| 97 |
const w = Number(q.get('w') || 0), h = Number(q.get('h') || 0)
|
| 98 |
// automation hooks (used by the QA scripts)
|
| 99 |
const win = window as unknown as Record<string, unknown>
|
|
|
|
| 106 |
},
|
| 107 |
cover: async (key: string, ratio: Ratio) => {
|
| 108 |
const d = COVER_DESIGNS.find((x) => x.key === key)!
|
| 109 |
+
const c = await renderCover(film, d, ratio)
|
| 110 |
return blobB64(await canvasToBlob(c))
|
| 111 |
},
|
| 112 |
+
// verified frame grabbing (the export path minus the encoder); optionally lose the GL
|
| 113 |
+
// context after `loseAfter` frames to exercise the recovery logic
|
| 114 |
+
grab: async (w: number, h: number, times: number[], loseAfter = -1) => {
|
| 115 |
+
const gb = createGrabber(film, w, h, {})
|
| 116 |
+
const lum: number[] = []
|
| 117 |
+
try {
|
| 118 |
+
for (let i = 0; i < times.length; i++) {
|
| 119 |
+
if (i === loseAfter) {
|
| 120 |
+
const ext = film.renderer.getContext().getExtension('WEBGL_lose_context')
|
| 121 |
+
ext?.loseContext()
|
| 122 |
+
setTimeout(() => ext?.restoreContext(), 1500)
|
| 123 |
+
}
|
| 124 |
+
await gb.grab(times[i])
|
| 125 |
+
const g = document.createElement('canvas').getContext('2d', { willReadFrequently: true })!
|
| 126 |
+
g.canvas.width = 32; g.canvas.height = 18
|
| 127 |
+
g.drawImage(gb.comp, 0, 0, 32, 18)
|
| 128 |
+
const d = g.getImageData(0, 0, 32, 18).data
|
| 129 |
+
let sum = 0
|
| 130 |
+
for (let k = 0; k < d.length; k += 4) sum += 0.2126 * d[k] + 0.7152 * d[k + 1] + 0.0722 * d[k + 2]
|
| 131 |
+
lum.push(Math.round(sum / (d.length / 4)))
|
| 132 |
+
}
|
| 133 |
+
} finally { gb.release() }
|
| 134 |
+
return { lum, stats: gb.stats, lostCount: film.lostCount }
|
| 135 |
+
},
|
| 136 |
live: async (w: number, h: number, frames: number) => {
|
| 137 |
const r = await generateLivePhotos(film, () => {}, { cancelled: false }, { w, h, frames, clips: 1 })
|
| 138 |
return blobB64(r.zip)
|
|
|
|
| 270 |
const loop = () => {
|
| 271 |
raf = requestAnimationFrame(loop)
|
| 272 |
const film = filmRef.current
|
| 273 |
+
if (!film || !film.ready || busyRef.current || film.isContextLost()) return
|
| 274 |
const c = clockRef.current
|
| 275 |
let t = now()
|
| 276 |
if (c.playing && t >= DURATION) {
|
|
|
|
| 462 |
|
| 463 |
const ExportSection = memo(function ExportSection({ film, audio, ready, withBusy }: { film: FilmRef; audio: React.MutableRefObject<{ buf: AudioBuffer | null }>; ready: boolean; withBusy: Busy }) {
|
| 464 |
const [orient, setOrient] = useState<Aspect>('landscape')
|
| 465 |
+
const [res, setRes] = useState<720 | 1080 | 1440>(1080)
|
| 466 |
const [fps, setFps] = useState<24 | 30 | 60>(30)
|
| 467 |
const [subs, setSubs] = useState(true)
|
| 468 |
const [prog, setProg] = useState<{ p: number; label: string } | null>(null)
|
| 469 |
+
const [out, setOut] = useState<{ url: string; name: string; size: number; note: string; check: string } | null>(null)
|
| 470 |
const [err, setErr] = useState('')
|
| 471 |
const sig = useRef({ cancelled: false })
|
| 472 |
const sup = useMemo(() => exportSupport(), [])
|
| 473 |
+
const long = res === 1440 ? 2560 : res === 1080 ? 1920 : 1280
|
| 474 |
+
const short = res === 1440 ? 1440 : res === 1080 ? 1080 : 720
|
| 475 |
+
const dims = orient === 'landscape' ? { w: long, h: short } : { w: short, h: long }
|
| 476 |
const start = async () => {
|
| 477 |
if (!film.current) return
|
| 478 |
setErr('')
|
|
|
|
| 485 |
onProgress: (p, info) => setProg({ p, label: `${info.stage} ${info.frame}/${info.total} 帧${info.eta > 1 ? ` · 预计还需 ${Math.ceil(info.eta)} 秒` : ''}` }),
|
| 486 |
})
|
| 487 |
const name = `暮归江南_${orient === 'landscape' ? '横屏' : '竖屏'}_${dims.w}x${dims.h}_${fps}fps.mp4`
|
| 488 |
+
const st = r.stats
|
| 489 |
+
const check = `逐帧校验:${st.frames} 帧全部通过,无黑帧` + (st.retries ? `(${st.retries} 帧曾渲染为黑、已自动重渲染)` : '') + (st.contextLosses ? `;显卡上下文丢失 ${st.contextLosses} 次,已恢复` : '') + ` · ${st.ss}× 超采样` + (st.degraded ? '(显存不足,已自动降低超采样)' : '')
|
| 490 |
+
setOut({ url: URL.createObjectURL(r.blob), name, size: r.blob.size, check, note: `${r.videoCodec} · ${(r.bitrate / 1e6).toFixed(0)} Mbps · ` + (r.audioCodec ? `音轨:${r.audioCodec === 'aac' ? 'AAC 256 kbps' : 'Opus'}` : '当前浏览器不支持音频编码,导出的视频没有声音(建议用电脑版 Chrome / Edge)') })
|
| 491 |
})
|
| 492 |
} catch (e) {
|
| 493 |
setErr((e as Error).message || String(e))
|
|
|
|
| 508 |
</div>
|
| 509 |
<div className="jn-label">清晰度</div>
|
| 510 |
<div className="jn-opt">
|
| 511 |
+
<button className={`jn-btn ${res === 1440 ? 'on' : ''}`} onClick={() => setRes(1440)}>2K 1440p(高配电脑)</button>
|
| 512 |
<button className={`jn-btn ${res === 1080 ? 'on' : ''}`} onClick={() => setRes(1080)}>1080p</button>
|
| 513 |
<button className={`jn-btn ${res === 720 ? 'on' : ''}`} onClick={() => setRes(720)}>720p(更快)</button>
|
| 514 |
</div>
|
|
|
|
| 543 |
{prog && (
|
| 544 |
<div className="jn-progress">
|
| 545 |
<div className="jn-bar"><i style={{ width: `${(prog.p * 100).toFixed(1)}%` }} /></div>
|
| 546 |
+
<small>{prog.label}(导出期间请不要切换标签页或最小化窗口;每一帧都会先校验再写入,不会出现黑屏)</small>
|
| 547 |
</div>
|
| 548 |
)}
|
| 549 |
{err && <div className="jn-err">导出失败:{err}</div>}
|
|
|
|
| 554 |
<a className="jn-btn primary" href={out.url} download={out.name}>保存 {out.name}({(out.size / 1048576).toFixed(1)} MB)</a>
|
| 555 |
<span className="jn-note" style={{ margin: 0 }}>{out.note}</span>
|
| 556 |
</div>
|
| 557 |
+
<div className="jn-note" style={{ marginTop: 6 }}>{out.check}</div>
|
| 558 |
</div>
|
| 559 |
)}
|
| 560 |
+
<p className="jn-note">提示:1080p 60fps 共 {Math.round(DURATION * 60)} 帧,独立显卡电脑约 2–4 分钟;手机建议导出 720p。画面以 2 倍超采样渲染后缩小并轻微锐化,H.264 High Profile、1080p 24 Mbps / 2K 40 Mbps,Level 按分辨率取最低可用值(手机 / 微信 / 视频号播放器均可解码)。片头 / 片尾各有不到 1 秒的淡入淡出,属于设计。iPhone 上导出后点「保存」→「存储视频」。</p>
|
| 561 |
</section>
|
| 562 |
)
|
| 563 |
})
|
|
|
|
| 581 |
setProg(i / jobs.length)
|
| 582 |
await new Promise((r) => setTimeout(r, 0))
|
| 583 |
const { r, d } = jobs[i]
|
| 584 |
+
const c = await renderCover(film.current!, d, r)
|
| 585 |
const blob = await canvasToBlob(c)
|
| 586 |
out.push({ key: `${r}-${d.key}`, ratio: r, name: `暮归江南_封面_${r.replace(':', 'x')}_${d.name}.jpg`, url: URL.createObjectURL(blob), blob })
|
| 587 |
}
|
src/jiangnan/engine/Film.ts
CHANGED
|
@@ -47,8 +47,23 @@ export class Film {
|
|
| 47 |
lampLevel = new Map<number, number>()
|
| 48 |
width = 1280
|
| 49 |
height = 720
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
quality: Quality
|
| 51 |
ready = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
/** QA: freeze the camera at this film time (everything else keeps animating) */
|
| 53 |
freezeCam: number | null = null
|
| 54 |
|
|
@@ -62,9 +77,85 @@ export class Film {
|
|
| 62 |
this.renderer.toneMappingExposure = 1
|
| 63 |
this.renderer.outputColorSpace = THREE.SRGBColorSpace
|
| 64 |
this.renderer.shadowMap.enabled = true
|
| 65 |
-
this.renderer.shadowMap.type = THREE.
|
| 66 |
this.renderer.shadowMap.autoUpdate = false
|
| 67 |
this.pmrem = new THREE.PMREMGenerator(this.renderer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
async init(progress: (p: number, label: string) => void) {
|
|
@@ -73,7 +164,13 @@ export class Film {
|
|
| 73 |
s.fog = new THREE.Fog(0x000000, 1, 2)
|
| 74 |
progress(0.04, '绘制材质贴图')
|
| 75 |
await tick()
|
| 76 |
-
getTextures()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
progress(0.12, '生成天空与山谷')
|
| 78 |
await tick()
|
| 79 |
this.sky = createSky(this.renderer.capabilities.reversedDepthBuffer)
|
|
@@ -109,8 +206,7 @@ export class Film {
|
|
| 109 |
|
| 110 |
// lights
|
| 111 |
this.sun.castShadow = true
|
| 112 |
-
|
| 113 |
-
this.sun.shadow.mapSize.set(sm, sm)
|
| 114 |
this.sun.shadow.bias = -0.0004
|
| 115 |
this.sun.shadow.normalBias = 0.04
|
| 116 |
s.add(this.sun, this.sun.target, this.moon, this.moon.target, this.hemi)
|
|
@@ -128,7 +224,7 @@ export class Film {
|
|
| 128 |
ground.rotation.x = -Math.PI / 2
|
| 129 |
ground.position.y = -6
|
| 130 |
this.envScene.add(ground)
|
| 131 |
-
this.post = createPost(this.renderer, this.scene, this.camera, this.width, this.height, this.
|
| 132 |
progress(0.9, '烘焙天光')
|
| 133 |
await tick()
|
| 134 |
await this.bakeEnvironment((p) => progress(0.9 + p * 0.05, '烘焙天光'))
|
|
@@ -154,15 +250,25 @@ export class Film {
|
|
| 154 |
this.ready = true
|
| 155 |
}
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
this.width = w
|
| 159 |
this.height = h
|
|
|
|
| 160 |
this.renderer.setSize(w, h, false)
|
| 161 |
this.camera.aspect = w / h
|
| 162 |
this.camera.updateProjectionMatrix()
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
|
| 168 |
/** Environment light from the sky. Pre-baked every 0.5 s of film time (no runtime hitches). */
|
|
|
|
| 47 |
lampLevel = new Map<number, number>()
|
| 48 |
width = 1280
|
| 49 |
height = 720
|
| 50 |
+
/** internal supersampling factor: the post chain renders at width*ss × height*ss, the final pass filters it down to the canvas */
|
| 51 |
+
ss = 1
|
| 52 |
+
postSamples = 4
|
| 53 |
+
shadowRes = 2048
|
| 54 |
+
/** reflection map resolution relative to the (supersampled) frame */
|
| 55 |
+
reflScale = 0.5
|
| 56 |
quality: Quality
|
| 57 |
ready = false
|
| 58 |
+
// ---- WebGL context loss bookkeeping (the usual cause of "the exported video is black")
|
| 59 |
+
contextLost = false
|
| 60 |
+
lostCount = 0
|
| 61 |
+
/** bumped on every context restore; GPU objects created under an older generation must not be deleted */
|
| 62 |
+
private glGen = 0
|
| 63 |
+
private postGen = 0
|
| 64 |
+
/** resolves once the context is back and the environment maps have been re-baked */
|
| 65 |
+
private restorePromise: Promise<void> | null = null
|
| 66 |
+
onContextEvent: ((state: 'lost' | 'restored') => void) | null = null
|
| 67 |
/** QA: freeze the camera at this film time (everything else keeps animating) */
|
| 68 |
freezeCam: number | null = null
|
| 69 |
|
|
|
|
| 77 |
this.renderer.toneMappingExposure = 1
|
| 78 |
this.renderer.outputColorSpace = THREE.SRGBColorSpace
|
| 79 |
this.renderer.shadowMap.enabled = true
|
| 80 |
+
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
|
| 81 |
this.renderer.shadowMap.autoUpdate = false
|
| 82 |
this.pmrem = new THREE.PMREMGenerator(this.renderer)
|
| 83 |
+
this.shadowRes = quality === 'low' ? 1024 : quality === 'medium' ? 2048 : 3072
|
| 84 |
+
this.postSamples = quality === 'low' ? 0 : 4
|
| 85 |
+
this.reflScale = quality === 'low' ? 0.35 : quality === 'medium' ? 0.5 : 0.75
|
| 86 |
+
// Context loss: three.js already calls preventDefault() (so the browser will try to restore).
|
| 87 |
+
// We additionally track the state so exports can wait, retry, and never encode black frames.
|
| 88 |
+
canvas.addEventListener('webglcontextlost', () => {
|
| 89 |
+
this.contextLost = true
|
| 90 |
+
this.lostCount++
|
| 91 |
+
console.warn('[暮归江南] WebGL context lost')
|
| 92 |
+
this.onContextEvent?.('lost')
|
| 93 |
+
})
|
| 94 |
+
canvas.addEventListener('webglcontextrestored', () => {
|
| 95 |
+
console.warn('[暮归江南] WebGL context restored → re-baking')
|
| 96 |
+
this.glGen++
|
| 97 |
+
this.restorePromise = this.recoverFromContextLoss().finally(() => { this.restorePromise = null })
|
| 98 |
+
})
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/** True when the GL context is currently lost. */
|
| 102 |
+
isContextLost() {
|
| 103 |
+
try { return this.contextLost || this.renderer.getContext().isContextLost() } catch { return true }
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/** Wait until the context is usable again (restored + environment re-baked). Resolves false on timeout. */
|
| 107 |
+
async waitForContext(timeoutMs = 15000) {
|
| 108 |
+
const t0 = performance.now()
|
| 109 |
+
while (performance.now() - t0 < timeoutMs) {
|
| 110 |
+
if (!this.isContextLost() && !this.restorePromise) return true
|
| 111 |
+
if (this.restorePromise) { await this.restorePromise; continue }
|
| 112 |
+
await new Promise((r) => setTimeout(r, 100))
|
| 113 |
+
}
|
| 114 |
+
return !this.isContextLost() && !this.restorePromise
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
/** Everything that lived only on the GPU has to be produced again after a context restore. */
|
| 118 |
+
private async recoverFromContextLoss() {
|
| 119 |
+
if (!this.ready) { this.contextLost = false; return }
|
| 120 |
+
// baked environment maps are gone (they were only ever GPU textures)
|
| 121 |
+
for (const rt of this.envCache.values()) rt.dispose()
|
| 122 |
+
this.envCache.clear()
|
| 123 |
+
this.envKey = -1
|
| 124 |
+
this.envRT = null
|
| 125 |
+
this.pmrem.dispose()
|
| 126 |
+
this.pmrem = new THREE.PMREMGenerator(this.renderer)
|
| 127 |
+
// shadow map, reflection target and post chain are re-created lazily by three.js, but the
|
| 128 |
+
// shadow frustum and the post targets' sizes must be re-applied
|
| 129 |
+
this.shadowShot = -1
|
| 130 |
+
// the old post targets belonged to the dead context: do not try to delete them
|
| 131 |
+
this.rebuildPost(this.postSamples, false)
|
| 132 |
+
this.setSize(this.width, this.height, this.ss)
|
| 133 |
+
if (this.sun.shadow.map) this.sun.shadow.map = null
|
| 134 |
+
this.contextLost = false
|
| 135 |
+
await this.bakeEnvironment()
|
| 136 |
+
this.onContextEvent?.('restored')
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
/** Re-create the post-processing chain (e.g. with a different MSAA sample count). */
|
| 140 |
+
rebuildPost(samples: number, dispose = true) {
|
| 141 |
+
this.postSamples = samples
|
| 142 |
+
if (this.post && dispose && this.postGen === this.glGen) {
|
| 143 |
+
this.post.bloom.dispose()
|
| 144 |
+
this.post.composer.dispose()
|
| 145 |
+
}
|
| 146 |
+
this.postGen = this.glGen
|
| 147 |
+
this.post = createPost(this.renderer, this.scene, this.camera, Math.round(this.width * this.ss), Math.round(this.height * this.ss), samples)
|
| 148 |
+
;(this.post.grade.uniforms.uRes.value as THREE.Vector2).set(this.width, this.height)
|
| 149 |
+
this.post.grade.uniforms.uSS.value = this.ss
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
/** Change the sun shadow map resolution (1024 … 4096). */
|
| 153 |
+
setShadowRes(n: number) {
|
| 154 |
+
if (n === this.shadowRes) return
|
| 155 |
+
this.shadowRes = n
|
| 156 |
+
this.sun.shadow.mapSize.set(n, n)
|
| 157 |
+
if (this.sun.shadow.map) { this.sun.shadow.map.dispose(); this.sun.shadow.map = null }
|
| 158 |
+
this.shadowShot = -1
|
| 159 |
}
|
| 160 |
|
| 161 |
async init(progress: (p: number, label: string) => void) {
|
|
|
|
| 164 |
s.fog = new THREE.Fog(0x000000, 1, 2)
|
| 165 |
progress(0.04, '绘制材质贴图')
|
| 166 |
await tick()
|
| 167 |
+
const T = getTextures() as unknown as Record<string, unknown>
|
| 168 |
+
// anisotropic filtering at the GPU maximum: tiles, flagstones and wood grain stay sharp at grazing angles
|
| 169 |
+
const aniso = Math.min(16, this.renderer.capabilities.getMaxAnisotropy())
|
| 170 |
+
for (const v of Object.values(T)) {
|
| 171 |
+
const list = Array.isArray(v) ? v : [v]
|
| 172 |
+
for (const t of list) if ((t as THREE.Texture)?.isTexture) { (t as THREE.Texture).anisotropy = aniso; (t as THREE.Texture).needsUpdate = true }
|
| 173 |
+
}
|
| 174 |
progress(0.12, '生成天空与山谷')
|
| 175 |
await tick()
|
| 176 |
this.sky = createSky(this.renderer.capabilities.reversedDepthBuffer)
|
|
|
|
| 206 |
|
| 207 |
// lights
|
| 208 |
this.sun.castShadow = true
|
| 209 |
+
this.sun.shadow.mapSize.set(this.shadowRes, this.shadowRes)
|
|
|
|
| 210 |
this.sun.shadow.bias = -0.0004
|
| 211 |
this.sun.shadow.normalBias = 0.04
|
| 212 |
s.add(this.sun, this.sun.target, this.moon, this.moon.target, this.hemi)
|
|
|
|
| 224 |
ground.rotation.x = -Math.PI / 2
|
| 225 |
ground.position.y = -6
|
| 226 |
this.envScene.add(ground)
|
| 227 |
+
this.post = createPost(this.renderer, this.scene, this.camera, this.width, this.height, this.postSamples)
|
| 228 |
progress(0.9, '烘焙天光')
|
| 229 |
await tick()
|
| 230 |
await this.bakeEnvironment((p) => progress(0.9 + p * 0.05, '烘焙天光'))
|
|
|
|
| 250 |
this.ready = true
|
| 251 |
}
|
| 252 |
|
| 253 |
+
/**
|
| 254 |
+
* Output size (canvas) and internal supersampling factor. With ss > 1 the whole scene + post
|
| 255 |
+
* chain renders at w*ss × h*ss and the final grade pass filters it down to the canvas, so
|
| 256 |
+
* the exported frame is anti-aliased without MSAA and without a second CPU-side resample.
|
| 257 |
+
*/
|
| 258 |
+
setSize(w: number, h: number, ss = this.ss) {
|
| 259 |
this.width = w
|
| 260 |
this.height = h
|
| 261 |
+
this.ss = ss
|
| 262 |
this.renderer.setSize(w, h, false)
|
| 263 |
this.camera.aspect = w / h
|
| 264 |
this.camera.updateProjectionMatrix()
|
| 265 |
+
const iw = Math.round(w * ss), ih = Math.round(h * ss)
|
| 266 |
+
this.post?.setSize(iw, ih)
|
| 267 |
+
if (this.post) {
|
| 268 |
+
;(this.post.grade.uniforms.uRes.value as THREE.Vector2).set(w, h)
|
| 269 |
+
this.post.grade.uniforms.uSS.value = ss
|
| 270 |
+
}
|
| 271 |
+
this.water?.setSize(iw * this.reflScale, ih * this.reflScale)
|
| 272 |
}
|
| 273 |
|
| 274 |
/** Environment light from the sky. Pre-baked every 0.5 s of film time (no runtime hitches). */
|
src/jiangnan/engine/post.ts
CHANGED
|
@@ -62,25 +62,50 @@ const GradeShader = {
|
|
| 62 |
tDiffuse: { value: null as THREE.Texture | null },
|
| 63 |
uTime: { value: 0 },
|
| 64 |
uVignette: { value: 0.32 },
|
| 65 |
-
uGrain: { value: 0.
|
| 66 |
uFade: { value: 0 },
|
| 67 |
uNight: { value: 0 },
|
|
|
|
| 68 |
uRes: { value: new THREE.Vector2(1920, 1080) },
|
| 69 |
-
uCA: { value: 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
},
|
| 71 |
vertexShader: /* glsl */ `varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`,
|
| 72 |
fragmentShader: /* glsl */ `
|
| 73 |
-
uniform sampler2D tDiffuse; uniform float uTime; uniform float uVignette; uniform float uGrain; uniform float uFade; uniform float uNight; uniform vec2 uRes; uniform float uCA;
|
| 74 |
varying vec2 vUv;
|
| 75 |
float h(vec2 p) { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
void main() {
|
| 77 |
vec2 c = vUv - 0.5;
|
| 78 |
float r2 = dot(c, c);
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
// split toning: warm highlights, teal-blue shadows (stronger at night)
|
| 85 |
float l = dot(col, vec3(0.2126, 0.7152, 0.0722));
|
| 86 |
vec3 shadowTint = mix(vec3(0.96, 1.0, 1.05), vec3(0.9, 0.98, 1.12), uNight);
|
|
@@ -92,7 +117,7 @@ const GradeShader = {
|
|
| 92 |
float vig = 1.0 - uVignette * smoothstep(0.12, 0.75, r2 * 1.6);
|
| 93 |
col *= vig;
|
| 94 |
// film grain (luma weighted, frame-locked to film time so export is deterministic)
|
| 95 |
-
float g = h(vUv * uRes + fract(uTime * 24.0) * 97.0) - 0.5;
|
| 96 |
col += g * uGrain * (0.6 + 0.4 * (1.0 - l));
|
| 97 |
col *= 1.0 - uFade;
|
| 98 |
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
|
@@ -120,9 +145,9 @@ export function createPost(renderer: THREE.WebGLRenderer, scene: THREE.Scene, ca
|
|
| 120 |
composer.addPass(new OutputPass())
|
| 121 |
const grade = new ShaderPass(GradeShader)
|
| 122 |
composer.addPass(grade)
|
|
|
|
| 123 |
const setSize = (W: number, H: number) => {
|
| 124 |
composer.setSize(W, H)
|
| 125 |
-
;(grade.uniforms.uRes.value as THREE.Vector2).set(W, H)
|
| 126 |
rays.uniforms.uAspect.value = W / H
|
| 127 |
}
|
| 128 |
setSize(w, h)
|
|
|
|
| 62 |
tDiffuse: { value: null as THREE.Texture | null },
|
| 63 |
uTime: { value: 0 },
|
| 64 |
uVignette: { value: 0.32 },
|
| 65 |
+
uGrain: { value: 0.010 },
|
| 66 |
uFade: { value: 0 },
|
| 67 |
uNight: { value: 0 },
|
| 68 |
+
/** output (canvas) resolution — grain and sharpening work in output pixels */
|
| 69 |
uRes: { value: new THREE.Vector2(1920, 1080) },
|
| 70 |
+
uCA: { value: 0.0007 },
|
| 71 |
+
/** internal supersampling factor of tDiffuse relative to uRes (1 = none) */
|
| 72 |
+
uSS: { value: 1 },
|
| 73 |
+
/** unsharp-mask amount applied after downsampling (0 = off; exports use ~0.18) */
|
| 74 |
+
uSharpen: { value: 0 },
|
| 75 |
},
|
| 76 |
vertexShader: /* glsl */ `varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`,
|
| 77 |
fragmentShader: /* glsl */ `
|
| 78 |
+
uniform sampler2D tDiffuse; uniform float uTime; uniform float uVignette; uniform float uGrain; uniform float uFade; uniform float uNight; uniform vec2 uRes; uniform float uCA; uniform float uSS; uniform float uSharpen;
|
| 79 |
varying vec2 vUv;
|
| 80 |
float h(vec2 p) { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
|
| 81 |
+
// Downsampling fetch. tDiffuse is uSS× larger than the output: at exactly 2× one bilinear tap
|
| 82 |
+
// at the output pixel centre is an exact 2×2 box filter; for other factors four quarter-pixel
|
| 83 |
+
// taps make a tent filter so that nothing crawls or aliases.
|
| 84 |
+
vec3 fetch(vec2 uv) {
|
| 85 |
+
if (uSS < 1.01 || abs(uSS - 2.0) < 0.01) return texture2D(tDiffuse, uv).rgb;
|
| 86 |
+
vec2 q = 0.25 / uRes;
|
| 87 |
+
return 0.25 * (texture2D(tDiffuse, uv + vec2(-q.x, -q.y)).rgb + texture2D(tDiffuse, uv + vec2(q.x, -q.y)).rgb
|
| 88 |
+
+ texture2D(tDiffuse, uv + vec2(-q.x, q.y)).rgb + texture2D(tDiffuse, uv + vec2(q.x, q.y)).rgb);
|
| 89 |
+
}
|
| 90 |
+
// gentle lens chromatic aberration toward the corners
|
| 91 |
+
vec3 fetchCA(vec2 uv) {
|
| 92 |
+
vec2 c = uv - 0.5;
|
| 93 |
+
vec2 off = c * dot(c, c) * uCA * 8.0;
|
| 94 |
+
return vec3(fetch(uv + off).r, fetch(uv).g, fetch(uv - off).b);
|
| 95 |
+
}
|
| 96 |
void main() {
|
| 97 |
vec2 c = vUv - 0.5;
|
| 98 |
float r2 = dot(c, c);
|
| 99 |
+
vec3 col = fetchCA(vUv);
|
| 100 |
+
if (uSharpen > 0.001) {
|
| 101 |
+
// luminance-only unsharp mask on the downsampled image (no colour fringing)
|
| 102 |
+
vec2 px = 1.0 / uRes;
|
| 103 |
+
vec3 n = fetchCA(vUv + vec2(0.0, px.y)), s = fetchCA(vUv - vec2(0.0, px.y));
|
| 104 |
+
vec3 e = fetchCA(vUv + vec2(px.x, 0.0)), w = fetchCA(vUv - vec2(px.x, 0.0));
|
| 105 |
+
vec3 lw = vec3(0.2126, 0.7152, 0.0722);
|
| 106 |
+
float lap = 4.0 * dot(col, lw) - dot(n, lw) - dot(s, lw) - dot(e, lw) - dot(w, lw);
|
| 107 |
+
col *= 1.0 + uSharpen * clamp(lap, -0.35, 0.35) / max(dot(col, lw), 0.03);
|
| 108 |
+
}
|
| 109 |
// split toning: warm highlights, teal-blue shadows (stronger at night)
|
| 110 |
float l = dot(col, vec3(0.2126, 0.7152, 0.0722));
|
| 111 |
vec3 shadowTint = mix(vec3(0.96, 1.0, 1.05), vec3(0.9, 0.98, 1.12), uNight);
|
|
|
|
| 117 |
float vig = 1.0 - uVignette * smoothstep(0.12, 0.75, r2 * 1.6);
|
| 118 |
col *= vig;
|
| 119 |
// film grain (luma weighted, frame-locked to film time so export is deterministic)
|
| 120 |
+
float g = h(floor(vUv * uRes) + fract(uTime * 24.0) * 97.0) - 0.5;
|
| 121 |
col += g * uGrain * (0.6 + 0.4 * (1.0 - l));
|
| 122 |
col *= 1.0 - uFade;
|
| 123 |
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
|
|
|
| 145 |
composer.addPass(new OutputPass())
|
| 146 |
const grade = new ShaderPass(GradeShader)
|
| 147 |
composer.addPass(grade)
|
| 148 |
+
// W × H is the internal (possibly supersampled) size; the caller sets grade.uRes / uSS to the output size
|
| 149 |
const setSize = (W: number, H: number) => {
|
| 150 |
composer.setSize(W, H)
|
|
|
|
| 151 |
rays.uniforms.uAspect.value = W / H
|
| 152 |
}
|
| 153 |
setSize(w, h)
|
src/jiangnan/engine/timeline.ts
CHANGED
|
@@ -91,5 +91,6 @@ export function endAlpha(t: number) {
|
|
| 91 |
}
|
| 92 |
export function fadeBlack(t: number) {
|
| 93 |
// fade in from black at start, fade out at the end
|
| 94 |
-
|
|
|
|
| 95 |
}
|
|
|
|
| 91 |
}
|
| 92 |
export function fadeBlack(t: number) {
|
| 93 |
// fade in from black at start, fade out at the end
|
| 94 |
+
// (v3: the opening fade is 0.9 s instead of 1.6 s so the first second is not a black screen)
|
| 95 |
+
return Math.max(1 - smoothstep(0, 0.9, t), smoothstep(DURATION - 1.2, DURATION, t))
|
| 96 |
}
|
src/jiangnan/export/covers.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
// then typeset with calligraphy titles.
|
| 3 |
import type { Film } from '../engine/Film'
|
| 4 |
import { drawSeal, FONT_BODY, FONT_TITLE } from '../player/overlay'
|
|
|
|
| 5 |
|
| 6 |
export type Ratio = '16:9' | '4:3' | '3:4'
|
| 7 |
export const RATIOS: { id: Ratio; w: number; h: number; label: string }[] = [
|
|
@@ -22,19 +23,19 @@ function vertical(g: CanvasRenderingContext2D, text: string, x: number, y: numbe
|
|
| 22 |
Array.from(text).forEach((c, i) => g.fillText(c, x, y + i * size * gap))
|
| 23 |
}
|
| 24 |
|
| 25 |
-
export function renderCover(film: Film, d: CoverDesign, ratio: Ratio): HTMLCanvasElement {
|
| 26 |
const R = RATIOS.find((r) => r.id === ratio)!
|
| 27 |
const W = R.w, H = R.h
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
const u = Math.min(W, H)
|
| 39 |
const portrait = H > W
|
| 40 |
g.textAlign = 'center'
|
|
|
|
| 2 |
// then typeset with calligraphy titles.
|
| 3 |
import type { Film } from '../engine/Film'
|
| 4 |
import { drawSeal, FONT_BODY, FONT_TITLE } from '../player/overlay'
|
| 5 |
+
import { createGrabber } from './frame'
|
| 6 |
|
| 7 |
export type Ratio = '16:9' | '4:3' | '3:4'
|
| 8 |
export const RATIOS: { id: Ratio; w: number; h: number; label: string }[] = [
|
|
|
|
| 23 |
Array.from(text).forEach((c, i) => g.fillText(c, x, y + i * size * gap))
|
| 24 |
}
|
| 25 |
|
| 26 |
+
export async function renderCover(film: Film, d: CoverDesign, ratio: Ratio): Promise<HTMLCanvasElement> {
|
| 27 |
const R = RATIOS.find((r) => r.id === ratio)!
|
| 28 |
const W = R.w, H = R.h
|
| 29 |
+
// supersampled + verified render (never a black cover after a GPU hiccup)
|
| 30 |
+
const grabber = createGrabber(film, W, H, { sharpen: 0.2 })
|
| 31 |
+
let c: HTMLCanvasElement, g: CanvasRenderingContext2D
|
| 32 |
+
try {
|
| 33 |
+
await grabber.grab(d.t, false)
|
| 34 |
+
c = grabber.comp
|
| 35 |
+
g = grabber.g
|
| 36 |
+
} finally {
|
| 37 |
+
grabber.release()
|
| 38 |
+
}
|
| 39 |
const u = Math.min(W, H)
|
| 40 |
const portrait = H > W
|
| 41 |
g.textAlign = 'center'
|
src/jiangnan/export/frame.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Safe frame grabbing for every offline output (MP4, covers, Live Photos).
|
| 2 |
+
//
|
| 3 |
+
// Why this exists: the single most common way a browser-rendered export ends up as a black
|
| 4 |
+
// video is a lost WebGL context (GPU memory pressure, tab throttling, driver resets). Once
|
| 5 |
+
// the context is gone every `drawImage` of the WebGL canvas silently yields black pixels,
|
| 6 |
+
// and a naive loop happily encodes them. Here every frame is rendered, copied, and *verified*
|
| 7 |
+
// before it is handed to an encoder; on failure we wait for the context, shrink the render
|
| 8 |
+
// budget, and re-render the same film time — the output never contains an unintended black frame.
|
| 9 |
+
import type { Film } from '../engine/Film'
|
| 10 |
+
import { fadeBlack } from '../engine/timeline'
|
| 11 |
+
|
| 12 |
+
export const isMobile = () => typeof navigator !== 'undefined' && /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent)
|
| 13 |
+
|
| 14 |
+
/** Yield to the event loop without the 1 s clamp that background tabs apply to setTimeout. */
|
| 15 |
+
export function yieldToBrowser(): Promise<void> {
|
| 16 |
+
if (typeof MessageChannel === 'undefined') return new Promise((r) => setTimeout(r, 0))
|
| 17 |
+
return new Promise((r) => {
|
| 18 |
+
const ch = new MessageChannel()
|
| 19 |
+
ch.port1.onmessage = () => { ch.port1.close(); r() }
|
| 20 |
+
ch.port2.postMessage(0)
|
| 21 |
+
})
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))
|
| 25 |
+
|
| 26 |
+
/**
|
| 27 |
+
* Choose the internal supersampling factor for an output of W × H pixels from the GPU limits
|
| 28 |
+
* and a conservative memory budget (the post chain keeps ~40 bytes per internal pixel alive).
|
| 29 |
+
*/
|
| 30 |
+
export function pickSupersample(film: Film, W: number, H: number): number {
|
| 31 |
+
let maxTex = 4096
|
| 32 |
+
try {
|
| 33 |
+
const gl = film.renderer.getContext()
|
| 34 |
+
maxTex = Math.min(gl.getParameter(gl.MAX_TEXTURE_SIZE) as number, gl.getParameter(gl.MAX_RENDERBUFFER_SIZE) as number)
|
| 35 |
+
} catch { /* keep default */ }
|
| 36 |
+
// once a context has been lost on this page, stay small for the rest of the session
|
| 37 |
+
const budget = film.lostCount > 0 ? 2.3e6 : isMobile() ? 4.2e6 : 9.0e6
|
| 38 |
+
for (const ss of [2, 1.5, 1.25, 1]) {
|
| 39 |
+
const w = Math.round(W * ss), h = Math.round(H * ss)
|
| 40 |
+
if (w <= maxTex && h <= maxTex && w * h <= budget) return ss
|
| 41 |
+
}
|
| 42 |
+
return 1
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
export interface GrabStats {
|
| 46 |
+
frames: number
|
| 47 |
+
/** frames that had to be re-rendered because the first attempt came back black */
|
| 48 |
+
retries: number
|
| 49 |
+
/** number of context losses observed during this job */
|
| 50 |
+
contextLosses: number
|
| 51 |
+
/** supersampling factor actually used for the last frame */
|
| 52 |
+
ss: number
|
| 53 |
+
degraded: boolean
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
export interface Grabber {
|
| 57 |
+
/** the W × H 2D canvas holding the latest verified frame (draw overlays on it, then encode) */
|
| 58 |
+
comp: HTMLCanvasElement
|
| 59 |
+
g: CanvasRenderingContext2D
|
| 60 |
+
/** render film time t into `comp`; throws if no non-black frame can be produced */
|
| 61 |
+
grab: (t: number, expectBlack?: boolean) => Promise<void>
|
| 62 |
+
/** restore the film to its interactive size / settings */
|
| 63 |
+
release: () => void
|
| 64 |
+
stats: GrabStats
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
export interface GrabOptions {
|
| 68 |
+
/** force a supersampling factor (default: pickSupersample) */
|
| 69 |
+
ss?: number
|
| 70 |
+
/** unsharp-mask amount after downsampling (0 … 0.3) */
|
| 71 |
+
sharpen?: number
|
| 72 |
+
/** sun shadow-map resolution while grabbing */
|
| 73 |
+
shadowRes?: number
|
| 74 |
+
/** reflection resolution relative to the internal frame */
|
| 75 |
+
reflScale?: number
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/** Maximum luminance (0–255) of a frame, measured on a 32 × 18 thumbnail. */
|
| 79 |
+
function maxLuma(src: HTMLCanvasElement, probe: CanvasRenderingContext2D) {
|
| 80 |
+
probe.drawImage(src, 0, 0, 32, 18)
|
| 81 |
+
const d = probe.getImageData(0, 0, 32, 18).data
|
| 82 |
+
let mx = 0
|
| 83 |
+
for (let i = 0; i < d.length; i += 4) {
|
| 84 |
+
const l = 0.2126 * d[i] + 0.7152 * d[i + 1] + 0.0722 * d[i + 2]
|
| 85 |
+
if (l > mx) mx = l
|
| 86 |
+
}
|
| 87 |
+
return mx
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
/**
|
| 91 |
+
* Put the film into "offline output" mode at W × H and return a verified frame grabber.
|
| 92 |
+
* The film's interactive size, supersampling, MSAA, shadow and reflection settings are
|
| 93 |
+
* restored by `release()`.
|
| 94 |
+
*/
|
| 95 |
+
export function createGrabber(film: Film, W: number, H: number, o: GrabOptions = {}): Grabber {
|
| 96 |
+
const prev = { w: film.width, h: film.height, ss: film.ss, samples: film.postSamples, shadow: film.shadowRes, refl: film.reflScale, sharpen: film.post.grade.uniforms.uSharpen.value as number }
|
| 97 |
+
const comp = document.createElement('canvas')
|
| 98 |
+
comp.width = W
|
| 99 |
+
comp.height = H
|
| 100 |
+
const g = comp.getContext('2d')!
|
| 101 |
+
const probeC = document.createElement('canvas')
|
| 102 |
+
probeC.width = 32
|
| 103 |
+
probeC.height = 18
|
| 104 |
+
const probe = probeC.getContext('2d', { willReadFrequently: true })!
|
| 105 |
+
const stats: GrabStats = { frames: 0, retries: 0, contextLosses: 0, ss: 1, degraded: false }
|
| 106 |
+
let ss = o.ss ?? pickSupersample(film, W, H)
|
| 107 |
+
|
| 108 |
+
const apply = () => {
|
| 109 |
+
// supersampling already anti-aliases: do not pay for MSAA on top of it
|
| 110 |
+
const samples = ss > 1.01 ? 0 : film.lostCount > 0 ? 0 : 4
|
| 111 |
+
if (samples !== film.postSamples) film.rebuildPost(samples)
|
| 112 |
+
film.reflScale = o.reflScale ?? (ss >= 1.5 ? 0.6 : 1)
|
| 113 |
+
film.setShadowRes(o.shadowRes ?? (film.lostCount > 0 ? 2048 : 4096))
|
| 114 |
+
film.setSize(W, H, ss)
|
| 115 |
+
film.post.grade.uniforms.uSharpen.value = o.sharpen ?? 0.18
|
| 116 |
+
stats.ss = ss
|
| 117 |
+
}
|
| 118 |
+
apply()
|
| 119 |
+
|
| 120 |
+
const degrade = () => {
|
| 121 |
+
const next = ss >= 2 ? 1.5 : ss > 1 ? 1 : 1
|
| 122 |
+
stats.degraded = true
|
| 123 |
+
ss = next
|
| 124 |
+
apply()
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
const grab = async (t: number, expectBlack = fadeBlack(t) > 0.97) => {
|
| 128 |
+
let lastErr: unknown = null
|
| 129 |
+
for (let attempt = 0; attempt < 4; attempt++) {
|
| 130 |
+
if (film.isContextLost()) {
|
| 131 |
+
stats.contextLosses++
|
| 132 |
+
const ok = await film.waitForContext(15000)
|
| 133 |
+
if (!ok) { lastErr = new Error('显卡(WebGL)上下文丢失后 15 秒内没有恢复'); continue }
|
| 134 |
+
degrade()
|
| 135 |
+
}
|
| 136 |
+
try {
|
| 137 |
+
film.renderFrame(t)
|
| 138 |
+
// clear first: drawing a canvas whose context is lost paints nothing, which would
|
| 139 |
+
// otherwise leave the previous (valid) frame in place and fool the check below
|
| 140 |
+
g.fillStyle = '#000'
|
| 141 |
+
g.fillRect(0, 0, W, H)
|
| 142 |
+
g.drawImage(film.renderer.domElement, 0, 0, W, H)
|
| 143 |
+
} catch (e) {
|
| 144 |
+
lastErr = e
|
| 145 |
+
stats.retries++
|
| 146 |
+
await sleep(50)
|
| 147 |
+
continue
|
| 148 |
+
}
|
| 149 |
+
if (expectBlack || maxLuma(comp, probe) >= 6) { stats.frames++; return }
|
| 150 |
+
// black where the film is not black: the context is lost (or on its way), or the GPU
|
| 151 |
+
// dropped the frame — wait, shrink the budget, and render again
|
| 152 |
+
stats.retries++
|
| 153 |
+
lastErr = new Error(`第 ${t.toFixed(2)} 秒渲染结果为全黑`)
|
| 154 |
+
await sleep(attempt === 0 ? 30 : 400)
|
| 155 |
+
if (!film.isContextLost()) degrade()
|
| 156 |
+
}
|
| 157 |
+
throw new Error(`${(lastErr as Error)?.message || '渲染失败'}。已停止导出以避免输出黑屏视频——请关闭其他占用显卡的标签页 / 程序,选择更低的清晰度后重试。`)
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
const release = () => {
|
| 161 |
+
film.post.grade.uniforms.uSharpen.value = prev.sharpen
|
| 162 |
+
film.reflScale = prev.refl
|
| 163 |
+
film.setShadowRes(prev.shadow)
|
| 164 |
+
if (film.postSamples !== prev.samples) film.rebuildPost(prev.samples)
|
| 165 |
+
film.setSize(prev.w, prev.h, prev.ss)
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
return { comp, g, grab, release, stats }
|
| 169 |
+
}
|
src/jiangnan/export/livegen.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { strToU8, zipSync, Zippable } from 'fflate'
|
|
| 5 |
import { LIVE_COPY } from '../copy/platforms'
|
| 6 |
import type { Film } from '../engine/Film'
|
| 7 |
import { drawSeal, FONT_BODY, FONT_TITLE } from '../player/overlay'
|
|
|
|
| 8 |
import { inspectLiveJpeg, PVT_PLIST, sha256, uuid4, VideoSample, writeLiveJpeg, writeLiveMov } from './livephoto'
|
| 9 |
|
| 10 |
export interface LiveClip { id: string; name: string; t0: number; cover: number; caption: string }
|
|
@@ -61,13 +62,9 @@ export async function generateLivePhotos(film: Film, onProgress: (p: number, lab
|
|
| 61 |
const previews: LiveResult['previews'] = []
|
| 62 |
const manifest: unknown[] = []
|
| 63 |
const root = '暮归江南_实况图'
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
comp
|
| 67 |
-
const g = comp.getContext('2d')!
|
| 68 |
-
const prevW = film.width, prevH = film.height
|
| 69 |
-
film.setSize(Math.round(W * 1.4), Math.round(H * 1.4)) // supersampled
|
| 70 |
-
g.imageSmoothingQuality = 'high'
|
| 71 |
try {
|
| 72 |
for (let ci = 0; ci < clips.length; ci++) {
|
| 73 |
const clip = clips[ci]
|
|
@@ -96,8 +93,7 @@ export async function generateLivePhotos(film: Film, onProgress: (p: number, lab
|
|
| 96 |
if (signal.cancelled) throw new Error('已取消')
|
| 97 |
if (err) throw err
|
| 98 |
const t = clip.t0 + i / FPS
|
| 99 |
-
|
| 100 |
-
g.drawImage(film.renderer.domElement, 0, 0, W, H)
|
| 101 |
caption(g, clip.caption)
|
| 102 |
if (i === coverIdx) {
|
| 103 |
const blob = await new Promise<Blob>((res, rej) => comp.toBlob((b) => (b ? res(b) : rej(new Error('封面导出失败'))), 'image/jpeg', 0.95))
|
|
@@ -110,7 +106,7 @@ export async function generateLivePhotos(film: Film, onProgress: (p: number, lab
|
|
| 110 |
while (enc.encodeQueueSize > 3) await new Promise((r) => setTimeout(r, 2))
|
| 111 |
if (i % 6 === 0) {
|
| 112 |
onProgress((ci + i / FRAMES) / clips.length, `正在渲染第 ${ci + 1}/${clips.length} 张:${clip.name}`)
|
| 113 |
-
await
|
| 114 |
}
|
| 115 |
}
|
| 116 |
await enc.flush()
|
|
@@ -135,7 +131,7 @@ export async function generateLivePhotos(film: Film, onProgress: (p: number, lab
|
|
| 135 |
})
|
| 136 |
}
|
| 137 |
} finally {
|
| 138 |
-
|
| 139 |
}
|
| 140 |
const copy = LIVE_COPY.map((c) => `【${c.name}】\n标题(任选其一,≤${c.titleMax} 字):\n${c.titles.map((t) => '· ' + t).join('\n')}\n\n正文:\n${c.body}\n\n话题(${c.tags.length}/${c.tagMax}):\n${c.tags.map((t) => '#' + t).join(' ')}\n`).join('\n————————————\n\n')
|
| 141 |
const readme = `暮归江南 · 实况图(Apple Live Photo)\n\n共 ${LIVE_CLIPS.length} 组,每组 3 秒、30 fps、1080×1440(3:4),无声 H.264。\n\n使用方法(Mac):\n1. 打开「1_jpg+mov(Mac 直接导入照片)」文件夹,全选同名的 .jpg 和 .mov,拖进「照片」App(或 文件 → 导入),会自动合成为实况照片;\n2. 通过 iCloud 照片同步到 iPhone 后,在小红书 / 抖音发布图文时选择这些实况照片即可(发布时保持“实况”开启)。\n\n「2_pvt实况资源包」是 LiveCanvas 规范的 .pvt 目录包,可在 Mac 上作为单个实况照片打开或导入。\n\n技术说明:JPEG 的 Exif MakerNote(Apple tag 17)与 MOV 的 com.apple.quicktime.content.identifier 使用同一个 UUID;MOV 内含 mebx 定时元数据轨(com.apple.quicktime.still-image-time),指向封面那一帧。生成器在浏览器本地运行,已用 LiveCanvas skill 原版 Python 校验器(verify_pair / package_live_photo)验证过同一套写入逻辑。\n导入「照片」和手机播放属于接收端操作,本包未在你的设备上测试(manifest 中记为 not_tested)。\n`
|
|
|
|
| 5 |
import { LIVE_COPY } from '../copy/platforms'
|
| 6 |
import type { Film } from '../engine/Film'
|
| 7 |
import { drawSeal, FONT_BODY, FONT_TITLE } from '../player/overlay'
|
| 8 |
+
import { createGrabber, yieldToBrowser } from './frame'
|
| 9 |
import { inspectLiveJpeg, PVT_PLIST, sha256, uuid4, VideoSample, writeLiveJpeg, writeLiveMov } from './livephoto'
|
| 10 |
|
| 11 |
export interface LiveClip { id: string; name: string; t0: number; cover: number; caption: string }
|
|
|
|
| 62 |
const previews: LiveResult['previews'] = []
|
| 63 |
const manifest: unknown[] = []
|
| 64 |
const root = '暮归江南_实况图'
|
| 65 |
+
// supersampled + verified frames (see frame.ts)
|
| 66 |
+
const grabber = createGrabber(film, W, H, { sharpen: 0.18 })
|
| 67 |
+
const { comp, g } = grabber
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try {
|
| 69 |
for (let ci = 0; ci < clips.length; ci++) {
|
| 70 |
const clip = clips[ci]
|
|
|
|
| 93 |
if (signal.cancelled) throw new Error('已取消')
|
| 94 |
if (err) throw err
|
| 95 |
const t = clip.t0 + i / FPS
|
| 96 |
+
await grabber.grab(t, false)
|
|
|
|
| 97 |
caption(g, clip.caption)
|
| 98 |
if (i === coverIdx) {
|
| 99 |
const blob = await new Promise<Blob>((res, rej) => comp.toBlob((b) => (b ? res(b) : rej(new Error('封面导出失败'))), 'image/jpeg', 0.95))
|
|
|
|
| 106 |
while (enc.encodeQueueSize > 3) await new Promise((r) => setTimeout(r, 2))
|
| 107 |
if (i % 6 === 0) {
|
| 108 |
onProgress((ci + i / FRAMES) / clips.length, `正在渲染第 ${ci + 1}/${clips.length} 张:${clip.name}`)
|
| 109 |
+
await yieldToBrowser()
|
| 110 |
}
|
| 111 |
}
|
| 112 |
await enc.flush()
|
|
|
|
| 131 |
})
|
| 132 |
}
|
| 133 |
} finally {
|
| 134 |
+
grabber.release()
|
| 135 |
}
|
| 136 |
const copy = LIVE_COPY.map((c) => `【${c.name}】\n标题(任选其一,≤${c.titleMax} 字):\n${c.titles.map((t) => '· ' + t).join('\n')}\n\n正文:\n${c.body}\n\n话题(${c.tags.length}/${c.tagMax}):\n${c.tags.map((t) => '#' + t).join(' ')}\n`).join('\n————————————\n\n')
|
| 137 |
const readme = `暮归江南 · 实况图(Apple Live Photo)\n\n共 ${LIVE_CLIPS.length} 组,每组 3 秒、30 fps、1080×1440(3:4),无声 H.264。\n\n使用方法(Mac):\n1. 打开「1_jpg+mov(Mac 直接导入照片)」文件夹,全选同名的 .jpg 和 .mov,拖进「照片」App(或 文件 → 导入),会自动合成为实况照片;\n2. 通过 iCloud 照片同步到 iPhone 后,在小红书 / 抖音发布图文时选择这些实况照片即可(发布时保持“实况”开启)。\n\n「2_pvt实况资源包」是 LiveCanvas 规范的 .pvt 目录包,可在 Mac 上作为单个实况照片打开或导入。\n\n技术说明:JPEG 的 Exif MakerNote(Apple tag 17)与 MOV 的 com.apple.quicktime.content.identifier 使用同一个 UUID;MOV 内含 mebx 定时元数据轨(com.apple.quicktime.still-image-time),指向封面那一帧。生成器在浏览器本地运行,已用 LiveCanvas skill 原版 Python 校验器(verify_pair / package_live_photo)验证过同一套写入逻辑。\n导入「照片」和手机播放属于接收端操作,本包未在你的设备上测试(manifest 中记为 not_tested)。\n`
|
src/jiangnan/export/mp4.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
// In-browser MP4 export: deterministic frame-by-frame rendering → WebCodecs
|
| 2 |
// H.264 (+ AAC / Opus audio) → mp4-muxer. Nothing leaves the user's device.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import { ArrayBufferTarget, Muxer } from 'mp4-muxer'
|
| 4 |
import type { Film } from '../engine/Film'
|
| 5 |
import { DURATION } from '../engine/timeline'
|
| 6 |
import { drawOverlay, OverlayOpts } from '../player/overlay'
|
|
|
|
| 7 |
|
| 8 |
export interface ExportOptions {
|
| 9 |
width: number
|
|
@@ -17,6 +22,14 @@ export interface ExportOptions {
|
|
| 17 |
start?: number
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
export function exportSupport() {
|
| 21 |
return {
|
| 22 |
video: typeof window !== 'undefined' && 'VideoEncoder' in window,
|
|
@@ -24,24 +37,60 @@ export function exportSupport() {
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
async function pickVideoCodec(w: number, h: number, fps: number, bitrate: number) {
|
| 28 |
-
const
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
}
|
| 38 |
-
throw new Error('当前浏览器不支持 H.264 编码,请使用最新版 Chrome / Edge / Safari')
|
| 39 |
}
|
| 40 |
|
| 41 |
async function pickAudioCodec() {
|
| 42 |
if (!('AudioEncoder' in window)) return null
|
| 43 |
for (const [codec, mux] of [['mp4a.40.2', 'aac'], ['opus', 'opus']] as const) {
|
| 44 |
-
const cfg: AudioEncoderConfig = { codec, sampleRate: 48000, numberOfChannels: 2, bitrate:
|
| 45 |
try {
|
| 46 |
const s = await AudioEncoder.isConfigSupported(cfg)
|
| 47 |
if (s.supported) return { cfg, mux }
|
|
@@ -50,13 +99,14 @@ async function pickAudioCodec() {
|
|
| 50 |
return null
|
| 51 |
}
|
| 52 |
|
|
|
|
| 53 |
export function suggestedBitrate(w: number, h: number, fps: number) {
|
| 54 |
const px = w * h
|
| 55 |
-
const base = px >= 1920 * 1080 * 0.9 ?
|
| 56 |
-
return Math.round(base * (fps >= 50 ? 1.5 : fps <= 24 ? 0.
|
| 57 |
}
|
| 58 |
|
| 59 |
-
export async function exportMp4(film: Film, o: ExportOptions): Promise<
|
| 60 |
const { width: W, height: H, fps } = o
|
| 61 |
const start = o.start ?? 0
|
| 62 |
const dur = o.duration ?? DURATION
|
|
@@ -80,7 +130,7 @@ export async function exportMp4(film: Film, o: ExportOptions): Promise<{ blob: B
|
|
| 80 |
const ae = new AudioEncoder({ output: (c, m) => muxer.addAudioChunk(c, m), error: (e) => { encErr = e } })
|
| 81 |
ae.configure(acodec.cfg)
|
| 82 |
const buf = o.audio
|
| 83 |
-
const L = buf.getChannelData(0), R = buf.getChannelData(1)
|
| 84 |
const s0 = Math.floor(start * 48000)
|
| 85 |
const len = Math.min(L.length - s0, Math.round(dur * 48000))
|
| 86 |
const block = 4800
|
|
@@ -92,7 +142,7 @@ export async function exportMp4(film: Film, o: ExportOptions): Promise<{ blob: B
|
|
| 92 |
const ad = new AudioData({ format: 'f32-planar', sampleRate: 48000, numberOfFrames: n, numberOfChannels: 2, timestamp: Math.round((i / 48000) * 1e6), data })
|
| 93 |
ae.encode(ad)
|
| 94 |
ad.close()
|
| 95 |
-
if (ae.encodeQueueSize > 20) await
|
| 96 |
}
|
| 97 |
await ae.flush()
|
| 98 |
ae.close()
|
|
@@ -101,42 +151,38 @@ export async function exportMp4(film: Film, o: ExportOptions): Promise<{ blob: B
|
|
| 101 |
// ---- video
|
| 102 |
const ve = new VideoEncoder({ output: (c, m) => muxer.addVideoChunk(c, m), error: (e) => { encErr = e } })
|
| 103 |
ve.configure(vcfg)
|
| 104 |
-
const
|
| 105 |
-
comp
|
| 106 |
-
comp.height = H
|
| 107 |
-
const g = comp.getContext('2d')!
|
| 108 |
-
const prevW = film.width, prevH = film.height
|
| 109 |
-
// supersampling: render larger, then filter down → crisp edges, no crawling on fine detail
|
| 110 |
-
const ss = W * H <= 1280 * 720 ? 2 : 1.5
|
| 111 |
-
film.setSize(Math.round(W * ss), Math.round(H * ss))
|
| 112 |
-
g.imageSmoothingEnabled = true
|
| 113 |
-
g.imageSmoothingQuality = 'high'
|
| 114 |
const t0 = performance.now()
|
| 115 |
try {
|
|
|
|
|
|
|
|
|
|
| 116 |
for (let i = 0; i < total; i++) {
|
| 117 |
if (o.signal.cancelled) throw new Error('已取消')
|
| 118 |
if (encErr) throw encErr
|
| 119 |
const t = start + i / fps
|
| 120 |
-
|
| 121 |
-
g.drawImage(film.renderer.domElement, 0, 0, W, H)
|
| 122 |
drawOverlay(g, t, W, H, o.overlay)
|
| 123 |
const frame = new VideoFrame(comp, { timestamp: Math.round((i * 1e6) / fps), duration: Math.round(1e6 / fps) })
|
| 124 |
ve.encode(frame, { keyFrame: i % (fps * 2) === 0 })
|
| 125 |
frame.close()
|
| 126 |
-
while (ve.encodeQueueSize > 3) await new Promise((r) => setTimeout(r, 2))
|
| 127 |
if (i % 3 === 0) {
|
| 128 |
const el = (performance.now() - t0) / 1000
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
}
|
| 132 |
}
|
| 133 |
o.onProgress(1, { frame: total, total, eta: 0, stage: '封装 MP4' })
|
| 134 |
await ve.flush()
|
| 135 |
ve.close()
|
|
|
|
| 136 |
muxer.finalize()
|
| 137 |
} finally {
|
| 138 |
-
|
| 139 |
if (ve.state !== 'closed') try { ve.close() } catch { /* ignore */ }
|
| 140 |
}
|
| 141 |
-
return { blob: new Blob([target.buffer], { type: 'video/mp4' }), audioCodec: acodec?.mux ?? null }
|
| 142 |
}
|
|
|
|
| 1 |
// In-browser MP4 export: deterministic frame-by-frame rendering → WebCodecs
|
| 2 |
// H.264 (+ AAC / Opus audio) → mp4-muxer. Nothing leaves the user's device.
|
| 3 |
+
//
|
| 4 |
+
// v3: every frame is verified before encoding (see frame.ts), the H.264 level is the lowest
|
| 5 |
+
// one that fits the picture (phones / WeChat / 视频号 players reject level 5.x streams and
|
| 6 |
+
// show a black picture with sound), bitrates are raised, and the picture is supersampled.
|
| 7 |
import { ArrayBufferTarget, Muxer } from 'mp4-muxer'
|
| 8 |
import type { Film } from '../engine/Film'
|
| 9 |
import { DURATION } from '../engine/timeline'
|
| 10 |
import { drawOverlay, OverlayOpts } from '../player/overlay'
|
| 11 |
+
import { createGrabber, GrabStats, yieldToBrowser } from './frame'
|
| 12 |
|
| 13 |
export interface ExportOptions {
|
| 14 |
width: number
|
|
|
|
| 22 |
start?: number
|
| 23 |
}
|
| 24 |
|
| 25 |
+
export interface ExportResult {
|
| 26 |
+
blob: Blob
|
| 27 |
+
audioCodec: string | null
|
| 28 |
+
videoCodec: string
|
| 29 |
+
bitrate: number
|
| 30 |
+
stats: GrabStats
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
export function exportSupport() {
|
| 34 |
return {
|
| 35 |
video: typeof window !== 'undefined' && 'VideoEncoder' in window,
|
|
|
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
+
// H.264 levels: [level_idc, MaxFS (macroblocks), MaxMBPS (macroblocks / s), MaxBR kbit/s (Main; High = ×1.25)]
|
| 41 |
+
const AVC_LEVELS: [number, number, number, number][] = [
|
| 42 |
+
[0x1f, 3600, 108000, 14000], // 3.1 720p30
|
| 43 |
+
[0x20, 5120, 216000, 20000], // 3.2 720p60
|
| 44 |
+
[0x28, 8192, 245760, 20000], // 4.0 1080p30
|
| 45 |
+
[0x29, 8192, 245760, 50000], // 4.1
|
| 46 |
+
[0x2a, 8704, 522240, 50000], // 4.2 1080p60
|
| 47 |
+
[0x32, 22080, 589824, 135000], // 5.0 1440p30
|
| 48 |
+
[0x33, 36864, 983040, 240000], // 5.1 1440p60 / 2160p30
|
| 49 |
+
[0x34, 36864, 2073600, 240000], // 5.2 2160p60
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
/** The lowest H.264 level that can carry w × h @ fps at the given bitrate. */
|
| 53 |
+
export function avcLevelFor(w: number, h: number, fps: number, bitrate: number) {
|
| 54 |
+
const mbs = Math.ceil(w / 16) * Math.ceil(h / 16)
|
| 55 |
+
const mbps = mbs * fps
|
| 56 |
+
for (const [idc, maxFS, maxMBPS, maxBR] of AVC_LEVELS) {
|
| 57 |
+
if (mbs <= maxFS && mbps <= maxMBPS && bitrate <= maxBR * 1000 * 1.25) return idc
|
| 58 |
+
}
|
| 59 |
+
return 0x34
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
export function codecName(cfg: VideoEncoderConfig) {
|
| 63 |
+
const c = cfg.codec
|
| 64 |
+
const p = c.slice(5, 7), l = parseInt(c.slice(9, 11), 16)
|
| 65 |
+
const prof = p === '64' ? 'High' : p === '4D' ? 'Main' : 'Baseline'
|
| 66 |
+
return `H.264 ${prof} @ L${(l / 10).toFixed(1)}`
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
async function pickVideoCodec(w: number, h: number, fps: number, bitrate: number) {
|
| 70 |
+
const minIdc = avcLevelFor(w, h, fps, bitrate)
|
| 71 |
+
const levels = AVC_LEVELS.map((l) => l[0]).filter((l) => l >= minIdc)
|
| 72 |
+
// High → Main → Constrained Baseline; at each profile, the lowest level first
|
| 73 |
+
const profiles = ['6400', '4D40', '42E0']
|
| 74 |
+
let lastErr: unknown = null
|
| 75 |
+
for (const prof of profiles) {
|
| 76 |
+
for (const lvl of levels) {
|
| 77 |
+
const codec = `avc1.${prof}${lvl.toString(16).padStart(2, '0').toUpperCase()}`
|
| 78 |
+
for (const hw of ['prefer-hardware', 'no-preference'] as const) {
|
| 79 |
+
const cfg: VideoEncoderConfig = { codec, width: w, height: h, bitrate, framerate: fps, hardwareAcceleration: hw, avc: { format: 'avc' }, latencyMode: 'quality', bitrateMode: 'variable' }
|
| 80 |
+
try {
|
| 81 |
+
const s = await VideoEncoder.isConfigSupported(cfg)
|
| 82 |
+
if (s.supported) return cfg
|
| 83 |
+
} catch (e) { lastErr = e }
|
| 84 |
+
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
+
throw new Error('当前浏览器不支持 H.264 编码,请使用最新版 Chrome / Edge / Safari' + (lastErr ? `(${(lastErr as Error).message})` : ''))
|
| 88 |
}
|
| 89 |
|
| 90 |
async function pickAudioCodec() {
|
| 91 |
if (!('AudioEncoder' in window)) return null
|
| 92 |
for (const [codec, mux] of [['mp4a.40.2', 'aac'], ['opus', 'opus']] as const) {
|
| 93 |
+
const cfg: AudioEncoderConfig = { codec, sampleRate: 48000, numberOfChannels: 2, bitrate: 256000 }
|
| 94 |
try {
|
| 95 |
const s = await AudioEncoder.isConfigSupported(cfg)
|
| 96 |
if (s.supported) return { cfg, mux }
|
|
|
|
| 99 |
return null
|
| 100 |
}
|
| 101 |
|
| 102 |
+
/** Target bitrate: generous, because film grain, mist and water glitter are expensive to encode. */
|
| 103 |
export function suggestedBitrate(w: number, h: number, fps: number) {
|
| 104 |
const px = w * h
|
| 105 |
+
const base = px >= 2560 * 1440 * 0.9 ? 40e6 : px >= 1920 * 1080 * 0.9 ? 24e6 : 12e6
|
| 106 |
+
return Math.round(base * (fps >= 50 ? 1.5 : fps <= 24 ? 0.9 : 1))
|
| 107 |
}
|
| 108 |
|
| 109 |
+
export async function exportMp4(film: Film, o: ExportOptions): Promise<ExportResult> {
|
| 110 |
const { width: W, height: H, fps } = o
|
| 111 |
const start = o.start ?? 0
|
| 112 |
const dur = o.duration ?? DURATION
|
|
|
|
| 130 |
const ae = new AudioEncoder({ output: (c, m) => muxer.addAudioChunk(c, m), error: (e) => { encErr = e } })
|
| 131 |
ae.configure(acodec.cfg)
|
| 132 |
const buf = o.audio
|
| 133 |
+
const L = buf.getChannelData(0), R = buf.numberOfChannels > 1 ? buf.getChannelData(1) : L
|
| 134 |
const s0 = Math.floor(start * 48000)
|
| 135 |
const len = Math.min(L.length - s0, Math.round(dur * 48000))
|
| 136 |
const block = 4800
|
|
|
|
| 142 |
const ad = new AudioData({ format: 'f32-planar', sampleRate: 48000, numberOfFrames: n, numberOfChannels: 2, timestamp: Math.round((i / 48000) * 1e6), data })
|
| 143 |
ae.encode(ad)
|
| 144 |
ad.close()
|
| 145 |
+
if (ae.encodeQueueSize > 20) await yieldToBrowser()
|
| 146 |
}
|
| 147 |
await ae.flush()
|
| 148 |
ae.close()
|
|
|
|
| 151 |
// ---- video
|
| 152 |
const ve = new VideoEncoder({ output: (c, m) => muxer.addVideoChunk(c, m), error: (e) => { encErr = e } })
|
| 153 |
ve.configure(vcfg)
|
| 154 |
+
const grabber = createGrabber(film, W, H, { sharpen: 0.18 })
|
| 155 |
+
const { comp, g } = grabber
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
const t0 = performance.now()
|
| 157 |
try {
|
| 158 |
+
// probe frame: make sure the whole chain (render → copy → verify) works before we start
|
| 159 |
+
o.onProgress(0, { frame: 0, total, eta: 0, stage: '检查渲染链路' })
|
| 160 |
+
await grabber.grab(start + Math.min(dur, 3) * 0.5)
|
| 161 |
for (let i = 0; i < total; i++) {
|
| 162 |
if (o.signal.cancelled) throw new Error('已取消')
|
| 163 |
if (encErr) throw encErr
|
| 164 |
const t = start + i / fps
|
| 165 |
+
await grabber.grab(t)
|
|
|
|
| 166 |
drawOverlay(g, t, W, H, o.overlay)
|
| 167 |
const frame = new VideoFrame(comp, { timestamp: Math.round((i * 1e6) / fps), duration: Math.round(1e6 / fps) })
|
| 168 |
ve.encode(frame, { keyFrame: i % (fps * 2) === 0 })
|
| 169 |
frame.close()
|
| 170 |
+
while (ve.encodeQueueSize > 3) { await yieldToBrowser(); if (ve.encodeQueueSize > 3) await new Promise((r) => setTimeout(r, 2)) }
|
| 171 |
if (i % 3 === 0) {
|
| 172 |
const el = (performance.now() - t0) / 1000
|
| 173 |
+
const note = grabber.stats.retries ? `(已自动修复 ${grabber.stats.retries} 帧)` : ''
|
| 174 |
+
o.onProgress((i + 1) / total, { frame: i + 1, total, eta: (el / (i + 1)) * (total - i - 1), stage: `渲染画面 ${grabber.stats.ss}×超采样${note}` })
|
| 175 |
+
await yieldToBrowser()
|
| 176 |
}
|
| 177 |
}
|
| 178 |
o.onProgress(1, { frame: total, total, eta: 0, stage: '封装 MP4' })
|
| 179 |
await ve.flush()
|
| 180 |
ve.close()
|
| 181 |
+
if (encErr) throw encErr
|
| 182 |
muxer.finalize()
|
| 183 |
} finally {
|
| 184 |
+
grabber.release()
|
| 185 |
if (ve.state !== 'closed') try { ve.close() } catch { /* ignore */ }
|
| 186 |
}
|
| 187 |
+
return { blob: new Blob([target.buffer], { type: 'video/mp4' }), audioCodec: acodec?.mux ?? null, videoCodec: codecName(vcfg), bitrate, stats: grabber.stats }
|
| 188 |
}
|
tools/jiangnan/lose_context.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Context-loss drill: render → force WEBGL_lose_context → restore → verify the picture comes back,
|
| 2 |
+
# both for the interactive renderer and for the verified export grabber (frame.ts).
|
| 3 |
+
# usage: lose_context.py <base-url>
|
| 4 |
+
import sys, time
|
| 5 |
+
from playwright.sync_api import sync_playwright
|
| 6 |
+
BASE = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:3000/jiangnan/'
|
| 7 |
+
LUMA = """(() => { const f = window.__film; const c = document.createElement('canvas'); c.width = 32; c.height = 18;
|
| 8 |
+
const g = c.getContext('2d'); g.drawImage(f.renderer.domElement, 0, 0, 32, 18); const d = g.getImageData(0, 0, 32, 18).data;
|
| 9 |
+
let s = 0; for (let i = 0; i < d.length; i += 4) s += 0.2126*d[i] + 0.7152*d[i+1] + 0.0722*d[i+2]; return Math.round(s / (d.length / 4)) })()"""
|
| 10 |
+
with sync_playwright() as p:
|
| 11 |
+
b = p.chromium.launch(args=['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist', '--disable-dev-shm-usage'])
|
| 12 |
+
pg = b.new_page(viewport={'width': 400, 'height': 225})
|
| 13 |
+
logs = []
|
| 14 |
+
pg.on('console', lambda m: logs.append(m.type + ': ' + m.text) if m.type in ('error', 'warning') else None)
|
| 15 |
+
pg.on('pageerror', lambda e: logs.append('PAGEERROR: ' + str(e)))
|
| 16 |
+
t0 = time.time()
|
| 17 |
+
pg.goto(BASE + '?w=320&h=180&noaudio=1&q=low', timeout=180000)
|
| 18 |
+
pg.wait_for_function("document.title === 'ready' || document.title === 'error'", timeout=900000)
|
| 19 |
+
print('init', round(time.time() - t0, 1), 's', pg.title(), flush=True)
|
| 20 |
+
# 1) interactive renderer
|
| 21 |
+
pg.evaluate("window.__film.renderFrame(30)")
|
| 22 |
+
print('before loss: luma =', pg.evaluate("(() => { window.__film.renderFrame(30); return " + LUMA + " })()"), flush=True)
|
| 23 |
+
pg.evaluate("window.__ext = window.__film.renderer.getContext().getExtension('WEBGL_lose_context'); window.__ext.loseContext()")
|
| 24 |
+
time.sleep(0.5)
|
| 25 |
+
print('while lost: isContextLost =', pg.evaluate("window.__film.isContextLost()"), ' luma =', pg.evaluate(LUMA), flush=True)
|
| 26 |
+
pg.evaluate("window.__ext.restoreContext()")
|
| 27 |
+
pg.wait_for_function("!window.__film.isContextLost()", timeout=60000)
|
| 28 |
+
ok = pg.evaluate("window.__film.waitForContext(60000)")
|
| 29 |
+
# render + read back inside one task (preserveDrawingBuffer is off, so the buffer only lives until the next composite)
|
| 30 |
+
l1 = pg.evaluate("(() => { window.__film.renderFrame(30); return " + LUMA + " })()")
|
| 31 |
+
l2 = pg.evaluate("(() => { window.__film.renderFrame(30.5); return " + LUMA + " })()")
|
| 32 |
+
print('after restore: waitForContext =', ok, ' luma(1st frame) =', l1, ' luma(2nd frame) =', l2, ' lostCount =', pg.evaluate("window.__film.lostCount"), flush=True)
|
| 33 |
+
# 2) export grabber with a loss injected in the middle of the sequence
|
| 34 |
+
r = pg.evaluate("window.__test.grab(320, 180, [20, 21, 22, 23, 24, 25], 2)")
|
| 35 |
+
print('grabber with injected loss:', r, flush=True)
|
| 36 |
+
bad = [i for i, l in enumerate(r['lum']) if l < 6]
|
| 37 |
+
print('RESULT:', 'PASS (no black frame reached the output)' if not bad else f'FAIL black frames at {bad}')
|
| 38 |
+
for l in logs[-10:]: print(l)
|
| 39 |
+
b.close()
|
tools/jiangnan/scan_black.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Scan the whole film: render every `step` seconds at small size, report mean luminance / black frames / errors
|
| 2 |
+
import base64, io, sys, time, json
|
| 3 |
+
from playwright.sync_api import sync_playwright
|
| 4 |
+
from PIL import Image
|
| 5 |
+
BASE = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:3000/jiangnan/'
|
| 6 |
+
step = float(sys.argv[2]) if len(sys.argv) > 2 else 0.5
|
| 7 |
+
w, h = (int(sys.argv[3]), int(sys.argv[4])) if len(sys.argv) > 4 else (480, 270)
|
| 8 |
+
q = sys.argv[5] if len(sys.argv) > 5 else 'high'
|
| 9 |
+
with sync_playwright() as p:
|
| 10 |
+
b = p.chromium.launch(args=['--use-angle=swiftshader','--enable-unsafe-swiftshader','--ignore-gpu-blocklist','--disable-dev-shm-usage'])
|
| 11 |
+
pg = b.new_page(viewport={'width': w, 'height': h})
|
| 12 |
+
logs = []
|
| 13 |
+
pg.on('console', lambda m: logs.append(m.type + ': ' + m.text) if m.type in ('error','warning') else None)
|
| 14 |
+
pg.on('pageerror', lambda e: logs.append('PAGEERROR: ' + str(e)))
|
| 15 |
+
t0 = time.time()
|
| 16 |
+
pg.goto(f'{BASE}?w={w}&h={h}&noaudio=1&q={q}', timeout=180000)
|
| 17 |
+
pg.wait_for_function("document.title === 'ready' || document.title === 'error'", timeout=900000)
|
| 18 |
+
print('init', round(time.time() - t0, 1), 's', pg.title(), flush=True)
|
| 19 |
+
t = 0.0
|
| 20 |
+
rows = []
|
| 21 |
+
while t <= 86.0:
|
| 22 |
+
r = pg.evaluate(f"""(() => {{ const f = window.__film; const t0 = performance.now(); let err = null;
|
| 23 |
+
try {{ f.renderFrame({t}) }} catch (e) {{ err = String(e) }}
|
| 24 |
+
const ms = performance.now() - t0;
|
| 25 |
+
const c = document.createElement('canvas'); c.width = 48; c.height = 27; const g = c.getContext('2d');
|
| 26 |
+
g.drawImage(f.renderer.domElement, 0, 0, 48, 27); const d = g.getImageData(0, 0, 48, 27).data;
|
| 27 |
+
let sum = 0, mx = 0; for (let i = 0; i < d.length; i += 4) {{ const l = 0.2126*d[i] + 0.7152*d[i+1] + 0.0722*d[i+2]; sum += l; if (l > mx) mx = l; }}
|
| 28 |
+
return {{ mean: sum / (d.length / 4), max: mx, ms: Math.round(ms), err, lost: f.renderer.getContext().isContextLost() }} }})()""")
|
| 29 |
+
rows.append((t, r))
|
| 30 |
+
flag = ' <-- BLACK' if r['max'] < 8 else (' <-- DARK' if r['mean'] < 6 else '')
|
| 31 |
+
print(f"t={t:5.1f} mean={r['mean']:6.1f} max={r['max']:5.0f} ms={r['ms']:4d} lost={r['lost']} {r['err'] or ''}{flag}", flush=True)
|
| 32 |
+
t = round(t + step, 3)
|
| 33 |
+
print('--- logs')
|
| 34 |
+
for l in logs[-30:]: print(l)
|
| 35 |
+
b.close()
|
tools/jiangnan/shots.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Render stills at the given film times and save JPEGs: shots.py <base> <outdir> <w> <h> <q> t1,t2,...
|
| 2 |
+
import base64, io, sys, time
|
| 3 |
+
from playwright.sync_api import sync_playwright
|
| 4 |
+
from PIL import Image
|
| 5 |
+
BASE, out, w, h, q = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), sys.argv[5]
|
| 6 |
+
times = [float(x) for x in sys.argv[6].split(',')]
|
| 7 |
+
import os; os.makedirs(out, exist_ok=True)
|
| 8 |
+
with sync_playwright() as p:
|
| 9 |
+
b = p.chromium.launch(args=['--use-angle=swiftshader','--enable-unsafe-swiftshader','--ignore-gpu-blocklist','--disable-dev-shm-usage'])
|
| 10 |
+
pg = b.new_page(viewport={'width': w, 'height': h})
|
| 11 |
+
logs = []
|
| 12 |
+
pg.on('console', lambda m: logs.append(m.type + ': ' + m.text) if m.type in ('error','warning') else None)
|
| 13 |
+
pg.on('pageerror', lambda e: logs.append('PAGEERROR: ' + str(e)))
|
| 14 |
+
t0 = time.time()
|
| 15 |
+
pg.goto(f'{BASE}?w={w}&h={h}&noaudio=1&q={q}', timeout=180000)
|
| 16 |
+
pg.wait_for_function("document.title === 'ready' || document.title === 'error'", timeout=900000)
|
| 17 |
+
print('init', round(time.time() - t0, 1), 's', pg.title(), flush=True)
|
| 18 |
+
for t in times:
|
| 19 |
+
d = pg.evaluate(f"(() => {{ window.__film.renderFrame({t}); return window.__film.renderer.domElement.toDataURL('image/jpeg', 0.92) }})()")
|
| 20 |
+
img = Image.open(io.BytesIO(base64.b64decode(d.split(',')[1])))
|
| 21 |
+
img.save(f'{out}/t{t:05.1f}.jpg')
|
| 22 |
+
L = list(img.convert('L').getdata())
|
| 23 |
+
print(f't={t} mean={sum(L)/len(L):.1f} max={max(L)}', flush=True)
|
| 24 |
+
for l in logs[-10:]: print(l)
|
| 25 |
+
b.close()
|