// 导出面板:画幅 / 分辨率 / 帧率 / 背景音乐 import { useEffect, useRef, useState } from 'react' import { exportMp4, pickSaveFile, ExportOpts, ExportProgress, RES, Res, Fps, dims, estimateMB, canStreamToDisk, webCodecsOK, fileName } from '../video/export' import { DURATION } from '../video/timeline' import { Chip, Switch, cx, fmtTime } from './ui' export default function ExportPanel({ orient }: { orient: 'l' | 'p' }) { const [o, setO] = useState({ orient, res: '1080', fps: 30, music: true, toDisk: false }) const [prog, setProg] = useState(null) const [busy, setBusy] = useState(false) const [err, setErr] = useState(null) const [result, setResult] = useState<{ url: string | null; name: string; size: number; codec: string; disk: boolean } | null>(null) const abortRef = useRef(null) const [supported, setSupported] = useState(true) const [disk, setDisk] = useState(false) useEffect(() => { setSupported(webCodecsOK()) setDisk(canStreamToDisk()) }, []) useEffect(() => setO((x) => ({ ...x, orient })), [orient]) const est = estimateMB(o) useEffect(() => { if (disk) setO((x) => ({ ...x, toDisk: estimateMB(x) > 450 })) }, [o.res, o.fps, disk]) useEffect(() => () => { if (result?.url) URL.revokeObjectURL(result.url) }, [result]) const start = async () => { setErr(null) let fw: FileSystemWritableFileStream | undefined if (o.toDisk && disk) { try { fw = await pickSaveFile(o) } catch { return // 用户取消了保存对话框 } } if (result?.url) URL.revokeObjectURL(result.url) setResult(null) setBusy(true) const ac = new AbortController() abortRef.current = ac try { const r = await exportMp4(o, setProg, ac.signal, fw) const name = fileName(o) if (r.blob) { const url = URL.createObjectURL(r.blob) setResult({ url, name, size: r.blob.size, codec: r.codec, disk: false }) const a = document.createElement('a') a.href = url a.download = name document.body.appendChild(a) a.click() a.remove() } else setResult({ url: null, name, size: 0, codec: r.codec, disk: true }) } catch (e) { const m = e instanceof Error ? e.message : String(e) if ((e as DOMException)?.name === 'AbortError') setErr('导出已取消') else setErr(m) } finally { setBusy(false) abortRef.current = null } } const { W, H } = dims(o) const pct = prog ? Math.round(prog.p * 1000) / 10 : 0 return (
setO({ ...o, orient: 'l' })} disabled={busy}>横屏 16:9 setO({ ...o, orient: 'p' })} disabled={busy}>竖屏 9:16(手机) {(['1440', '1080', '720'] as Res[]).map((r) => ( setO({ ...o, res: r })} disabled={busy}>{RES[r].label} ))} {([24, 30, 60] as Fps[]).map((f) => ( setO({ ...o, fps: f })} disabled={busy}>{f} fps ))} !busy && setO({ ...o, music: v })} label={o.music ? '导出背景音乐' : '不导出背景音乐(保留旁白与音效)'} /> {disk && ( !busy && setO({ ...o, toDisk: v })} label={o.toDisk ? '边渲染边写入磁盘(大文件推荐)' : '渲染完成后下载'} /> )}
视频在你的浏览器里逐帧渲染并编码成 MP4,不上传任何服务器。导出时请保持本页面在前台。2K / 60 帧画面更细腻,但耗时更长、文件更大。
{!supported &&
当前浏览器不支持本地视频编码(WebCodecs)。请使用最新版 Chrome 或 Edge 电脑浏览器导出。
} {busy || prog?.phase === 'done' ? (
{prog?.phase === 'prepare' ? '准备素材与编码器…' : prog?.phase === 'finalize' ? '正在封装 MP4…' : prog?.phase === 'done' ? '导出完成' : '正在渲染'}
{pct.toFixed(1)}%
{prog && prog.phase === 'render' && (
帧 {prog.frame}/{prog.frames}
速度 {prog.fpsNow.toFixed(1)} 帧/秒
剩余约 {fmtTime(prog.eta)}
)} {prog?.codec &&
编码:{prog.codec}
}
) : null} {err &&
{err}
} {result && (
{result.disk ? '已保存到你选择的位置' : '已生成,下载已开始'}
{result.name}{result.size ? ` · ${(result.size / 1024 / 1024).toFixed(1)} MB` : ''} · {result.codec}
{result.url && ( 再次下载 )}
)}
{!busy ? ( ) : ( )}
) } function Row({ label, children }: { label: string; children: React.ReactNode }) { return (
{label}
{children}
) } function Stat({ k, v }: { k: string; v: string }) { return (
{k}
{v}
) }