jvs-ppt-ui 0.0.32 → 0.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +152 -0
  2. package/dist/lib/jvs-ppt-ui.es.js +23678 -21439
  3. package/dist/lib/jvs-ppt-ui.umd.js +136 -100
  4. package/dist/lib/style.css +1 -1
  5. package/package.json +2 -1
  6. package/src/components/CanvasArea.vue +77 -35
  7. package/src/components/Header.vue +184 -26
  8. package/src/components/PitchImportDialog.vue +29 -6
  9. package/src/components/PptEditor.vue +53 -1
  10. package/src/components/QuickColorPopover.vue +2 -1
  11. package/src/components/SidebarLeft.vue +367 -41
  12. package/src/components/SidebarRight.vue +183 -134
  13. package/src/components/canvas/CanvasTransformHandles.vue +27 -36
  14. package/src/components/canvas/elements/CanvasMediaElement.vue +4 -3
  15. package/src/components/canvas/elements/CanvasShapeElement.vue +9 -6
  16. package/src/components/sidebar/panels/LayoutThumbSvg.vue +9 -6
  17. package/src/components/sidebar/panels/SidebarShapeFormatPanel.vue +1 -1
  18. package/src/components/sidebar/panels/SidebarTextFormatPanel.vue +1 -1
  19. package/src/composables/canvas/useCanvasFormatting.ts +20 -13
  20. package/src/composables/canvas/useCanvasText.ts +4 -3
  21. package/src/composables/canvas/useCanvasTransform.ts +37 -10
  22. package/src/composables/sidebar/useSidebarElementTransform.ts +86 -57
  23. package/src/composables/sidebar/useSidebarTextFormat.ts +242 -19
  24. package/src/composables/useSlides.ts +93 -28
  25. package/src/configs/slideTemplates.ts +1001 -0
  26. package/src/style.css +265 -2
  27. package/src/types/index.ts +37 -1
  28. package/src/utils/canvas/elementStyles.ts +55 -20
  29. package/src/utils/canvas/formatPainter.ts +177 -30
  30. package/src/utils/muxHlsToMp4.ts +569 -0
  31. package/src/utils/pitchConverter.ts +512 -13
  32. package/dist/lib/templates/index.json +0 -5
