Surf commited on
Commit
c23e56f
·
1 Parent(s): 1800a97

修复:长合集MP4按position写入并通过151秒音视频解码回归(#26)

Browse files
README.md CHANGED
@@ -23,11 +23,32 @@ pinned: false
23
  | 同步节点 | 工作 | 状态 |
24
  |---|---|---|
25
  | #25 | 计划与根因记录,先推送 | 已记录 |
26
- | #26 | 位置写入修复、回归与静态产物 | 待完成 |
27
  | #27 | 发布核验与验收记录 | 待完成 |
28
 
29
  **验收边界**:不能只添加第二个参数来绕过校验,必须按 `position` 写入;最终只记录实际完成的测试,不把封装单测等同于全分辨率浏览器端到端验收。
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  ---
32
 
33
  # 奥德彪语录 · 个人 IP 封面升级(v5)
 
23
  | 同步节点 | 工作 | 状态 |
24
  |---|---|---|
25
  | #25 | 计划与根因记录,先推送 | 已记录 |
26
+ | #26 | 位置写入修复、回归与静态产物 | 已完成,见下方实测 |
27
  | #27 | 发布核验与验收记录 | 待完成 |
28
 
29
  **验收边界**:不能只添加第二个参数来绕过校验,必须按 `position` 写入;最终只记录实际完成的测试,不把封装单测等同于全分辨率浏览器端到端验收。
30
 
31
+ ### #26 修复与实测结果
32
+
33
+ - 新增 `src/film/core/mp4-target.ts`。`onData(data, position)` 按绝对字节位置写入 8 MiB 块,支持回填、覆盖、跨块和乱序;输出按文件位置组装,而不是按回调顺序追加。
34
+ - `exporter.ts` 接入新目标工厂;≤150 秒仍用原内存封装,>150 秒仍用分片 MP4,不通过禁用长合集来规避报错。
35
+ - **旧问题复现通过**:原单参数回调稳定抛出用户报告的异常;150 / 150.001 秒分支验证通过。
36
+ - **位置写入回归通过**:乱序、重叠覆盖、空隙补零、输入缓冲区变更、非法偏移、真实 8 MiB 边界。
37
+ - **真实编码样本回归通过**:用 FFmpeg 生成 H.264/AAC 测试样本(160×90、1fps),通过项目实际目标工厂与 MP4 封装器输出以下四个文件。每份输出与相同配置的内存参考封装逐字节一致,FFmpeg 完整解码无错误:
38
+
39
+ | 测试 | 成品大小(字节) | 解码视频帧数 | 解码音频帧数 |
40
+ |---|---:|---:|---:|
41
+ | 2 秒,无音轨 | 8,401 | 2 | 0 |
42
+ | 2 秒,H.264 + AAC | 38,455 | 2 | 87 |
43
+ | 151 秒,无音轨 | 605,313 | 151 | 0 |
44
+ | 151 秒,H.264 + AAC | 2,806,843 | 151 | 6,504 |
45
+
46
+ - 额外以非分片 MP4 强制触发真实封装器的文件头回填、倒序回调,与内存参考输出逐字节一致。
47
+ - `npm run typecheck`、`npm run build`、`git diff --check` 通过;新静态入口 `assets/index-ip-f7c9b6d31c.js`,避免继续命中旧版缓存。
48
+ - 复现命令(Node ≥22.6,需 FFmpeg):`FFMPEG_BIN=/path/to/ffmpeg node --experimental-strip-types pipeline/check-export.mjs`。
49
+ - **测试范围**:上述为真实媒体封装/解码回归,不是本轮对全部语录在所有浏览器、所有分辨率的完整重导出;用户设备的 WebCodecs 支持与内存上限仍取决于浏览器。
50
+
51
+
52
  ---
53
 
54
  # 奥德彪语录 · 个人 IP 封面升级(v5)
assets/index-ip-f7c9b6d31c.js ADDED
The diff for this file is too large to render. See raw diff
 
index.html CHANGED
@@ -12,7 +12,7 @@
12
  document.documentElement.classList.add('dark');
13
  </script>
14
  <link rel="stylesheet" crossorigin href="./assets/index-Ce46jEpY.css">
15
- <script type="module" crossorigin src="./assets/index-ip-ee96b9d3ab.js"></script>
16
  </head>
17
  <body>
18
  <div id="root"><!--ssr-outlet--></div>
 
12
  document.documentElement.classList.add('dark');
13
  </script>
14
  <link rel="stylesheet" crossorigin href="./assets/index-Ce46jEpY.css">
15
+ <script type="module" crossorigin src="./assets/index-ip-f7c9b6d31c.js"></script>
16
  </head>
17
  <body>
18
  <div id="root"><!--ssr-outlet--></div>
pipeline/check-export.mjs ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Node >=22.6: node --experimental-strip-types pipeline/check-export.mjs
2
+ // FFMPEG_BIN may point to a standalone FFmpeg with libx264 + AAC encoding.
3
+ import assert from 'node:assert/strict'
4
+ import { mkdtemp, writeFile, readFile } from 'node:fs/promises'
5
+ import { tmpdir } from 'node:os'
6
+ import { join } from 'node:path'
7
+ import { spawnSync } from 'node:child_process'
8
+ import { Muxer, StreamTarget, ArrayBufferTarget } from 'mp4-muxer'
9
+ import { PositionedBuffer, createMp4Target } from '../src/film/core/mp4-target.ts'
10
+
11
+ const ffmpeg = process.env.FFMPEG_BIN || 'ffmpeg'
12
+ const dir = await mkdtemp(join(tmpdir(), 'odb-mp4-regression-'))
13
+ function ff(args) {
14
+ const p = spawnSync(ffmpeg, ['-v', 'error', '-xerror', '-y', ...args], { encoding: null, maxBuffer: 64 * 1024 * 1024 })
15
+ if (p.error) throw p.error
16
+ assert.equal(p.status, 0, p.stderr?.toString()); return p.stdout
17
+ }
18
+ const bytes = async blob => new Uint8Array(await blob.arrayBuffer())
19
+ assert.throws(() => new StreamTarget({ onData: data => void data }), /at least two arguments/)
20
+ assert(createMp4Target(150).target instanceof ArrayBufferTarget)
21
+ assert(createMp4Target(150.001).target instanceof StreamTarget)
22
+ console.log('PASS: old callback error reproduced; 150s / >150s routing verified')
23
+
24
+ const buffer = new PositionedBuffer(8)
25
+ const reference = new Uint8Array(39)
26
+ for (const [at, list] of [[17,[3,4,5]], [0,[7,8]], [5,[1,2,3,4,5,6,7]], [35,[9,10,11,12]], [7,[99,98,97]], [1,[88]]]) {
27
+ const data = Uint8Array.from(list); buffer.write(data, at); reference.set(data, at); data.fill(0)
28
+ }
29
+ buffer.write(new Uint8Array(), 9999)
30
+ assert.deepEqual(await bytes(buffer.toBlob()), reference)
31
+ assert.throws(() => buffer.write(new Uint8Array([1]), -1), RangeError)
32
+ assert.throws(() => buffer.write(new Uint8Array([1]), 0.5), RangeError)
33
+ assert.throws(() => buffer.write(new Uint8Array([1]), Number.MAX_SAFE_INTEGER), RangeError)
34
+ buffer.dispose(); assert.equal(buffer.toBlob().size, 0)
35
+ const large = new PositionedBuffer(), boundary = 8 * 1024 * 1024
36
+ large.write(new Uint8Array([1,2,3,4]), boundary - 2)
37
+ large.write(new Uint8Array([8,9]), boundary - 1)
38
+ const largeBytes = await bytes(large.toBlob())
39
+ assert.equal(largeBytes.length, boundary + 2)
40
+ assert.deepEqual([...largeBytes.slice(boundary - 2)], [1,8,9,4]); large.dispose()
41
+ console.log('PASS: out-of-order writes, rewrites, gaps, input-copy isolation, invalid offsets and real 8MiB boundary')
42
+
43
+ // Real encoded fixtures, not dummy bytes: a baseline H.264 I-frame and an AAC-LC frame.
44
+ const h264 = ff(['-f','lavfi','-i','testsrc2=size=160x90:rate=1','-frames:v','1','-c:v','libx264','-profile:v','baseline','-pix_fmt','yuv420p','-f','h264','pipe:1'])
45
+ const markers = [...h264.toString('latin1').matchAll(/\x00\x00\x00?\x01/g)]
46
+ const nals = markers.map((m,i) => h264.subarray(m.index + m[0].length, markers[i+1]?.index ?? h264.length))
47
+ const sps = nals.find(n => (n[0] & 31) === 7), pps = nals.find(n => (n[0] & 31) === 8)
48
+ assert(sps && pps)
49
+ const u16 = n => [n >> 8, n & 255]
50
+ const avcc = Uint8Array.from([1,sps[1],sps[2],sps[3],255,225,...u16(sps.length),...sps,1,...u16(pps.length),...pps])
51
+ const video = Buffer.concat(nals.filter(n => ![7,8].includes(n[0] & 31)).map(n => { const len = Buffer.alloc(4); len.writeUInt32BE(n.length); return Buffer.concat([len,n]) }))
52
+ const videoMeta = { decoderConfig: { codec: `avc1.${sps.subarray(1,4).toString('hex')}`, codedWidth:160, codedHeight:90, description:avcc } }
53
+ const adts = ff(['-f','lavfi','-i','sine=frequency=440:sample_rate=44100','-ac','2','-t','0.1','-c:a','aac','-f','adts','pipe:1'])
54
+ const frameSize = ((adts[3] & 3) << 11) | (adts[4] << 3) | (adts[5] >> 5)
55
+ const audio = adts.subarray((adts[1] & 1) ? 7 : 9, frameSize)
56
+ const objectType = (adts[2] >> 6) + 1, rateIndex = (adts[2] >> 2) & 15, channels = ((adts[2] & 1) << 2) | (adts[3] >> 6)
57
+ const audioMeta = { decoderConfig: { codec:'mp4a.40.2', sampleRate:44100, numberOfChannels:channels, description:Uint8Array.from([(objectType << 3) | (rateIndex >> 1), ((rateIndex & 1) << 7) | (channels << 3)]) } }
58
+ function mux(target, fastStart, seconds, sound) {
59
+ const m = new Muxer({ target, video:{codec:'avc',width:160,height:90,frameRate:1}, ...(sound ? { audio:{codec:'aac',sampleRate:44100,numberOfChannels:channels} } : {}), fastStart, firstTimestampBehavior:'offset' })
60
+ // Match the app: encode all audio before the video frames.
61
+ if (sound) for (let i=0; i<Math.ceil(seconds*44100/1024); i++) m.addAudioChunkRaw(audio,'key',Math.round(i*1024/44100*1e6),1024/44100*1e6,i === 0 ? audioMeta : undefined)
62
+ for (let i=0; i<seconds; i++) m.addVideoChunkRaw(video,'key',i*1e6,1e6,i === 0 ? videoMeta : undefined)
63
+ m.finalize()
64
+ }
65
+ const summary = []
66
+ for (const seconds of [2,151]) for (const sound of [false,true]) {
67
+ const file = createMp4Target(seconds), baseline = new ArrayBufferTarget()
68
+ mux(file.target, file.fastStart, seconds, sound); mux(baseline,file.fastStart,seconds,sound)
69
+ const blob = file.toBlob(), output = await bytes(blob)
70
+ assert.deepEqual(output,new Uint8Array(baseline.buffer),'stream output must match reference byte-for-byte')
71
+ assert.equal(blob.type,'video/mp4')
72
+ const path = join(dir,`${seconds}s-${sound ? 'av' : 'video'}.mp4`)
73
+ await writeFile(path,output); file.dispose()
74
+ const decoded = ff(['-i',path,'-map','0:v:0','-map','0:a:0?','-f','framehash','-hash','md5','pipe:1']).toString()
75
+ const vf = decoded.split('\n').filter(l => /^0,/.test(l)).length
76
+ const af = decoded.split('\n').filter(l => /^1,/.test(l)).length
77
+ assert.equal(vf,seconds); assert.equal(af > 0,sound)
78
+ summary.push({seconds,sound,bytes:output.length,decodedVideoFrames:vf,decodedAudioFrames:af})
79
+ console.log('PASS:',JSON.stringify(summary.at(-1)))
80
+ }
81
+ // Force actual MP4 header back-patches and out-of-order callbacks too.
82
+ const positional = new PositionedBuffer(1024), baseline = new ArrayBufferTarget(), positions = []
83
+ const target = new StreamTarget({ onData:(data, position) => { positions.push(position); positional.write(data,position) }, chunked:true,chunkSize:1024 })
84
+ mux(target,false,10,true);mux(baseline,false,10,true)
85
+ assert.deepEqual(await bytes(positional.toBlob()),new Uint8Array(baseline.buffer))
86
+ assert(positions.some((pos,i) => i > 0 && pos < positions[i-1]),'must exercise backwards writes')
87
+ console.log('PASS: actual nonfragmented MP4 backwards header writes match reference')
88
+ await writeFile(join(dir,'results.json'),JSON.stringify({summary,backwardsWrites:true},null,2))
89
+ console.log('ALL PASS; test artifacts:',dir)
src/film/core/exporter.ts CHANGED
@@ -1,5 +1,6 @@
1
  // 浏览器本地导出 MP4:逐帧离线渲染 → WebCodecs(H.264 + AAC) → MP4 封装
2
- import { Muxer, ArrayBufferTarget, StreamTarget } from 'mp4-muxer'
 
3
  import { Program } from './film'
4
  import { Renderer } from './renderer'
5
  import { FootageExact } from './footage'
@@ -94,14 +95,13 @@ export async function exportMp4(o: ExportOptions): Promise<{ blob: Blob; hasAudi
94
  check()
95
 
96
  const total = Math.ceil(prog.duration * fps)
97
- const big = prog.duration > 150
98
- const parts: Uint8Array[] = []
99
- const target = big ? new StreamTarget({ onData: (d) => parts.push(d.slice()), chunked: true, chunkSize: 8 * 1024 * 1024 }) : new ArrayBufferTarget()
100
  const muxer = new Muxer({
101
  target,
102
  video: { codec: 'avc', width: W, height: H, frameRate: fps },
103
  ...(audio && acodec ? { audio: { codec: acodec.mux, numberOfChannels: 2, sampleRate: acodec.cfg.sampleRate } } : {}),
104
- fastStart: big ? 'fragmented' : 'in-memory',
105
  firstTimestampBehavior: 'offset',
106
  })
107
 
@@ -163,7 +163,8 @@ export async function exportMp4(o: ExportOptions): Promise<{ blob: Blob; hasAudi
163
  muxer.finalize()
164
  footage.dispose()
165
  o.onProgress?.(1, '完成')
166
- const blob = big ? new Blob(parts as BlobPart[], { type: 'video/mp4' }) : new Blob([(target as ArrayBufferTarget).buffer], { type: 'video/mp4' })
 
167
  return { blob, hasAudio: !!audio, codec: vcfg.codec }
168
  }
169
 
 
1
  // 浏览器本地导出 MP4:逐帧离线渲染 → WebCodecs(H.264 + AAC) → MP4 封装
2
+ import { Muxer } from 'mp4-muxer'
3
+ import { createMp4Target } from './mp4-target'
4
  import { Program } from './film'
5
  import { Renderer } from './renderer'
6
  import { FootageExact } from './footage'
 
95
  check()
96
 
97
  const total = Math.ceil(prog.duration * fps)
98
+ const file = createMp4Target(prog.duration)
99
+ const { target, fastStart } = file
 
100
  const muxer = new Muxer({
101
  target,
102
  video: { codec: 'avc', width: W, height: H, frameRate: fps },
103
  ...(audio && acodec ? { audio: { codec: acodec.mux, numberOfChannels: 2, sampleRate: acodec.cfg.sampleRate } } : {}),
104
+ fastStart,
105
  firstTimestampBehavior: 'offset',
106
  })
107
 
 
163
  muxer.finalize()
164
  footage.dispose()
165
  o.onProgress?.(1, '完成')
166
+ const blob = file.toBlob()
167
+ file.dispose()
168
  return { blob, hasAudio: !!audio, codec: vcfg.codec }
169
  }
170
 
src/film/core/mp4-target.ts ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // MP4 streaming callbacks carry absolute byte offsets, not an append-only sequence.
2
+ import { ArrayBufferTarget, StreamTarget } from 'mp4-muxer'
3
+
4
+ const BLOCK_BYTES = 8 * 1024 * 1024
5
+
6
+ export class PositionedBuffer {
7
+ private blocks = new Map<number, Uint8Array<ArrayBuffer>>()
8
+ private blockSize: number
9
+ private length = 0
10
+
11
+ constructor(blockSize = BLOCK_BYTES) {
12
+ if (!Number.isSafeInteger(blockSize) || blockSize <= 0) throw new RangeError('Invalid block size')
13
+ this.blockSize = blockSize
14
+ }
15
+
16
+ write(data: Uint8Array, position: number): void {
17
+ if (!Number.isSafeInteger(position) || position < 0 || !Number.isSafeInteger(position + data.byteLength)) {
18
+ throw new RangeError('Invalid MP4 byte position')
19
+ }
20
+ // Empty callbacks do not extend the file or introduce artificial gaps.
21
+ if (!data.byteLength) return
22
+ let read = 0
23
+ while (read < data.byteLength) {
24
+ const offset = position + read
25
+ const index = Math.floor(offset / this.blockSize)
26
+ const start = offset % this.blockSize
27
+ const count = Math.min(this.blockSize - start, data.byteLength - read)
28
+ let block = this.blocks.get(index)
29
+ if (!block) { block = new Uint8Array(this.blockSize); this.blocks.set(index, block) }
30
+ block.set(data.subarray(read, read + count), start)
31
+ read += count
32
+ }
33
+ this.length = Math.max(this.length, position + data.byteLength)
34
+ }
35
+
36
+ toBlob(): Blob {
37
+ const parts: BlobPart[] = []
38
+ for (let offset = 0; offset < this.length; offset += this.blockSize) {
39
+ const size = Math.min(this.blockSize, this.length - offset)
40
+ const block = this.blocks.get(offset / this.blockSize)
41
+ parts.push(block ? block.subarray(0, size) : new Uint8Array(size))
42
+ }
43
+ return new Blob(parts, { type: 'video/mp4' })
44
+ }
45
+
46
+ dispose(): void { this.blocks.clear(); this.length = 0 }
47
+ }
48
+
49
+ /** Same 150s switch as before; fix storage semantics rather than disabling long exports. */
50
+ export function createMp4Target(duration: number) {
51
+ const stream = duration > 150 ? new PositionedBuffer() : null
52
+ const target = stream
53
+ ? new StreamTarget({
54
+ // Keep BOTH parameters and use the absolute position, including rewrites.
55
+ onData: (data, position) => stream.write(data, position),
56
+ chunked: true,
57
+ chunkSize: BLOCK_BYTES,
58
+ })
59
+ : new ArrayBufferTarget()
60
+ return {
61
+ target,
62
+ fastStart: stream ? 'fragmented' as const : 'in-memory' as const,
63
+ toBlob(): Blob {
64
+ if (stream) return stream.toBlob()
65
+ const buffer = (target as ArrayBufferTarget).buffer
66
+ if (!buffer) throw new Error('MP4 封装尚未完成')
67
+ return new Blob([buffer], { type: 'video/mp4' })
68
+ },
69
+ dispose() { stream?.dispose() },
70
+ }
71
+ }