// In-browser MP4 export: deterministic frame-by-frame rendering → WebCodecs // H.264 (+ AAC / Opus audio) → mp4-muxer. Nothing leaves the user's device. // // v3: every frame is verified before encoding (see frame.ts), the H.264 level is the lowest // one that fits the picture (phones / WeChat / 视频号 players reject level 5.x streams and // show a black picture with sound), bitrates are raised, and the picture is supersampled. import { ArrayBufferTarget, Muxer } from 'mp4-muxer' import type { Film } from '../engine/Film' import { DURATION } from '../engine/timeline' import { drawOverlay, OverlayOpts } from '../player/overlay' import { createGrabber, GrabStats, yieldToBrowser } from './frame' export interface ExportOptions { width: number height: number fps: 24 | 30 | 60 audio: AudioBuffer | null overlay: OverlayOpts onProgress: (p: number, info: { frame: number; total: number; eta: number; stage: string }) => void signal: { cancelled: boolean } duration?: number start?: number /** * auto: hardware encoder first, keyframe every 2 s. * compatible: software encoder first, 'realtime' latency mode (no frame re-ordering), keyframes * only every 10 s — for GPUs / drivers whose hardware H.264 encoder emits broken (black) frames. */ encoder?: 'auto' | 'compatible' } export interface ExportResult { blob: Blob audioCodec: string | null videoCodec: string bitrate: number stats: GrabStats encoder: 'auto' | 'compatible' hardware: string keyInterval: number } export function exportSupport() { return { video: typeof window !== 'undefined' && 'VideoEncoder' in window, audio: typeof window !== 'undefined' && 'AudioEncoder' in window, } } // H.264 levels: [level_idc, MaxFS (macroblocks), MaxMBPS (macroblocks / s), MaxBR kbit/s (Main; High = ×1.25)] const AVC_LEVELS: [number, number, number, number][] = [ [0x1f, 3600, 108000, 14000], // 3.1 720p30 [0x20, 5120, 216000, 20000], // 3.2 720p60 [0x28, 8192, 245760, 20000], // 4.0 1080p30 [0x29, 8192, 245760, 50000], // 4.1 [0x2a, 8704, 522240, 50000], // 4.2 1080p60 [0x32, 22080, 589824, 135000], // 5.0 1440p30 [0x33, 36864, 983040, 240000], // 5.1 1440p60 / 2160p30 [0x34, 36864, 2073600, 240000], // 5.2 2160p60 ] /** The lowest H.264 level that can carry w × h @ fps at the given bitrate. */ export function avcLevelFor(w: number, h: number, fps: number, bitrate: number) { const mbs = Math.ceil(w / 16) * Math.ceil(h / 16) const mbps = mbs * fps for (const [idc, maxFS, maxMBPS, maxBR] of AVC_LEVELS) { if (mbs <= maxFS && mbps <= maxMBPS && bitrate <= maxBR * 1000 * 1.25) return idc } return 0x34 } export function codecName(cfg: VideoEncoderConfig) { const c = cfg.codec const p = c.slice(5, 7), l = parseInt(c.slice(9, 11), 16) const prof = p === '64' ? 'High' : p === '4D' ? 'Main' : 'Baseline' return `H.264 ${prof} @ L${(l / 10).toFixed(1)}` } async function pickVideoCodec(w: number, h: number, fps: number, bitrate: number, mode: 'auto' | 'compatible') { const minIdc = avcLevelFor(w, h, fps, bitrate) const levels = AVC_LEVELS.map((l) => l[0]).filter((l) => l >= minIdc) // High → Main → Constrained Baseline; at each profile, the lowest level first const profiles = ['6400', '4D40', '42E0'] const hwOrder = mode === 'compatible' ? (['prefer-software', 'no-preference', 'prefer-hardware'] as const) : (['prefer-hardware', 'no-preference'] as const) let lastErr: unknown = null for (const hw of hwOrder) { for (const prof of profiles) { for (const lvl of levels) { const codec = `avc1.${prof}${lvl.toString(16).padStart(2, '0').toUpperCase()}` const cfg: VideoEncoderConfig = { codec, width: w, height: h, bitrate, framerate: fps, hardwareAcceleration: hw, avc: { format: 'avc' }, latencyMode: mode === 'compatible' ? 'realtime' : 'quality', bitrateMode: 'variable' } try { const s = await VideoEncoder.isConfigSupported(cfg) if (s.supported) return cfg } catch (e) { lastErr = e } } } } throw new Error('当前浏览器不支持 H.264 编码,请使用最新版 Chrome / Edge / Safari' + (lastErr ? `(${(lastErr as Error).message})` : '')) } async function pickAudioCodec() { if (!('AudioEncoder' in window)) return null for (const [codec, mux] of [['mp4a.40.2', 'aac'], ['opus', 'opus']] as const) { const cfg: AudioEncoderConfig = { codec, sampleRate: 48000, numberOfChannels: 2, bitrate: 256000 } try { const s = await AudioEncoder.isConfigSupported(cfg) if (s.supported) return { cfg, mux } } catch { /* next */ } } return null } /** Target bitrate: generous, because film grain, mist and water glitter are expensive to encode. */ export function suggestedBitrate(w: number, h: number, fps: number) { const px = w * h const base = px >= 2560 * 1440 * 0.9 ? 40e6 : px >= 1920 * 1080 * 0.9 ? 24e6 : 12e6 return Math.round(base * (fps >= 50 ? 1.5 : fps <= 24 ? 0.9 : 1)) } export async function exportMp4(film: Film, o: ExportOptions): Promise { const { width: W, height: H, fps } = o const start = o.start ?? 0 const dur = o.duration ?? DURATION const total = Math.round(dur * fps) const bitrate = suggestedBitrate(W, H, fps) const mode = o.encoder ?? 'auto' const vcfg = await pickVideoCodec(W, H, fps, bitrate, mode) const keyInterval = mode === 'compatible' ? 10 : 2 const acodec = o.audio ? await pickAudioCodec() : null const target = new ArrayBufferTarget() const muxer = new Muxer({ target, video: { codec: 'avc', width: W, height: H, frameRate: fps }, ...(acodec ? { audio: { codec: acodec.mux, sampleRate: 48000, numberOfChannels: 2 } } : {}), fastStart: 'in-memory', firstTimestampBehavior: 'offset', }) let encErr: unknown = null // ---- audio first (fast) if (acodec && o.audio) { o.onProgress(0, { frame: 0, total, eta: 0, stage: '编码音轨' }) const ae = new AudioEncoder({ output: (c, m) => muxer.addAudioChunk(c, m), error: (e) => { encErr = e } }) ae.configure(acodec.cfg) const buf = o.audio const L = buf.getChannelData(0), R = buf.numberOfChannels > 1 ? buf.getChannelData(1) : L const s0 = Math.floor(start * 48000) const len = Math.min(L.length - s0, Math.round(dur * 48000)) const block = 4800 for (let i = 0; i < len; i += block) { const n = Math.min(block, len - i) const data = new Float32Array(n * 2) data.set(L.subarray(s0 + i, s0 + i + n), 0) data.set(R.subarray(s0 + i, s0 + i + n), n) const ad = new AudioData({ format: 'f32-planar', sampleRate: 48000, numberOfFrames: n, numberOfChannels: 2, timestamp: Math.round((i / 48000) * 1e6), data }) ae.encode(ad) ad.close() if (ae.encodeQueueSize > 20) await yieldToBrowser() } await ae.flush() ae.close() } // ---- video const ve = new VideoEncoder({ output: (c, m) => muxer.addVideoChunk(c, m), error: (e) => { encErr = e } }) ve.configure(vcfg) const grabber = createGrabber(film, W, H, { sharpen: 0.18 }) const { comp, g } = grabber const t0 = performance.now() try { // probe frame: make sure the whole chain (render → copy → verify) works before we start o.onProgress(0, { frame: 0, total, eta: 0, stage: '检查渲染链路' }) await grabber.grab(start + Math.min(dur, 3) * 0.5) for (let i = 0; i < total; i++) { if (o.signal.cancelled) throw new Error('已取消') if (encErr) throw encErr const t = start + i / fps await grabber.grab(t) drawOverlay(g, t, W, H, o.overlay) const frame = new VideoFrame(comp, { timestamp: Math.round((i * 1e6) / fps), duration: Math.round(1e6 / fps) }) ve.encode(frame, { keyFrame: i % (fps * keyInterval) === 0 }) frame.close() while (ve.encodeQueueSize > 3) { await yieldToBrowser(); if (ve.encodeQueueSize > 3) await new Promise((r) => setTimeout(r, 2)) } if (i % 3 === 0) { const el = (performance.now() - t0) / 1000 const note = grabber.stats.retries ? `(已自动修复 ${grabber.stats.retries} 帧)` : '' o.onProgress((i + 1) / total, { frame: i + 1, total, eta: (el / (i + 1)) * (total - i - 1), stage: `渲染画面 ${grabber.stats.ss}×超采样${note}` }) await yieldToBrowser() } } o.onProgress(1, { frame: total, total, eta: 0, stage: '封装 MP4' }) await ve.flush() ve.close() if (encErr) throw encErr muxer.finalize() } finally { grabber.release() if (ve.state !== 'closed') try { ve.close() } catch { /* ignore */ } } return { blob: new Blob([target.buffer], { type: 'video/mp4' }), audioCodec: acodec?.mux ?? null, videoCodec: codecName(vcfg), bitrate, stats: grabber.stats, encoder: mode, hardware: vcfg.hardwareAcceleration === 'prefer-software' ? '软件编码' : vcfg.hardwareAcceleration === 'prefer-hardware' ? '硬件编码' : '自动', keyInterval, } }