@@ -0,0 +1,569 @@
1
+ import { Buffer } from 'node:buffer'
2
+
3
+ function makeBox(type: string, ...payloads: Buffer[]): Buffer {
4
+ let size = 8
5
+ for (const p of payloads) size += p.length
6
+ const buf = Buffer.allocUnsafe(size)
7
+ buf.writeUInt32BE(size, 0)
8
+ buf.write(type, 4, 4, 'ascii')
9
+ let offset = 8
10
+ for (const p of payloads) {
11
+ p.copy(buf, offset)
12
+ offset += p.length
13
+ }
14
+ return buf
15
+ }
16
+
17
+ function makeFullBox(type: string, version: number, flags: number, ...payloads: Buffer[]): Buffer {
18
+ const vf = Buffer.allocUnsafe(4)
19
+ vf.writeUInt32BE(((version & 0xff) << 24) | (flags & 0xffffff), 0)
20
+ return makeBox(type, vf, ...payloads)
21
+ }
22
+
23
+ function readPts(buf: Buffer, offset: number): number {
24
+ const b0 = buf[offset] || 0
25
+ const b1 = buf[offset + 1] || 0
26
+ const b2 = buf[offset + 2] || 0
27
+ const b3 = buf[offset + 3] || 0
28
+ const b4 = buf[offset + 4] || 0
29
+ return (
30
+ (b0 & 0x0e) * 536870912 +
31
+ ((b1 & 0xff) << 22) +
32
+ ((b2 & 0xfe) << 14) +
33
+ ((b3 & 0xff) << 7) +
34
+ ((b4 & 0xfe) >>> 1)
35
+ )
36
+ }
37
+
38
+ function splitAnnexBNals(es: Buffer): Buffer[] {
39
+ const nals: Buffer[] = []
40
+ const len = es.length
41
+ let start = -1
42
+
43
+ let i = 0
44
+ while (i < len - 3) {
45
+ if (es[i] === 0 && es[i + 1] === 0) {
46
+ if (es[i + 2] === 1) {
47
+ if (start !== -1 && i > start) {
48
+ nals.push(es.subarray(start, i))
49
+ }
50
+ start = i + 3
51
+ i += 3
52
+ continue
53
+ } else if (i < len - 4 && es[i + 2] === 0 && es[i + 3] === 1) {
54
+ if (start !== -1 && i > start) {
55
+ nals.push(es.subarray(start, i))
56
+ }
57
+ start = i + 4
58
+ i += 4
59
+ continue
60
+ }
61
+ }
62
+ i++
63
+ }
64
+ if (start !== -1 && start < len) {
65
+ nals.push(es.subarray(start, len))
66
+ }
67
+ return nals
68
+ }
69
+
70
+ interface VideoSample {
71
+ data: Buffer
72
+ pts: number
73
+ dts: number
74
+ isKeyframe: boolean
75
+ }
76
+
77
+ /**
78
+ * 将 MPEG-TS (H.264/AVC) 字节流无损重封装为标准 faststart MP4 (video/mp4)
79
+ */
80
+ export function remuxMpegTsToMp4(tsBuffer: Buffer, width = 1280, height = 720): Buffer | null {
81
+ const len = tsBuffer.length
82
+ if (len < 188) return null
83
+
84
+ let videoPid = -1
85
+ let pmtPid = -1
86
+ const pesChunksByPid = new Map<number, Buffer[]>()
87
+
88
+ let sps: Buffer | null = null
89
+ let pps: Buffer | null = null
90
+ const samples: VideoSample[] = []
91
+
92
+ const flushPes = (pid: number) => {
93
+ const chunks = pesChunksByPid.get(pid)
94
+ if (!chunks || chunks.length === 0) return
95
+ pesChunksByPid.set(pid, [])
96
+ const pes = Buffer.concat(chunks)
97
+ if (pes.length < 9) return
98
+ if (pes[0] !== 0x00 || pes[1] !== 0x00 || pes[2] !== 0x01) return
99
+ const streamId = pes[3]
100
+ if (streamId < 0xe0 || streamId > 0xef) return
101
+
102
+ const flags2 = pes[7]
103
+ const ptsDtsFlags = (flags2 >> 6) & 0x03
104
+ const headerDataLen = pes[8]
105
+ const esOffset = 9 + headerDataLen
106
+ if (esOffset >= pes.length) return
107
+
108
+ let pts = 0
109
+ let dts = 0
110
+ if (ptsDtsFlags === 0x02 && pes.length >= 14) {
111
+ pts = readPts(pes, 9)
112
+ dts = pts
113
+ } else if (ptsDtsFlags === 0x03 && pes.length >= 19) {
114
+ pts = readPts(pes, 9)
115
+ dts = readPts(pes, 14)
116
+ } else if (samples.length > 0) {
117
+ dts = samples[samples.length - 1].dts + 3000
118
+ pts = dts
119
+ }
120
+
121
+ const nals = splitAnnexBNals(pes.subarray(esOffset))
122
+ if (nals.length === 0) return
123
+
124
+ // 按 AUD (NAL type 9) 或单 PES 包切分访问单元
125
+ const currentFrameNals: Buffer[] = []
126
+ let isKey = false
127
+
128
+ const pushFrame = () => {
129
+ if (currentFrameNals.length === 0) return
130
+ const avccParts: Buffer[] = []
131
+ for (const nal of currentFrameNals) {
132
+ const hdr = Buffer.allocUnsafe(4)
133
+ hdr.writeUInt32BE(nal.length, 0)
134
+ avccParts.push(hdr, nal)
135
+ }
136
+ samples.push({
137
+ data: Buffer.concat(avccParts),
138
+ pts,
139
+ dts,
140
+ isKeyframe: isKey
141
+ })
142
+ currentFrameNals.length = 0
143
+ isKey = false
144
+ }
145
+
146
+ for (const rawNal of nals) {
147
+ if (rawNal.length === 0) continue
148
+ const nalType = rawNal[0] & 0x1f
149
+ if (nalType === 9) {
150
+ // Access Unit Delimiter: 若已有切片则先推入上一帧
151
+ if (currentFrameNals.length > 0) {
152
+ pushFrame()
153
+ dts += 3000
154
+ pts += 3000
155
+ }
156
+ continue
157
+ }
158
+ if (nalType === 7) {
159
+ if (!sps) sps = Buffer.from(rawNal)
160
+ continue
161
+ }
162
+ if (nalType === 8) {
163
+ if (!pps) pps = Buffer.from(rawNal)
164
+ continue
165
+ }
166
+ if (nalType === 5) {
167
+ isKey = true
168
+ }
169
+ if (nalType >= 1 && nalType <= 6) {
170
+ currentFrameNals.push(rawNal)
171
+ }
172
+ }
173
+ pushFrame()
174
+ }
175
+
176
+ // 扫描 188 字节 TS 包
177
+ let p = 0
178
+ while (p <= len - 188) {
179
+ if (tsBuffer[p] !== 0x47) {
180
+ p++
181
+ continue
182
+ }
183
+ const pusi = (tsBuffer[p + 1] & 0x40) !== 0
184
+ const pid = ((tsBuffer[p + 1] & 0x1f) << 8) | tsBuffer[p + 2]
185
+ const afc = (tsBuffer[p + 3] >> 4) & 0x03
186
+
187
+ let payloadOffset = p + 4
188
+ if (afc === 0x02) {
189
+ p += 188
190
+ continue
191
+ }
192
+ if (afc === 0x03) {
193
+ const adaptLen = tsBuffer[p + 4]
194
+ payloadOffset = p + 5 + adaptLen
195
+ }
196
+ if (payloadOffset >= p + 188) {
197
+ p += 188
198
+ continue
199
+ }
200
+
201
+ const payload = tsBuffer.subarray(payloadOffset, p + 188)
202
+
203
+ // 解析 PAT (PID 0)
204
+ if (pid === 0 && pusi && payload.length > 13) {
205
+ const pointerField = payload[0]
206
+ const secStart = 1 + pointerField
207
+ if (secStart + 12 <= payload.length && payload[secStart] === 0x00) {
208
+ const progNum = (payload[secStart + 8] << 8) | payload[secStart + 9]
209
+ if (progNum !== 0) {
210
+ pmtPid = ((payload[secStart + 10] & 0x1f) << 8) | payload[secStart + 11]
211
+ }
212
+ }
213
+ p += 188
214
+ continue
215
+ }
216
+
217
+ // 解析 PMT
218
+ if (pmtPid !== -1 && pid === pmtPid && pusi && payload.length > 17) {
219
+ const pointerField = payload[0]
220
+ const secStart = 1 + pointerField
221
+ if (secStart + 12 <= payload.length && payload[secStart] === 0x02) {
222
+ const sectionLen = ((payload[secStart + 1] & 0x0f) << 8) | payload[secStart + 2]
223
+ const progInfoLen = ((payload[secStart + 10] & 0x0f) << 8) | payload[secStart + 11]
224
+ let pos = secStart + 12 + progInfoLen
225
+ const endPos = Math.min(payload.length, secStart + 3 + sectionLen - 4)
226
+ while (pos + 5 <= endPos) {
227
+ const streamType = payload[pos]
228
+ const elemPid = ((payload[pos + 1] & 0x1f) << 8) | payload[pos + 2]
229
+ const esInfoLen = ((payload[pos + 3] & 0x0f) << 8) | payload[pos + 4]
230
+ if (streamType === 0x1b && videoPid === -1) {
231
+ videoPid = elemPid
232
+ }
233
+ pos += 5 + esInfoLen
234
+ }
235
+ }
236
+ p += 188
237
+ continue
238
+ }
239
+
240
+ // 识别视频 PES 流
241
+ if (pusi && payload.length >= 4 && payload[0] === 0x00 && payload[1] === 0x00 && payload[2] === 0x01) {
242
+ const streamId = payload[3]
243
+ if (streamId >= 0xe0 && streamId <= 0xef) {
244
+ if (videoPid === -1) videoPid = pid
245
+ if (pid === videoPid) {
246
+ flushPes(pid)
247
+ pesChunksByPid.set(pid, [payload])
248
+ }
249
+ }
250
+ } else if (pid === videoPid && videoPid !== -1) {
251
+ const arr = pesChunksByPid.get(pid)
252
+ if (arr) arr.push(payload)
253
+ }
254
+
255
+ p += 188
256
+ }
257
+
258
+ if (videoPid !== -1) {
259
+ flushPes(videoPid)
260
+ }
261
+
262
+ if (!sps || !pps || samples.length === 0) {
263
+ return null
264
+ }
265
+
266
+ // 归一化时间戳 (90000 Hz)
267
+ const timescale = 90000
268
+ const defaultDelta = 3000 // ~30fps
269
+ const baseDts = samples[0].dts
270
+ for (let i = 0; i < samples.length; i++) {
271
+ samples[i].dts = Math.max(0, samples[i].dts - baseDts)
272
+ samples[i].pts = Math.max(samples[i].dts, samples[i].pts - baseDts)
273
+ if (i > 0 && samples[i].dts <= samples[i - 1].dts) {
274
+ samples[i].dts = samples[i - 1].dts + defaultDelta
275
+ if (samples[i].pts < samples[i].dts) {
276
+ samples[i].pts = samples[i].dts
277
+ }
278
+ }
279
+ }
280
+
281
+ const durations: number[] = []
282
+ let totalDuration = 0
283
+ for (let i = 0; i < samples.length; i++) {
284
+ const d =
285
+ i < samples.length - 1
286
+ ? Math.max(1, samples[i + 1].dts - samples[i].dts)
287
+ : samples.length > 1
288
+ ? durations[durations.length - 1]
289
+ : defaultDelta
290
+ durations.push(d)
291
+ totalDuration += d
292
+ }
293
+
294
+ const buildMoov = (firstSampleOffset: number): Buffer => {
295
+ // 1. mvhd
296
+ const mvhdPayload = Buffer.alloc(96, 0)
297
+ mvhdPayload.writeUInt32BE(timescale, 8)
298
+ mvhdPayload.writeUInt32BE(totalDuration, 12)
299
+ mvhdPayload.writeUInt32BE(0x00010000, 16) // rate 1.0
300
+ mvhdPayload.writeUInt16BE(0x0100, 20) // volume 1.0
301
+ // unity matrix
302
+ mvhdPayload.writeUInt32BE(0x00010000, 32)
303
+ mvhdPayload.writeUInt32BE(0x00010000, 48)
304
+ mvhdPayload.writeUInt32BE(0x40000000, 64)
305
+ mvhdPayload.writeUInt32BE(2, 92) // next_track_ID
306
+ const mvhd = makeFullBox('mvhd', 0, 0, mvhdPayload)
307
+
308
+ // 2. tkhd
309
+ const tkhdPayload = Buffer.alloc(80, 0)
310
+ tkhdPayload.writeUInt32BE(1, 8) // track_ID = 1
311
+ tkhdPayload.writeUInt32BE(totalDuration, 16)
312
+ tkhdPayload.writeUInt32BE(0x00010000, 36)
313
+ tkhdPayload.writeUInt32BE(0x00010000, 52)
314
+ tkhdPayload.writeUInt32BE(0x40000000, 68)
315
+ tkhdPayload.writeUInt32BE(width << 16, 72)
316
+ tkhdPayload.writeUInt32BE(height << 16, 76)
317
+ const tkhd = makeFullBox('tkhd', 0, 0x000003, tkhdPayload)
318
+
319
+ // 3. mdhd
320
+ const mdhdPayload = Buffer.alloc(20, 0)
321
+ mdhdPayload.writeUInt32BE(timescale, 8)
322
+ mdhdPayload.writeUInt32BE(totalDuration, 12)
323
+ mdhdPayload.writeUInt16BE(0x55c4, 16) // und
324
+ const mdhd = makeFullBox('mdhd', 0, 0, mdhdPayload)
325
+
326
+ // 4. hdlr
327
+ const hdlrPayload = Buffer.alloc(21, 0)
328
+ hdlrPayload.write('vide', 4, 4, 'ascii')
329
+ hdlrPayload.write('VideoHandler', 16, 12, 'ascii')
330
+ const hdlr = makeFullBox('hdlr', 0, 0, hdlrPayload)
331
+
332
+ // 5. vmhd & dinf
333
+ const vmhd = makeFullBox('vmhd', 0, 1, Buffer.alloc(8, 0))
334
+ const urlBox = makeFullBox('url ', 0, 1)
335
+ const drefPayload = Buffer.alloc(4, 0)
336
+ drefPayload.writeUInt32BE(1, 0)
337
+ const dref = makeFullBox('dref', 0, 0, drefPayload, urlBox)
338
+ const dinf = makeBox('dinf', dref)
339
+
340
+ // 6. stsd (avc1 + avcC)
341
+ const spsBuf = sps!
342
+ const ppsBuf = pps!
343
+ const avcCPayload = Buffer.allocUnsafe(11 + spsBuf.length + ppsBuf.length)
344
+ avcCPayload[0] = 1 // configurationVersion
345
+ avcCPayload[1] = spsBuf[1] || 0x64 // AVCProfileIndication
346
+ avcCPayload[2] = spsBuf[2] || 0x00 // profile_compatibility
347
+ avcCPayload[3] = spsBuf[3] || 0x1f // AVCLevelIndication
348
+ avcCPayload[4] = 0xff // lengthSizeMinusOne = 3 (4 bytes)
349
+ avcCPayload[5] = 0xe1 // numOfSequenceParameterSets = 1
350
+ avcCPayload.writeUInt16BE(spsBuf.length, 6)
351
+ spsBuf.copy(avcCPayload, 8)
352
+ let pOff = 8 + spsBuf.length
353
+ avcCPayload[pOff] = 1 // numOfPictureParameterSets = 1
354
+ avcCPayload.writeUInt16BE(ppsBuf.length, pOff + 1)
355
+ ppsBuf.copy(avcCPayload, pOff + 3)
356
+ const avcC = makeBox('avcC', avcCPayload)
357
+
358
+ const avc1Header = Buffer.alloc(78, 0)
359
+ avc1Header.writeUInt16BE(1, 6) // data_reference_index
360
+ avc1Header.writeUInt16BE(width, 24)
361
+ avc1Header.writeUInt16BE(height, 26)
362
+ avc1Header.writeUInt32BE(0x00480000, 28) // horizresolution 72dpi
363
+ avc1Header.writeUInt32BE(0x00480000, 32) // vertresolution 72dpi
364
+ avc1Header.writeUInt16BE(1, 40) // frame_count
365
+ avc1Header.writeUInt16BE(0x0018, 74) // depth = 24
366
+ avc1Header.writeInt16BE(-1, 76) // pre_defined = -1
367
+ const avc1 = makeBox('avc1', avc1Header, avcC)
368
+
369
+ const stsdHeader = Buffer.alloc(4, 0)
370
+ stsdHeader.writeUInt32BE(1, 0)
371
+ const stsd = makeFullBox('stsd', 0, 0, stsdHeader, avc1)
372
+
373
+ // 7. stts
374
+ const sttsPayload = Buffer.allocUnsafe(4 + samples.length * 8)
375
+ sttsPayload.writeUInt32BE(samples.length, 0)
376
+ for (let i = 0; i < samples.length; i++) {
377
+ sttsPayload.writeUInt32BE(1, 4 + i * 8)
378
+ sttsPayload.writeUInt32BE(durations[i], 8 + i * 8)
379
+ }
380
+ const stts = makeFullBox('stts', 0, 0, sttsPayload)
381
+
382
+ // 8. ctts
383
+ const cttsPayload = Buffer.allocUnsafe(4 + samples.length * 8)
384
+ cttsPayload.writeUInt32BE(samples.length, 0)
385
+ for (let i = 0; i < samples.length; i++) {
386
+ const cts = Math.max(0, samples[i].pts - samples[i].dts)
387
+ cttsPayload.writeUInt32BE(1, 4 + i * 8)
388
+ cttsPayload.writeUInt32BE(cts, 8 + i * 8)
389
+ }
390
+ const ctts = makeFullBox('ctts', 0, 0, cttsPayload)
391
+
392
+ // 9. stss (keyframes)
393
+ const keyIndices: number[] = []
394
+ for (let i = 0; i < samples.length; i++) {
395
+ if (samples[i].isKeyframe) keyIndices.push(i + 1)
396
+ }
397
+ if (keyIndices.length === 0) keyIndices.push(1)
398
+ const stssPayload = Buffer.allocUnsafe(4 + keyIndices.length * 4)
399
+ stssPayload.writeUInt32BE(keyIndices.length, 0)
400
+ for (let i = 0; i < keyIndices.length; i++) {
401
+ stssPayload.writeUInt32BE(keyIndices[i], 4 + i * 4)
402
+ }
403
+ const stss = makeFullBox('stss', 0, 0, stssPayload)
404
+
405
+ // 10. stsc (1 sample per chunk)
406
+ const stscPayload = Buffer.allocUnsafe(16)
407
+ stscPayload.writeUInt32BE(1, 0) // entry_count = 1
408
+ stscPayload.writeUInt32BE(1, 4) // first_chunk = 1
409
+ stscPayload.writeUInt32BE(1, 8) // samples_per_chunk = 1
410
+ stscPayload.writeUInt32BE(1, 12) // sample_description_index = 1
411
+ const stsc = makeFullBox('stsc', 0, 0, stscPayload)
412
+
413
+ // 11. stsz
414
+ const stszPayload = Buffer.allocUnsafe(8 + samples.length * 4)
415
+ stszPayload.writeUInt32BE(0, 0) // sample_size = 0 (variable)
416
+ stszPayload.writeUInt32BE(samples.length, 4)
417
+ for (let i = 0; i < samples.length; i++) {
418
+ stszPayload.writeUInt32BE(samples[i].data.length, 8 + i * 4)
419
+ }
420
+ const stsz = makeFullBox('stsz', 0, 0, stszPayload)
421
+
422
+ // 12. stco
423
+ const stcoPayload = Buffer.allocUnsafe(4 + samples.length * 4)
424
+ stcoPayload.writeUInt32BE(samples.length, 0)
425
+ let curOff = firstSampleOffset
426
+ for (let i = 0; i < samples.length; i++) {
427
+ stcoPayload.writeUInt32BE(curOff, 4 + i * 4)
428
+ curOff += samples[i].data.length
429
+ }
430
+ const stco = makeFullBox('stco', 0, 0, stcoPayload)
431
+
432
+ const stbl = makeBox('stbl', stsd, stts, ctts, stss, stsc, stsz, stco)
433
+ const minf = makeBox('minf', vmhd, dinf, stbl)
434
+ const mdia = makeBox('mdia', mdhd, hdlr, minf)
435
+ const trak = makeBox('trak', tkhd, mdia)
436
+ return makeBox('moov', mvhd, trak)
437
+ }
438
+
439
+ const ftypPayload = Buffer.allocUnsafe(20)
440
+ ftypPayload.write('isom', 0, 4, 'ascii')
441
+ ftypPayload.writeUInt32BE(0x200, 4)
442
+ ftypPayload.write('isomiso2avc1mp41', 8, 16, 'ascii')
443
+ const ftyp = makeBox('ftyp', ftypPayload)
444
+
445
+ const dummyMoov = buildMoov(0)
446
+ const firstSampleOffset = ftyp.length + dummyMoov.length + 8
447
+ const finalMoov = buildMoov(firstSampleOffset)
448
+
449
+ let mdatPayloadSize = 0
450
+ for (const s of samples) mdatPayloadSize += s.data.length
451
+ const mdatHeader = Buffer.allocUnsafe(8)
452
+ mdatHeader.writeUInt32BE(8 + mdatPayloadSize, 0)
453
+ mdatHeader.write('mdat', 4, 4, 'ascii')
454
+
455
+ return Buffer.concat([ftyp, finalMoov, mdatHeader, ...samples.map(s => s.data)])
456
+ }
457
+
458
+ /**
459
+ * 拉取 HLS (.m3u8) 播放列表中的分片并转换为标准 MP4 Buffer
460
+ */
461
+ export async function fetchHlsAndConvertToMp4(
462
+ m3u8Url: string,
463
+ headers: Record<string, string>
464
+ ): Promise<{ buffer: Buffer; mime: string } | null> {
465
+ try {
466
+ const masterRes = await fetch(m3u8Url, { headers })
467
+ if (!masterRes.ok) return null
468
+ const masterText = await masterRes.text()
469
+ if (!masterText.includes('#EXTM3U')) return null
470
+
471
+ let mediaPlaylistUrl = m3u8Url
472
+ let mediaPlaylistText = masterText
473
+ let width = 1280
474
+ let height = 720
475
+
476
+ // 若是主播放列表 (Master Playlist),挑选分辨率适中(优先 720p/1080p)的子播放列表
477
+ if (masterText.includes('#EXT-X-STREAM-INF')) {
478
+ const lines = masterText.split(/\r?\n/).map(l => l.trim()).filter(Boolean)
479
+ const variants: Array<{ url: string; bandwidth: number; w: number; h: number }> = []
480
+ for (let i = 0; i < lines.length; i++) {
481
+ if (lines[i].startsWith('#EXT-X-STREAM-INF:')) {
482
+ const info = lines[i]
483
+ const nextLine = lines[i + 1]
484
+ if (nextLine && !nextLine.startsWith('#')) {
485
+ const bwMatch = info.match(/BANDWIDTH=(\d+)/i)
486
+ const resMatch = info.match(/RESOLUTION=(\d+)x(\d+)/i)
487
+ const resolvedUrl = new URL(nextLine, m3u8Url).toString()
488
+ variants.push({
489
+ url: resolvedUrl,
490
+ bandwidth: bwMatch ? Number(bwMatch[1]) : 0,
491
+ w: resMatch ? Number(resMatch[1]) : 1280,
492
+ h: resMatch ? Number(resMatch[2]) : 720
493
+ })
494
+ }
495
+ }
496
+ }
497
+ if (variants.length === 0) return null
498
+ // 优先选择高度在 360~720 之间的适中清晰流,兼顾画质与 Base64 JSON 体积
499
+ variants.sort((a, b) => {
500
+ const inBandA = a.h >= 360 && a.h <= 720 ? 1 : 0
501
+ const inBandB = b.h >= 360 && b.h <= 720 ? 1 : 0
502
+ if (inBandA !== inBandB) return inBandB - inBandA
503
+ return Math.abs(a.h - 540) - Math.abs(b.h - 540)
504
+ })
505
+ const chosen = variants[0]
506
+ mediaPlaylistUrl = chosen.url
507
+ width = chosen.w || 1280
508
+ height = chosen.h || 720
509
+
510
+ const subRes = await fetch(mediaPlaylistUrl, { headers })
511
+ if (!subRes.ok) return null
512
+ mediaPlaylistText = await subRes.text()
513
+ }
514
+
515
+ const mediaLines = mediaPlaylistText.split(/\r?\n/).map(l => l.trim()).filter(Boolean)
516
+ let initMapUrl = ''
517
+ const segmentUrls: string[] = []
518
+
519
+ for (const line of mediaLines) {
520
+ if (line.startsWith('#EXT-X-MAP:')) {
521
+ const uriMatch = line.match(/URI="([^"]+)"/i)
522
+ if (uriMatch && uriMatch[1]) {
523
+ initMapUrl = new URL(uriMatch[1], mediaPlaylistUrl).toString()
524
+ }
525
+ } else if (!line.startsWith('#')) {
526
+ segmentUrls.push(new URL(line, mediaPlaylistUrl).toString())
527
+ }
528
+ }
529
+
530
+ if (segmentUrls.length === 0) return null
531
+ const maxSegments = Math.min(segmentUrls.length, 6)
532
+ const targetSegments = segmentUrls.slice(0, maxSegments)
533
+
534
+ const downloadedChunks: Buffer[] = []
535
+ if (initMapUrl) {
536
+ const initRes = await fetch(initMapUrl, { headers })
537
+ if (initRes.ok) {
538
+ downloadedChunks.push(Buffer.from(await initRes.arrayBuffer()))
539
+ }
540
+ }
541
+
542
+ for (const segUrl of targetSegments) {
543
+ const segRes = await fetch(segUrl, { headers })
544
+ if (!segRes.ok) break
545
+ const ab = await segRes.arrayBuffer()
546
+ if (ab.byteLength > 0) {
547
+ downloadedChunks.push(Buffer.from(ab))
548
+ }
549
+ }
550
+
551
+ if (downloadedChunks.length === 0) return null
552
+ const combined = Buffer.concat(downloadedChunks)
553
+
554
+ // 如果包含 #EXT-X-MAP 或是 ftyp 开头的 fMP4 分片,直接拼接即为合法 MP4
555
+ if (initMapUrl || (combined.length >= 8 && combined.subarray(4, 8).toString('ascii') === 'ftyp')) {
556
+ return { buffer: combined, mime: 'video/mp4' }
557
+ }
558
+
559
+ // 否则为 MPEG-TS (.ts) 分片,执行无损重封装为 MP4
560
+ const mp4Buf = remuxMpegTsToMp4(combined, width, height)
561
+ if (mp4Buf && mp4Buf.length > 0) {
562
+ return { buffer: mp4Buf, mime: 'video/mp4' }
563
+ }
564
+
565
+ return null
566
+ } catch {
567
+ return null
568
+ }
569
+ }