Spaces:
Paused
Paused
File size: 9,409 Bytes
230b544 20e791f 230b544 624ddb0 230b544 624ddb0 230b544 624ddb0 230b544 20e791f 230b544 20e791f 230b544 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 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',
}
}
|