Spaces:
Paused
Paused
步骤4:浏览器本地导出 MP4(24/30/60fps,横屏/竖屏,1080P/720P)
Browse files- README.md +2 -1
- src/mv/export/exportMp4.ts +229 -0
README.md
CHANGED
|
@@ -152,7 +152,7 @@ pinned: false
|
|
| 152 |
- [x] 步骤 1:克隆仓库,下载 LFS 素材,抽帧,分析画面和音乐 → 写好本 README → **推送**
|
| 153 |
- [x] 步骤 2:删除旧代码,搭好新项目骨架(Vite + React + TS + Dockerfile + 静态服务器 + 音频特征数据)→ **推送**
|
| 154 |
- [x] 步骤 3:渲染引擎 + 全部 9 个场景 + WebGL 后期 + 播放器(横屏 / 竖屏)→ **推送**
|
| 155 |
-
- [
|
| 156 |
- [ ] 步骤 5:平台文案(小红书 / 抖音 / 视频号 / B 站)+ 多比例多款封面,可多选下载 → **推送**
|
| 157 |
- [ ] 步骤 6:整体打磨、移动端测试、构建验证、Space 运行检查 → **最终推送**
|
| 158 |
|
|
@@ -163,3 +163,4 @@ pinned: false
|
|
| 163 |
- **步骤 1** ✅ 素材下载与分析完成(211 张截图、调色板 / 亮度 / 平涂率统计、BPM 105.5、Drop 位置 38.2s / 109.4s),计划已写入 README。
|
| 164 |
- **步骤 2** ✅ 删除旧 `Dockerfile`(kvideo 镜像),新建 Vite + React + TS 项目骨架:`package.json`、`vite.config.ts`、`index.html`、零依赖静态服务器 `server.mjs`(支持 Range,音频可拖动)、新 `Dockerfile`;加入 60Hz 音频特征数据、字体子集(ZCOOL 庆科黄油体 / 思源黑体 Black / Anton,共约 196KB)。
|
| 165 |
- **步骤 3** ✅ 渲染引擎完成:9 个场景(霓虹点亮 / 雨夜长街 / 隧道 / Drop1 四种快切镜头 / 红月天台 / 葱姜蒜夜市 / 心跳暗场 / 飙车光轨 / 粒子汇聚终章)、程序化「夜行者」角色、雨丝与积水倒影、WebGL 后期(辉光 / 色差 / 故障 / 青橙调色 / 颗粒 / 扫描线 / 暗角)、原创字幕;播放器支持横屏 16:9 / 竖屏 9:16、拖动进度、分镜跳转、全屏。已用无头浏览器逐场景截图检查亮度与构图。
|
|
|
|
|
|
| 152 |
- [x] 步骤 1:克隆仓库,下载 LFS 素材,抽帧,分析画面和音乐 → 写好本 README → **推送**
|
| 153 |
- [x] 步骤 2:删除旧代码,搭好新项目骨架(Vite + React + TS + Dockerfile + 静态服务器 + 音频特征数据)→ **推送**
|
| 154 |
- [x] 步骤 3:渲染引擎 + 全部 9 个场景 + WebGL 后期 + 播放器(横屏 / 竖屏)→ **推送**
|
| 155 |
+
- [x] 步骤 4:浏览器本地 MP4 导出(24/30/60fps,横 / 竖屏,多分辨率)→ **推送**
|
| 156 |
- [ ] 步骤 5:平台文案(小红书 / 抖音 / 视频号 / B 站)+ 多比例多款封面,可多选下载 → **推送**
|
| 157 |
- [ ] 步骤 6:整体打磨、移动端测试、构建验证、Space 运行检查 → **最终推送**
|
| 158 |
|
|
|
|
| 163 |
- **步骤 1** ✅ 素材下载与分析完成(211 张截图、调色板 / 亮度 / 平涂率统计、BPM 105.5、Drop 位置 38.2s / 109.4s),计划已写入 README。
|
| 164 |
- **步骤 2** ✅ 删除旧 `Dockerfile`(kvideo 镜像),新建 Vite + React + TS 项目骨架:`package.json`、`vite.config.ts`、`index.html`、零依赖静态服务器 `server.mjs`(支持 Range,音频可拖动)、新 `Dockerfile`;加入 60Hz 音频特征数据、字体子集(ZCOOL 庆科黄油体 / 思源黑体 Black / Anton,共约 196KB)。
|
| 165 |
- **步骤 3** ✅ 渲染引擎完成:9 个场景(霓虹点亮 / 雨夜长街 / 隧道 / Drop1 四种快切镜头 / 红月天台 / 葱姜蒜夜市 / 心跳暗场 / 飙车光轨 / 粒子汇聚终章)、程序化「夜行者」角色、雨丝与积水倒影、WebGL 后期(辉光 / 色差 / 故障 / 青橙调色 / 颗粒 / 扫描线 / 暗角)、原创字幕;播放器支持横屏 16:9 / 竖屏 9:16、拖动进度、分镜跳转、全屏。已用无头浏览器逐场景截图检查亮度与构图。
|
| 166 |
+
- **步骤 4** ✅ 浏览器本地 MP4 导出:WebCodecs H.264(自动挑选可用的 profile/level)+ AAC(不支持时自动改用 Opus)+ mp4-muxer 封装;横屏 / 竖屏 × 1080P / 720P × **24 / 30 / 60 fps**;带进度、剩余时间、取消;不支持 WebCodecs 的浏览器自动退回实时录制。实测导出 2:24、24fps 的 H.264 MP4,ffprobe 校验通过。
|
src/mv/export/exportMp4.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ArrayBufferTarget, Muxer } from 'mp4-muxer'
|
| 2 |
+
import { DURATION } from '../engine/audio'
|
| 3 |
+
import { Renderer } from '../engine/renderer'
|
| 4 |
+
import { AUDIO_URL } from '../fonts'
|
| 5 |
+
|
| 6 |
+
export interface ExportOptions {
|
| 7 |
+
width: number
|
| 8 |
+
height: number
|
| 9 |
+
fps: 24 | 30 | 60
|
| 10 |
+
signal: AbortSignal
|
| 11 |
+
onProgress: (p: { stage: string; progress: number; frame?: number; total?: number; eta?: number }) => void
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export interface ExportResult {
|
| 15 |
+
blob: Blob
|
| 16 |
+
mime: string
|
| 17 |
+
ext: 'mp4' | 'webm'
|
| 18 |
+
note?: string
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
export function webCodecsSupported(): boolean {
|
| 22 |
+
return typeof window !== 'undefined' && 'VideoEncoder' in window && 'VideoFrame' in window
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
async function pickVideoCodec(width: number, height: number, fps: number, bitrate: number): Promise<string | null> {
|
| 26 |
+
const big = width * height > 1280 * 720
|
| 27 |
+
const candidates =
|
| 28 |
+
fps > 30
|
| 29 |
+
? big
|
| 30 |
+
? ['avc1.640033', 'avc1.64002A', 'avc1.4D402A', 'avc1.42E033', 'avc1.42002A']
|
| 31 |
+
: ['avc1.640020', 'avc1.64002A', 'avc1.4D4020', 'avc1.42E020', 'avc1.42E01F']
|
| 32 |
+
: big
|
| 33 |
+
? ['avc1.640028', 'avc1.64002A', 'avc1.4D4028', 'avc1.42E028', 'avc1.42002A']
|
| 34 |
+
: ['avc1.64001F', 'avc1.640028', 'avc1.4D401F', 'avc1.42E01F']
|
| 35 |
+
for (const codec of candidates) {
|
| 36 |
+
for (const hardwareAcceleration of ['prefer-hardware', 'no-preference'] as const) {
|
| 37 |
+
try {
|
| 38 |
+
const r = await VideoEncoder.isConfigSupported({ codec, width, height, bitrate, framerate: fps, hardwareAcceleration })
|
| 39 |
+
if (r.supported) return codec
|
| 40 |
+
} catch {
|
| 41 |
+
/* try next */
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
return null
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
async function decodeAudio(): Promise<AudioBuffer> {
|
| 49 |
+
const res = await fetch(AUDIO_URL)
|
| 50 |
+
const buf = await res.arrayBuffer()
|
| 51 |
+
const AC: typeof AudioContext = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext
|
| 52 |
+
const ac = new AC()
|
| 53 |
+
try {
|
| 54 |
+
return await new Promise<AudioBuffer>((resolve, reject) => {
|
| 55 |
+
const p = ac.decodeAudioData(buf, resolve, reject)
|
| 56 |
+
if (p && typeof p.then === 'function') p.then(resolve, reject)
|
| 57 |
+
})
|
| 58 |
+
} finally {
|
| 59 |
+
ac.close().catch(() => {})
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
const tick = () => new Promise<void>((r) => setTimeout(r, 0))
|
| 64 |
+
|
| 65 |
+
export async function exportMp4(o: ExportOptions): Promise<ExportResult> {
|
| 66 |
+
if (!webCodecsSupported()) return exportRealtime(o)
|
| 67 |
+
const { width, height, fps, signal, onProgress } = o
|
| 68 |
+
const bitrate = Math.round(width * height * fps * (fps > 30 ? 0.12 : 0.17))
|
| 69 |
+
onProgress({ stage: '检测编码器', progress: 0 })
|
| 70 |
+
const codec = await pickVideoCodec(width, height, fps, bitrate)
|
| 71 |
+
if (!codec) return exportRealtime(o)
|
| 72 |
+
|
| 73 |
+
// ---- audio
|
| 74 |
+
onProgress({ stage: '解码音乐', progress: 0.01 })
|
| 75 |
+
let audioBuf: AudioBuffer | null = null
|
| 76 |
+
try {
|
| 77 |
+
audioBuf = await decodeAudio()
|
| 78 |
+
} catch (e) {
|
| 79 |
+
console.warn('audio decode failed', e)
|
| 80 |
+
}
|
| 81 |
+
let audioCodec: 'aac' | 'opus' | null = null
|
| 82 |
+
let audioCodecStr = ''
|
| 83 |
+
const sampleRate = audioBuf?.sampleRate ?? 44100
|
| 84 |
+
const channels = Math.min(2, audioBuf?.numberOfChannels ?? 2)
|
| 85 |
+
if (audioBuf && 'AudioEncoder' in window) {
|
| 86 |
+
for (const [c, s] of [
|
| 87 |
+
['aac', 'mp4a.40.2'],
|
| 88 |
+
['opus', 'opus'],
|
| 89 |
+
] as const) {
|
| 90 |
+
try {
|
| 91 |
+
const r = await AudioEncoder.isConfigSupported({ codec: s, sampleRate, numberOfChannels: channels, bitrate: 192000 })
|
| 92 |
+
if (r.supported) {
|
| 93 |
+
audioCodec = c
|
| 94 |
+
audioCodecStr = s
|
| 95 |
+
break
|
| 96 |
+
}
|
| 97 |
+
} catch {
|
| 98 |
+
/* next */
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
const target = new ArrayBufferTarget()
|
| 104 |
+
const muxer = new Muxer({
|
| 105 |
+
target,
|
| 106 |
+
video: { codec: 'avc', width, height, frameRate: fps },
|
| 107 |
+
audio: audioCodec ? { codec: audioCodec, numberOfChannels: channels, sampleRate } : undefined,
|
| 108 |
+
fastStart: 'in-memory',
|
| 109 |
+
firstTimestampBehavior: 'offset',
|
| 110 |
+
})
|
| 111 |
+
|
| 112 |
+
let failure: Error | null = null
|
| 113 |
+
if (audioBuf && audioCodec) {
|
| 114 |
+
onProgress({ stage: '编码音频', progress: 0.02 })
|
| 115 |
+
const ae = new AudioEncoder({
|
| 116 |
+
output: (chunk, meta) => muxer.addAudioChunk(chunk, meta),
|
| 117 |
+
error: (e) => (failure = e as Error),
|
| 118 |
+
})
|
| 119 |
+
ae.configure({ codec: audioCodecStr, sampleRate, numberOfChannels: channels, bitrate: 192000 })
|
| 120 |
+
const total = Math.min(audioBuf.length, Math.floor(DURATION * sampleRate))
|
| 121 |
+
const block = 4096
|
| 122 |
+
const chData = Array.from({ length: channels }, (_, c) => audioBuf!.getChannelData(Math.min(c, audioBuf!.numberOfChannels - 1)))
|
| 123 |
+
for (let i = 0; i < total; i += block) {
|
| 124 |
+
if (signal.aborted) throw new DOMException('aborted', 'AbortError')
|
| 125 |
+
const n = Math.min(block, total - i)
|
| 126 |
+
const data = new Float32Array(n * channels)
|
| 127 |
+
for (let c = 0; c < channels; c++) data.set(chData[c].subarray(i, i + n), c * n)
|
| 128 |
+
const ad = new AudioData({ format: 'f32-planar', sampleRate, numberOfFrames: n, numberOfChannels: channels, timestamp: Math.round((i / sampleRate) * 1e6), data })
|
| 129 |
+
ae.encode(ad)
|
| 130 |
+
ad.close()
|
| 131 |
+
if (ae.encodeQueueSize > 32) await tick()
|
| 132 |
+
}
|
| 133 |
+
await ae.flush()
|
| 134 |
+
ae.close()
|
| 135 |
+
}
|
| 136 |
+
if (failure) throw failure
|
| 137 |
+
|
| 138 |
+
// ---- video
|
| 139 |
+
const ve = new VideoEncoder({
|
| 140 |
+
output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
|
| 141 |
+
error: (e) => (failure = e as Error),
|
| 142 |
+
})
|
| 143 |
+
ve.configure({ codec, width, height, bitrate, framerate: fps, latencyMode: 'quality', avc: { format: 'avc' } })
|
| 144 |
+
const r = new Renderer()
|
| 145 |
+
r.setSize(width, height)
|
| 146 |
+
const total = Math.ceil(DURATION * fps)
|
| 147 |
+
const t0 = performance.now()
|
| 148 |
+
for (let i = 0; i < total; i++) {
|
| 149 |
+
if (signal.aborted) {
|
| 150 |
+
ve.close()
|
| 151 |
+
throw new DOMException('aborted', 'AbortError')
|
| 152 |
+
}
|
| 153 |
+
if (failure) throw failure
|
| 154 |
+
r.render(i / fps)
|
| 155 |
+
const frame = new VideoFrame(r.out, { timestamp: Math.round((i * 1e6) / fps), duration: Math.round(1e6 / fps) })
|
| 156 |
+
ve.encode(frame, { keyFrame: i % (fps * 2) === 0 })
|
| 157 |
+
frame.close()
|
| 158 |
+
while (ve.encodeQueueSize > 4) await new Promise((res) => setTimeout(res, 1))
|
| 159 |
+
if (i % 3 === 0) {
|
| 160 |
+
const el = (performance.now() - t0) / 1000
|
| 161 |
+
const eta = ((total - i - 1) * el) / (i + 1)
|
| 162 |
+
onProgress({ stage: '渲染画面', progress: 0.03 + 0.95 * (i / total), frame: i + 1, total, eta })
|
| 163 |
+
await tick()
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
await ve.flush()
|
| 167 |
+
ve.close()
|
| 168 |
+
if (failure) throw failure
|
| 169 |
+
onProgress({ stage: '封装 MP4', progress: 0.99 })
|
| 170 |
+
muxer.finalize()
|
| 171 |
+
const blob = new Blob([target.buffer], { type: 'video/mp4' })
|
| 172 |
+
onProgress({ stage: '完成', progress: 1 })
|
| 173 |
+
return { blob, mime: 'video/mp4', ext: 'mp4', note: audioCodec ? undefined : '当前浏览器不支持音频编码,导出的视频没有声音' }
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
/** Fallback for browsers without WebCodecs: realtime capture with MediaRecorder. */
|
| 177 |
+
async function exportRealtime(o: ExportOptions): Promise<ExportResult> {
|
| 178 |
+
const { width, height, fps, signal, onProgress } = o
|
| 179 |
+
if (typeof MediaRecorder === 'undefined') throw new Error('当前浏览器不支持本地视频编码,请使用最新版 Chrome / Edge / Safari')
|
| 180 |
+
const mimes = ['video/mp4;codecs=avc1.640028,mp4a.40.2', 'video/mp4;codecs=avc1,mp4a', 'video/mp4', 'video/webm;codecs=vp9,opus', 'video/webm']
|
| 181 |
+
const mime = mimes.find((m) => MediaRecorder.isTypeSupported(m)) || ''
|
| 182 |
+
const r = new Renderer()
|
| 183 |
+
r.setSize(width, height)
|
| 184 |
+
const AC: typeof AudioContext = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext
|
| 185 |
+
const ac = new AC()
|
| 186 |
+
const audioBuf = await decodeAudio()
|
| 187 |
+
const src = ac.createBufferSource()
|
| 188 |
+
src.buffer = audioBuf
|
| 189 |
+
const dest = ac.createMediaStreamDestination()
|
| 190 |
+
src.connect(dest)
|
| 191 |
+
const stream = (r.out as HTMLCanvasElement & { captureStream(fps?: number): MediaStream }).captureStream(fps)
|
| 192 |
+
dest.stream.getAudioTracks().forEach((tr) => stream.addTrack(tr))
|
| 193 |
+
const rec = new MediaRecorder(stream, mime ? { mimeType: mime, videoBitsPerSecond: 12_000_000 } : undefined)
|
| 194 |
+
const chunks: Blob[] = []
|
| 195 |
+
rec.ondataavailable = (e) => e.data.size && chunks.push(e.data)
|
| 196 |
+
const done = new Promise<void>((res) => (rec.onstop = () => res()))
|
| 197 |
+
rec.start(1000)
|
| 198 |
+
const start = ac.currentTime + 0.1
|
| 199 |
+
src.start(start)
|
| 200 |
+
await new Promise<void>((resolve, reject) => {
|
| 201 |
+
const loop = () => {
|
| 202 |
+
if (signal.aborted) {
|
| 203 |
+
rec.stop()
|
| 204 |
+
src.stop()
|
| 205 |
+
reject(new DOMException('aborted', 'AbortError'))
|
| 206 |
+
return
|
| 207 |
+
}
|
| 208 |
+
const t = ac.currentTime - start
|
| 209 |
+
r.render(Math.max(0, t))
|
| 210 |
+
onProgress({ stage: '实时录制(浏览器不支持快速编码)', progress: Math.min(1, t / DURATION), eta: DURATION - t })
|
| 211 |
+
if (t >= DURATION) {
|
| 212 |
+
rec.stop()
|
| 213 |
+
resolve()
|
| 214 |
+
return
|
| 215 |
+
}
|
| 216 |
+
requestAnimationFrame(loop)
|
| 217 |
+
}
|
| 218 |
+
loop()
|
| 219 |
+
})
|
| 220 |
+
await done
|
| 221 |
+
ac.close().catch(() => {})
|
| 222 |
+
const isMp4 = (mime || rec.mimeType).includes('mp4')
|
| 223 |
+
return {
|
| 224 |
+
blob: new Blob(chunks, { type: isMp4 ? 'video/mp4' : 'video/webm' }),
|
| 225 |
+
mime: isMp4 ? 'video/mp4' : 'video/webm',
|
| 226 |
+
ext: isMp4 ? 'mp4' : 'webm',
|
| 227 |
+
note: isMp4 ? '使用实时录制模式导出' : '当前浏览器只能录制 WebM,建议用 Chrome / Edge / Safari 导出 MP4',
|
| 228 |
+
}
|
| 229 |
+
}
|