File size: 6,361 Bytes
6fff8e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 浏览器本地渲染导出 MP4:逐帧离屏渲染 → WebCodecs 编码 → mediabunny 封装
import {
  Output, Mp4OutputFormat, BufferTarget, StreamTarget, CanvasSource, AudioBufferSource, Quality,
  getFirstEncodableVideoCodec, getFirstEncodableAudioCodec, type VideoCodec, type AudioCodec, type StreamTargetChunk,
} from 'mediabunny'
import { Renderer } from './render'
import { DURATION } from './timeline'
import { loadFonts } from './fonts'
import { loadVoices, renderSegment, SEG, SEG_COUNT, SR } from './audio'
import { makeCanvas, ctx2d } from './util'

export type Res = '1440' | '1080' | '720'
export type Fps = 24 | 30 | 60
export interface ExportOpts {
  orient: 'l' | 'p'
  res: Res
  fps: Fps
  music: boolean
  toDisk: boolean
  /** 仅用于自检:只导出前 N 秒 */
  limit?: number
}
export interface ExportProgress {
  phase: 'prepare' | 'render' | 'finalize' | 'done'
  p: number
  frame: number
  frames: number
  eta: number
  fpsNow: number
  codec?: string
}
export const RES: Record<Res, { label: string; w: number; h: number }> = {
  '1440': { label: '2K', w: 2560, h: 1440 },
  '1080': { label: '1080P', w: 1920, h: 1080 },
  '720': { label: '720P', w: 1280, h: 720 },
}
const BITRATE: Record<Res, Record<Fps, number>> = {
  '720': { 24: 2.4e6, 30: 2.8e6, 60: 4.2e6 },
  '1080': { 24: 4.8e6, 30: 5.6e6, 60: 8.4e6 },
  '1440': { 24: 7.6e6, 30: 9e6, 60: 13e6 },
}
export function dims(o: Pick<ExportOpts, 'orient' | 'res'>) {
  const r = RES[o.res]
  return o.orient === 'l' ? { W: r.w, H: r.h } : { W: r.h, H: r.w }
}
export function estimateMB(o: Pick<ExportOpts, 'res' | 'fps'>) {
  return ((BITRATE[o.res][o.fps] + 160000) * DURATION) / 8 / 1024 / 1024
}
export const canStreamToDisk = () => typeof window !== 'undefined' && 'showSaveFilePicker' in window
export const webCodecsOK = () => typeof window !== 'undefined' && 'VideoEncoder' in window && 'AudioEncoder' in window

export function fileName(o: ExportOpts) {
  return `登泰山记_五个镜头_${o.orient === 'l' ? '横屏' : '竖屏'}_${RES[o.res].label}_${o.fps}fps${o.music ? '' : '_无背景音乐'}.mp4`
}

/** 必须在用户点击的同步阶段调用(浏览器要求用户手势) */
export async function pickSaveFile(o: ExportOpts): Promise<FileSystemWritableFileStream> {
  const handle = await (window as unknown as { showSaveFilePicker: (o: unknown) => Promise<FileSystemFileHandle> }).showSaveFilePicker({
    suggestedName: fileName(o),
    types: [{ description: 'MP4 视频', accept: { 'video/mp4': ['.mp4'] } }],
  })
  return handle.createWritable()
}

export async function exportMp4(o: ExportOpts, onProgress: (p: ExportProgress) => void, signal: AbortSignal, fileWritable?: FileSystemWritableFileStream): Promise<{ blob: Blob | null; codec: string }> {
  if (!webCodecsOK()) throw new Error('当前浏览器不支持 WebCodecs,请使用最新版 Chrome / Edge 桌面浏览器导出。')
  const { W, H } = dims(o)
  const bitrate = BITRATE[o.res][o.fps]
  onProgress({ phase: 'prepare', p: 0, frame: 0, frames: 0, eta: 0, fpsNow: 0 })
  await loadFonts()
  await loadVoices()
  const vCodec = (await getFirstEncodableVideoCodec(['avc', 'hevc', 'vp9', 'av1'] as VideoCodec[], { width: W, height: H, bitrate, frameRate: o.fps })) as VideoCodec | null
  if (!vCodec) throw new Error(`当前浏览器无法以 ${W}×${H} 编码视频,请尝试更低分辨率。`)
  const aCodec = (await getFirstEncodableAudioCodec(['aac', 'opus'] as AudioCodec[], { numberOfChannels: 2, sampleRate: SR, bitrate: 192000 })) as AudioCodec | null
  // 目标:优先流式写入磁盘(大文件不占内存)
  let target: BufferTarget | StreamTarget
  const writable: FileSystemWritableFileStream | null = fileWritable ?? null
  if (writable) target = new StreamTarget(writable as unknown as WritableStream<StreamTargetChunk>, { chunked: true })
  else target = new BufferTarget()
  const output = new Output({ format: new Mp4OutputFormat({ fastStart: writable ? false : 'in-memory' }), target })
  const canvas = makeCanvas(W, H)
  const g = ctx2d(canvas)
  const video = new CanvasSource(canvas, { codec: vCodec, quality: new Quality({ bitrate, bitrateMode: 'variable' }), keyFrameInterval: 2 })
  output.addVideoTrack(video, { frameRate: o.fps })
  let audio: AudioBufferSource | null = null
  if (aCodec) {
    audio = new AudioBufferSource({ codec: aCodec, quality: new Quality({ bitrate: 192000 }) })
    output.addAudioTrack(audio)
  }
  await output.start()
  const r = new Renderer(W, H)
  const frames = Math.ceil(Math.min(DURATION, o.limit ?? DURATION) * o.fps)
  let segAdded = 0
  const addAudioUntil = async (t: number) => {
    if (!audio) return
    while (segAdded < SEG_COUNT && segAdded * SEG <= t) {
      const buf = await renderSegment(segAdded, o.music)
      await audio.add(buf)
      segAdded++
    }
  }
  const t0 = performance.now()
  let lastT = t0
  let lastF = 0
  let fpsNow = 0
  const codecLabel = `${vCodec.toUpperCase()} + ${aCodec ? aCodec.toUpperCase() : '无音轨'}`
  try {
    for (let i = 0; i < frames; i++) {
      if (signal.aborted) throw new DOMException('已取消', 'AbortError')
      const t = i / o.fps
      await addAudioUntil(t + 1)
      r.render(g, t)
      await video.add(t, 1 / o.fps)
      if (i % 6 === 0 || i === frames - 1) {
        const now = performance.now()
        if (now - lastT > 600) {
          fpsNow = ((i - lastF) * 1000) / (now - lastT)
          lastT = now
          lastF = i
        }
        const el = (now - t0) / 1000
        const p = (i + 1) / frames
        onProgress({ phase: 'render', p, frame: i + 1, frames, eta: (el / p) * (1 - p), fpsNow, codec: codecLabel })
      }
    }
    await addAudioUntil(o.limit ? Math.min(DURATION, o.limit) - 0.001 : DURATION + SEG)
    onProgress({ phase: 'finalize', p: 1, frame: frames, frames, eta: 0, fpsNow, codec: codecLabel })
    await output.finalize()
  } catch (e) {
    await output.cancel().catch(() => undefined)
    if (writable) await writable.abort().catch(() => undefined)
    r.dispose()
    throw e
  }
  r.dispose()
  onProgress({ phase: 'done', p: 1, frame: frames, frames, eta: 0, fpsNow, codec: codecLabel })
  if (target instanceof BufferTarget && target.buffer) return { blob: new Blob([target.buffer], { type: 'video/mp4' }), codec: codecLabel }
  return { blob: null, codec: codecLabel }
}