video / src /mv /export /exportMp4.ts
Ethscriptions's picture
v7:街景楼群不跳;修正58秒光效;夜市服装与正面摊主;Drop2去货车/加方向盘/正后方起飞/去HUD/灰白冷气/Roadster比例;音频留1dB余量
624ddb0 verified
Raw History Blame Contribute Delete
9.41 kB
import { ArrayBufferTarget, Muxer } from 'mp4-muxer'
import { DURATION } from '../engine/audio'
import { Renderer } from '../engine/renderer'
import { AUDIO_URL } from '../fonts'
export interface ExportOptions {
width: number
height: number
fps: 24 | 30 | 60
signal: AbortSignal
onProgress: (p: { stage: string; progress: number; frame?: number; total?: number; eta?: number }) => void
}
export interface ExportResult {
blob: Blob
mime: string
ext: 'mp4' | 'webm'
note?: string
}
export function webCodecsSupported(): boolean {
return typeof window !== 'undefined' && 'VideoEncoder' in window && 'VideoFrame' in window
}
async function pickVideoCodec(width: number, height: number, fps: number, bitrate: number): Promise<string | null> {
const big = width * height > 1280 * 720
const candidates =
fps > 30
? big
? ['avc1.640033', 'avc1.64002A', 'avc1.4D402A', 'avc1.42E033', 'avc1.42002A']
: ['avc1.640020', 'avc1.64002A', 'avc1.4D4020', 'avc1.42E020', 'avc1.42E01F']
: big
? ['avc1.640028', 'avc1.64002A', 'avc1.4D4028', 'avc1.42E028', 'avc1.42002A']
: ['avc1.64001F', 'avc1.640028', 'avc1.4D401F', 'avc1.42E01F']
for (const codec of candidates) {
for (const hardwareAcceleration of ['prefer-hardware', 'no-preference'] as const) {
try {
const r = await VideoEncoder.isConfigSupported({ codec, width, height, bitrate, framerate: fps, hardwareAcceleration })
if (r.supported) return codec
} catch {
/* try next */
}
}
}
return null
}
async function decodeAudio(): Promise<AudioBuffer> {
const res = await fetch(AUDIO_URL)
const buf = await res.arrayBuffer()
const AC: typeof AudioContext = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext
const ac = new AC()
try {
return await new Promise<AudioBuffer>((resolve, reject) => {
const p = ac.decodeAudioData(buf, resolve, reject)
if (p && typeof p.then === 'function') p.then(resolve, reject)
})
} finally {
ac.close().catch(() => {})
}
}
const tick = () => new Promise<void>((r) => setTimeout(r, 0))
// one WebGL context for all exports (browsers cap the number of live contexts)
let sharedRenderer: Renderer | null = null
export function exportRenderer(): Renderer {
sharedRenderer ??= new Renderer()
return sharedRenderer
}
export async function exportMp4(o: ExportOptions): Promise<ExportResult> {
if (!webCodecsSupported()) return exportRealtime(o)
const { width, height, fps, signal, onProgress } = o
const bitrate = Math.round(width * height * fps * (fps > 30 ? 0.12 : 0.17))
onProgress({ stage: '检测编码器', progress: 0 })
const codec = await pickVideoCodec(width, height, fps, bitrate)
if (!codec) return exportRealtime(o)
// ---- audio
onProgress({ stage: '解码音乐', progress: 0.01 })
let audioBuf: AudioBuffer | null = null
try {
audioBuf = await decodeAudio()
} catch (e) {
console.warn('audio decode failed', e)
}
let audioCodec: 'aac' | 'opus' | null = null
let audioCodecStr = ''
const sampleRate = audioBuf?.sampleRate ?? 44100
const channels = Math.min(2, audioBuf?.numberOfChannels ?? 2)
if (audioBuf && 'AudioEncoder' in window) {
for (const [c, s] of [
['aac', 'mp4a.40.2'],
['opus', 'opus'],
] as const) {
try {
const r = await AudioEncoder.isConfigSupported({ codec: s, sampleRate, numberOfChannels: channels, bitrate: 256000 })
if (r.supported) {
audioCodec = c
audioCodecStr = s
break
}
} catch {
/* next */
}
}
}
const target = new ArrayBufferTarget()
const muxer = new Muxer({
target,
video: { codec: 'avc', width, height, frameRate: fps },
audio: audioCodec ? { codec: audioCodec, numberOfChannels: channels, sampleRate } : undefined,
fastStart: 'in-memory',
firstTimestampBehavior: 'offset',
})
let failure: Error | null = null
if (audioBuf && audioCodec) {
onProgress({ stage: '编码音频', progress: 0.02 })
const ae = new AudioEncoder({
output: (chunk, meta) => muxer.addAudioChunk(chunk, meta),
error: (e) => (failure = e as Error),
})
ae.configure({ codec: audioCodecStr, sampleRate, numberOfChannels: channels, bitrate: 256000 })
const total = Math.min(audioBuf.length, Math.floor(DURATION * sampleRate))
const block = 4096
const chData = Array.from({ length: channels }, (_, c) => audioBuf!.getChannelData(Math.min(c, audioBuf!.numberOfChannels - 1)))
for (let i = 0; i < total; i += block) {
if (signal.aborted) throw new DOMException('aborted', 'AbortError')
const n = Math.min(block, total - i)
const data = new Float32Array(n * channels)
// ~1 dB of headroom: the source master peaks at +0.49 dBFS and would clip in the encoder
for (let c = 0; c < channels; c++) {
const src = chData[c].subarray(i, i + n)
for (let k = 0; k < n; k++) data[c * n + k] = src[k] * 0.89
}
const ad = new AudioData({ format: 'f32-planar', sampleRate, numberOfFrames: n, numberOfChannels: channels, timestamp: Math.round((i / sampleRate) * 1e6), data })
ae.encode(ad)
ad.close()
if (ae.encodeQueueSize > 32) await tick()
}
await ae.flush()
ae.close()
}
if (failure) throw failure
// ---- video
const ve = new VideoEncoder({
output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
error: (e) => (failure = e as Error),
})
ve.configure({ codec, width, height, bitrate, framerate: fps, latencyMode: 'quality', avc: { format: 'avc' } })
const r = exportRenderer()
r.setSize(width, height)
const total = Math.ceil(DURATION * fps)
const t0 = performance.now()
for (let i = 0; i < total; i++) {
if (signal.aborted) {
ve.close()
throw new DOMException('aborted', 'AbortError')
}
if (failure) throw failure
r.render(i / fps)
const frame = new VideoFrame(r.out, { timestamp: Math.round((i * 1e6) / fps), duration: Math.round(1e6 / fps) })
ve.encode(frame, { keyFrame: i % (fps * 2) === 0 })
frame.close()
while (ve.encodeQueueSize > 4) await new Promise((res) => setTimeout(res, 1))
if (i % 3 === 0) {
const el = (performance.now() - t0) / 1000
const eta = ((total - i - 1) * el) / (i + 1)
onProgress({ stage: '渲染画面', progress: 0.03 + 0.95 * (i / total), frame: i + 1, total, eta })
await tick()
}
}
await ve.flush()
ve.close()
if (failure) throw failure
onProgress({ stage: '封装 MP4', progress: 0.99 })
muxer.finalize()
const blob = new Blob([target.buffer], { type: 'video/mp4' })
onProgress({ stage: '完成', progress: 1 })
return { blob, mime: 'video/mp4', ext: 'mp4', note: audioCodec ? undefined : '当前浏览器不支持音频编码,导出的视频没有声音' }
}
/** Fallback for browsers without WebCodecs: realtime capture with MediaRecorder. */
async function exportRealtime(o: ExportOptions): Promise<ExportResult> {
const { width, height, fps, signal, onProgress } = o
if (typeof MediaRecorder === 'undefined') throw new Error('当前浏览器不支持本地视频编码,请使用最新版 Chrome / Edge / Safari')
const mimes = ['video/mp4;codecs=avc1.640028,mp4a.40.2', 'video/mp4;codecs=avc1,mp4a', 'video/mp4', 'video/webm;codecs=vp9,opus', 'video/webm']
const mime = mimes.find((m) => MediaRecorder.isTypeSupported(m)) || ''
const r = exportRenderer()
r.setSize(width, height)
const AC: typeof AudioContext = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext
const ac = new AC()
const audioBuf = await decodeAudio()
const src = ac.createBufferSource()
src.buffer = audioBuf
const dest = ac.createMediaStreamDestination()
src.connect(dest)
const stream = (r.out as HTMLCanvasElement & { captureStream(fps?: number): MediaStream }).captureStream(fps)
dest.stream.getAudioTracks().forEach((tr) => stream.addTrack(tr))
const rec = new MediaRecorder(stream, mime ? { mimeType: mime, videoBitsPerSecond: 12_000_000 } : undefined)
const chunks: Blob[] = []
rec.ondataavailable = (e) => e.data.size && chunks.push(e.data)
const done = new Promise<void>((res) => (rec.onstop = () => res()))
rec.start(1000)
const start = ac.currentTime + 0.1
src.start(start)
await new Promise<void>((resolve, reject) => {
const loop = () => {
if (signal.aborted) {
rec.stop()
src.stop()
reject(new DOMException('aborted', 'AbortError'))
return
}
const t = ac.currentTime - start
r.render(Math.max(0, t))
onProgress({ stage: '实时录制(浏览器不支持快速编码)', progress: Math.min(1, t / DURATION), eta: DURATION - t })
if (t >= DURATION) {
rec.stop()
resolve()
return
}
requestAnimationFrame(loop)
}
loop()
})
await done
ac.close().catch(() => {})
const isMp4 = (mime || rec.mimeType).includes('mp4')
return {
blob: new Blob(chunks, { type: isMp4 ? 'video/mp4' : 'video/webm' }),
mime: isMp4 ? 'video/mp4' : 'video/webm',
ext: isMp4 ? 'mp4' : 'webm',
note: isMp4 ? '使用实时录制模式导出' : '当前浏览器只能录制 WebM,建议用 Chrome / Edge / Safari 导出 MP4',
}
}