@meframe/core 0.5.9 → 0.5.11

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 (49) hide show
  1. package/dist/node_modules/.pnpm/mp4-muxer@5.2.2/node_modules/mp4-muxer/build/mp4-muxer.js.map +1 -0
  2. package/dist/{meframe-service/node_modules → node_modules}/.pnpm/mp4box@0.5.4/node_modules/mp4box/dist/mp4box.all.js +14 -14
  3. package/dist/node_modules/.pnpm/mp4box@0.5.4/node_modules/mp4box/dist/mp4box.all.js.map +1 -0
  4. package/dist/orchestrator/CompositionPlanner.d.ts.map +1 -1
  5. package/dist/orchestrator/CompositionPlanner.js +6 -1
  6. package/dist/orchestrator/CompositionPlanner.js.map +1 -1
  7. package/dist/stages/compose/VideoComposer.d.ts +9 -0
  8. package/dist/stages/compose/VideoComposer.d.ts.map +1 -1
  9. package/dist/stages/compose/VideoComposer.js +15 -0
  10. package/dist/stages/compose/VideoComposer.js.map +1 -1
  11. package/dist/stages/compose/text-renderers/basic-text-renderer.d.ts.map +1 -1
  12. package/dist/stages/compose/text-renderers/basic-text-renderer.js +6 -18
  13. package/dist/stages/compose/text-renderers/basic-text-renderer.js.map +1 -1
  14. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.d.ts.map +1 -1
  15. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js +6 -18
  16. package/dist/stages/compose/text-renderers/caption-stagger-entrance-renderer.js.map +1 -1
  17. package/dist/stages/compose/text-renderers/character-ktv-renderer.d.ts.map +1 -1
  18. package/dist/stages/compose/text-renderers/character-ktv-renderer.js +6 -18
  19. package/dist/stages/compose/text-renderers/character-ktv-renderer.js.map +1 -1
  20. package/dist/stages/compose/text-renderers/word-by-word-renderer.d.ts.map +1 -1
  21. package/dist/stages/compose/text-renderers/word-by-word-renderer.js +6 -18
  22. package/dist/stages/compose/text-renderers/word-by-word-renderer.js.map +1 -1
  23. package/dist/stages/compose/text-renderers/word-fancy-renderer.d.ts.map +1 -1
  24. package/dist/stages/compose/text-renderers/word-fancy-renderer.js +6 -18
  25. package/dist/stages/compose/text-renderers/word-fancy-renderer.js.map +1 -1
  26. package/dist/stages/compose/text-utils/caption-position.d.ts +12 -0
  27. package/dist/stages/compose/text-utils/caption-position.d.ts.map +1 -0
  28. package/dist/stages/compose/text-utils/caption-position.js +21 -0
  29. package/dist/stages/compose/text-utils/caption-position.js.map +1 -0
  30. package/dist/stages/encode/AudioChunkEncoder.d.ts +1 -0
  31. package/dist/stages/encode/AudioChunkEncoder.d.ts.map +1 -1
  32. package/dist/stages/encode/AudioChunkEncoder.js +4 -1
  33. package/dist/stages/encode/AudioChunkEncoder.js.map +1 -1
  34. package/dist/stages/encode/BaseEncoder.d.ts +12 -0
  35. package/dist/stages/encode/BaseEncoder.d.ts.map +1 -1
  36. package/dist/stages/encode/BaseEncoder.js +28 -0
  37. package/dist/stages/encode/BaseEncoder.js.map +1 -1
  38. package/dist/stages/encode/VideoChunkEncoder.d.ts +1 -0
  39. package/dist/stages/encode/VideoChunkEncoder.d.ts.map +1 -1
  40. package/dist/stages/mux/MP4Muxer.js +1 -1
  41. package/dist/utils/mp4box.js +1 -1
  42. package/dist/workers/stages/export/{export.worker.CWPrx2Nf.js → export.worker.CO7nW1W0.js} +100 -92
  43. package/dist/workers/stages/export/export.worker.CO7nW1W0.js.map +1 -0
  44. package/dist/workers/worker-manifest.json +1 -1
  45. package/package.json +1 -1
  46. package/dist/meframe-service/node_modules/.pnpm/mp4-muxer@5.2.2/node_modules/mp4-muxer/build/mp4-muxer.js.map +0 -1
  47. package/dist/meframe-service/node_modules/.pnpm/mp4box@0.5.4/node_modules/mp4box/dist/mp4box.all.js.map +0 -1
  48. package/dist/workers/stages/export/export.worker.CWPrx2Nf.js.map +0 -1
  49. /package/dist/{meframe-service/node_modules → node_modules}/.pnpm/mp4-muxer@5.2.2/node_modules/mp4-muxer/build/mp4-muxer.js +0 -0
@@ -525,6 +525,23 @@ function textAnimationFrameForTextLayer(layer, fallbackFrame, fps) {
525
525
  }
526
526
  return fallbackFrame;
527
527
  }
528
+ function calculateCaptionStartY(canvasHeight, totalHeight, globalPosition) {
529
+ if (!globalPosition) {
530
+ return canvasHeight / 2 - totalHeight / 2;
531
+ }
532
+ if (globalPosition.top) {
533
+ const centerPercent = parseFloat(globalPosition.top) / 100;
534
+ return canvasHeight * centerPercent - totalHeight / 2;
535
+ }
536
+ if (globalPosition.bottom) {
537
+ const bottomPercent = parseFloat(globalPosition.bottom) / 100;
538
+ return canvasHeight * (1 - bottomPercent) - totalHeight;
539
+ }
540
+ if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
541
+ return canvasHeight / 2 - totalHeight / 2;
542
+ }
543
+ return canvasHeight / 2 - totalHeight / 2;
544
+ }
528
545
  function measureTextWidth(ctx, text, fontSize, fontFamily, fontWeight = 400) {
529
546
  ctx.save();
530
547
  ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
@@ -847,23 +864,6 @@ function needsSpaceBetweenWords(locale, text) {
847
864
  }
848
865
  return !["zh-CN", "ja-JP", "ko-KR"].includes(locale);
849
866
  }
850
- function calculateYPosition$4(canvasHeight, totalHeight, globalPosition) {
851
- if (!globalPosition) {
852
- return canvasHeight / 2 - totalHeight / 2;
853
- }
854
- if (globalPosition.top) {
855
- const topPercent = parseFloat(globalPosition.top) / 100;
856
- return canvasHeight * topPercent;
857
- }
858
- if (globalPosition.bottom) {
859
- const bottomPercent = parseFloat(globalPosition.bottom) / 100;
860
- return canvasHeight * (1 - bottomPercent) - totalHeight;
861
- }
862
- if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
863
- return canvasHeight / 2 - totalHeight / 2;
864
- }
865
- return canvasHeight / 2 - totalHeight / 2;
866
- }
867
867
  function drawBasicTextLines(ctx, layer, canvasWidth, canvasHeight, lines) {
868
868
  const fontConfig = layer.fontConfig?.textStyle;
869
869
  if (!fontConfig) return;
@@ -881,7 +881,11 @@ function drawBasicTextLines(ctx, layer, canvasWidth, canvasHeight, lines) {
881
881
  ctx.lineJoin = "round";
882
882
  ctx.lineCap = "round";
883
883
  const totalHeight = lines.length * fontSize * lineHeight;
884
- const startY = calculateYPosition$4(canvasHeight, totalHeight, layer.fontConfig?.globalPosition);
884
+ const startY = calculateCaptionStartY(
885
+ canvasHeight,
886
+ totalHeight,
887
+ layer.fontConfig?.globalPosition
888
+ );
885
889
  for (let i = 0; i < lines.length; i++) {
886
890
  const line = lines[i];
887
891
  const y = startY + i * fontSize * lineHeight + fontSize / 2;
@@ -949,23 +953,6 @@ function easeOutExpo(t) {
949
953
  if (t >= 1) return 1;
950
954
  return 1 - Math.pow(2, -10 * t);
951
955
  }
952
- function calculateYPosition$3(canvasHeight, totalHeight, globalPosition) {
953
- if (!globalPosition) {
954
- return canvasHeight / 2 - totalHeight / 2;
955
- }
956
- if (globalPosition.top) {
957
- const topPercent = parseFloat(globalPosition.top) / 100;
958
- return canvasHeight * topPercent;
959
- }
960
- if (globalPosition.bottom) {
961
- const bottomPercent = parseFloat(globalPosition.bottom) / 100;
962
- return canvasHeight * (1 - bottomPercent) - totalHeight;
963
- }
964
- if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
965
- return canvasHeight / 2 - totalHeight / 2;
966
- }
967
- return canvasHeight / 2 - totalHeight / 2;
968
- }
969
956
  function charProgress(relativeFrame, fps, charIndex, staggerMs, durationMs) {
970
957
  const tMs = relativeFrame / fps * 1e3;
971
958
  const startMs = charIndex * staggerMs;
@@ -1030,7 +1017,11 @@ function buildCharSlots(ctx, layer, canvasWidth, canvasHeight) {
1030
1017
  lines = getCachedWrapText(ctx, text, maxWidth, fontSize, fontFamily, fontWeight);
1031
1018
  }
1032
1019
  const totalHeight = lines.length * fontSize * lineHeight;
1033
- const startY = calculateYPosition$3(canvasHeight, totalHeight, layer.fontConfig?.globalPosition);
1020
+ const startY = calculateCaptionStartY(
1021
+ canvasHeight,
1022
+ totalHeight,
1023
+ layer.fontConfig?.globalPosition
1024
+ );
1034
1025
  ctx.save();
1035
1026
  ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
1036
1027
  const slots = [];
@@ -1193,23 +1184,6 @@ function renderCaptionStaggerEntrance(ctx, layer, canvasWidth, canvasHeight, rel
1193
1184
  function usToFrame$2(us, fps) {
1194
1185
  return Math.floor(us / (1e6 / fps));
1195
1186
  }
1196
- function calculateYPosition$2(canvasHeight, totalHeight, globalPosition) {
1197
- if (!globalPosition) {
1198
- return canvasHeight / 2 - totalHeight / 2;
1199
- }
1200
- if (globalPosition.top) {
1201
- const topPercent = parseFloat(globalPosition.top) / 100;
1202
- return canvasHeight * topPercent;
1203
- }
1204
- if (globalPosition.bottom) {
1205
- const bottomPercent = parseFloat(globalPosition.bottom) / 100;
1206
- return canvasHeight * (1 - bottomPercent) - totalHeight;
1207
- }
1208
- if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
1209
- return canvasHeight / 2 - totalHeight / 2;
1210
- }
1211
- return canvasHeight / 2 - totalHeight / 2;
1212
- }
1213
1187
  function renderWordByWord(ctx, layer, canvasWidth, canvasHeight, relativeFrame, fps = 30) {
1214
1188
  const fontConfig = layer.fontConfig?.textStyle;
1215
1189
  if (!fontConfig) return;
@@ -1238,7 +1212,11 @@ function renderWordByWord(ctx, layer, canvasWidth, canvasHeight, relativeFrame,
1238
1212
  const wordPositions = [];
1239
1213
  let wordIndex = 0;
1240
1214
  const totalHeight = lines.length * fontSize * lineHeight;
1241
- const startY = calculateYPosition$2(canvasHeight, totalHeight, layer.fontConfig?.globalPosition);
1215
+ const startY = calculateCaptionStartY(
1216
+ canvasHeight,
1217
+ totalHeight,
1218
+ layer.fontConfig?.globalPosition
1219
+ );
1242
1220
  for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
1243
1221
  const line = lines[lineIndex];
1244
1222
  const lineWords = line.split(needsSpace ? /\s+/ : "");
@@ -1313,23 +1291,6 @@ function renderWordByWord(ctx, layer, canvasWidth, canvasHeight, relativeFrame,
1313
1291
  function usToFrame$1(us, fps) {
1314
1292
  return Math.floor(us / (1e6 / fps));
1315
1293
  }
1316
- function calculateYPosition$1(canvasHeight, totalHeight, globalPosition) {
1317
- if (!globalPosition) {
1318
- return canvasHeight / 2 - totalHeight / 2;
1319
- }
1320
- if (globalPosition.top) {
1321
- const topPercent = parseFloat(globalPosition.top) / 100;
1322
- return canvasHeight * topPercent;
1323
- }
1324
- if (globalPosition.bottom) {
1325
- const bottomPercent = parseFloat(globalPosition.bottom) / 100;
1326
- return canvasHeight * (1 - bottomPercent) - totalHeight;
1327
- }
1328
- if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
1329
- return canvasHeight / 2 - totalHeight / 2;
1330
- }
1331
- return canvasHeight / 2 - totalHeight / 2;
1332
- }
1333
1294
  function renderCharacterKTV(ctx, layer, canvasWidth, canvasHeight, relativeFrame, fps = 30) {
1334
1295
  const fontConfig = layer.fontConfig?.textStyle;
1335
1296
  if (!fontConfig) return;
@@ -1401,7 +1362,11 @@ function renderCharacterKTV(ctx, layer, canvasWidth, canvasHeight, relativeFrame
1401
1362
  ctx.lineJoin = "round";
1402
1363
  ctx.lineCap = "round";
1403
1364
  const totalHeight = fullTextLines.length * fontSize * lineHeight;
1404
- const startY = calculateYPosition$1(canvasHeight, totalHeight, layer.fontConfig?.globalPosition);
1365
+ const startY = calculateCaptionStartY(
1366
+ canvasHeight,
1367
+ totalHeight,
1368
+ layer.fontConfig?.globalPosition
1369
+ );
1405
1370
  let charIndexInText = 0;
1406
1371
  for (let lineIndex = 0; lineIndex < fullTextLines.length; lineIndex++) {
1407
1372
  const line = fullTextLines[lineIndex];
@@ -1438,23 +1403,6 @@ function renderCharacterKTV(ctx, layer, canvasWidth, canvasHeight, relativeFrame
1438
1403
  function usToFrame(us, fps) {
1439
1404
  return Math.floor(us / (1e6 / fps));
1440
1405
  }
1441
- function calculateYPosition(canvasHeight, totalHeight, globalPosition) {
1442
- if (!globalPosition) {
1443
- return canvasHeight / 2 - totalHeight / 2;
1444
- }
1445
- if (globalPosition.top) {
1446
- const topPercent = parseFloat(globalPosition.top) / 100;
1447
- return canvasHeight * topPercent;
1448
- }
1449
- if (globalPosition.bottom) {
1450
- const bottomPercent = parseFloat(globalPosition.bottom) / 100;
1451
- return canvasHeight * (1 - bottomPercent) - totalHeight;
1452
- }
1453
- if (globalPosition.justifyContent === "center" || globalPosition.alignItems === "center") {
1454
- return canvasHeight / 2 - totalHeight / 2;
1455
- }
1456
- return canvasHeight / 2 - totalHeight / 2;
1457
- }
1458
1406
  function renderWordByWordFancy(ctx, layer, canvasWidth, canvasHeight, relativeFrame, fps = 30) {
1459
1407
  const fontConfig = layer.fontConfig?.textStyle;
1460
1408
  if (!fontConfig) return;
@@ -1482,7 +1430,11 @@ function renderWordByWordFancy(ctx, layer, canvasWidth, canvasHeight, relativeFr
1482
1430
  const wordPositions = [];
1483
1431
  let wordIndex = 0;
1484
1432
  const totalHeight = lines.length * fontSize * lineHeight;
1485
- const startY = calculateYPosition(canvasHeight, totalHeight, layer.fontConfig?.globalPosition);
1433
+ const startY = calculateCaptionStartY(
1434
+ canvasHeight,
1435
+ totalHeight,
1436
+ layer.fontConfig?.globalPosition
1437
+ );
1486
1438
  for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
1487
1439
  const line = lines[lineIndex];
1488
1440
  const lineWords = line.split(needsSpace ? /\s+/ : "");
@@ -2316,6 +2268,9 @@ class VideoComposer {
2316
2268
  fontsLoadingPromise = null;
2317
2269
  loadedFonts = /* @__PURE__ */ new Set();
2318
2270
  static FONT_LOAD_TIMEOUT_MS = 1e4;
2271
+ // 导出耗时埋点:累计合成(含字幕逐帧绘制)耗时与帧数,由 export.worker 在 flush 时汇总打印。
2272
+ traceComposeMs = 0;
2273
+ traceComposeFrames = 0;
2319
2274
  constructor(config) {
2320
2275
  this.config = this.applyDefaults(config);
2321
2276
  this.frameDurationUs = Math.round(1e6 / this.config.fps);
@@ -2383,7 +2338,10 @@ class VideoComposer {
2383
2338
  const stream = new TransformStream(
2384
2339
  {
2385
2340
  transform: async (request, controller) => {
2341
+ const t0 = performance.now();
2386
2342
  const result = await this.composeFrame(request);
2343
+ this.traceComposeMs += performance.now() - t0;
2344
+ this.traceComposeFrames++;
2387
2345
  if (result.frame) {
2388
2346
  controller.enqueue(result.frame);
2389
2347
  }
@@ -2403,6 +2361,15 @@ class VideoComposer {
2403
2361
  );
2404
2362
  return stream;
2405
2363
  }
2364
+ /** 重置合成耗时埋点(每次导出开始时由 export.worker 调用)。 */
2365
+ resetTraceStats() {
2366
+ this.traceComposeMs = 0;
2367
+ this.traceComposeFrames = 0;
2368
+ }
2369
+ /** 读取累计合成耗时埋点(导出结束时由 export.worker 汇总)。 */
2370
+ getTraceStats() {
2371
+ return { composeMs: this.traceComposeMs, composeFrames: this.traceComposeFrames };
2372
+ }
2406
2373
  async composeFrame(request) {
2407
2374
  if (this.fontsLoadingPromise) {
2408
2375
  try {
@@ -2578,6 +2545,10 @@ class BaseEncoder {
2578
2545
  encoder;
2579
2546
  config;
2580
2547
  controller = null;
2548
+ // 导出耗时埋点:WebCodecs encode() 异步非阻塞,编码真实开销体现在「背压等待」与「flush 排空」。
2549
+ traceBackpressureMs = 0;
2550
+ traceFlushMs = 0;
2551
+ traceFrames = 0;
2581
2552
  constructor(config) {
2582
2553
  this.config = config;
2583
2554
  }
@@ -2625,6 +2596,11 @@ class BaseEncoder {
2625
2596
  console.warn("Codec not supported:", JSON.stringify(this.config));
2626
2597
  throw new Error(`Codec not supported: ${JSON.stringify(this.config)}`);
2627
2598
  }
2599
+ const req = this.config;
2600
+ const resolvedCfg = isSupported.config ?? this.config;
2601
+ console.info(
2602
+ `[Encoder] [trace:total] configured codec=${resolvedCfg.codec ?? req.codec} requestedHardwareAcceleration=${req.hardwareAcceleration ?? "no-preference"} resolvedHardwareAcceleration=${resolvedCfg.hardwareAcceleration ?? "unknown"} bitrate=${req.bitrate ?? "default"} ${req.width ?? "?"}x${req.height ?? "?"}`
2603
+ );
2628
2604
  this.encoder = this.createEncoder({
2629
2605
  output: this.handleOutput.bind(this),
2630
2606
  error: this.handleError.bind(this)
@@ -2736,6 +2712,7 @@ class BaseEncoder {
2736
2712
  await this.initialize();
2737
2713
  }
2738
2714
  if (this.encoder && this.encoder.encodeQueueSize >= this.encodeQueueThreshold) {
2715
+ const bp0 = performance.now();
2739
2716
  await new Promise((resolve) => {
2740
2717
  const check = () => {
2741
2718
  if (!this.encoder || this.encoder.encodeQueueSize < this.encodeQueueThreshold - 1) {
@@ -2746,6 +2723,7 @@ class BaseEncoder {
2746
2723
  };
2747
2724
  check();
2748
2725
  });
2726
+ this.traceBackpressureMs += performance.now() - bp0;
2749
2727
  }
2750
2728
  if (!this.isReady) {
2751
2729
  console.warn("Encoder is not ready, initializing 2...");
@@ -2757,9 +2735,12 @@ class BaseEncoder {
2757
2735
  }
2758
2736
  const frame = input.frame || input;
2759
2737
  this.encode(frame);
2738
+ this.traceFrames++;
2760
2739
  },
2761
2740
  flush: async () => {
2741
+ const flush0 = performance.now();
2762
2742
  await this.flush();
2743
+ this.traceFlushMs += performance.now() - flush0;
2763
2744
  }
2764
2745
  },
2765
2746
  // Queuing strategy with backpressure configuration
@@ -2770,6 +2751,20 @@ class BaseEncoder {
2770
2751
  }
2771
2752
  );
2772
2753
  }
2754
+ /** 重置编码耗时埋点(每次导出开始时由 export.worker 调用)。 */
2755
+ resetTraceStats() {
2756
+ this.traceBackpressureMs = 0;
2757
+ this.traceFlushMs = 0;
2758
+ this.traceFrames = 0;
2759
+ }
2760
+ /** 读取累计编码耗时埋点(导出结束时由 export.worker 汇总)。 */
2761
+ getTraceStats() {
2762
+ return {
2763
+ backpressureMs: this.traceBackpressureMs,
2764
+ flushMs: this.traceFlushMs,
2765
+ frames: this.traceFrames
2766
+ };
2767
+ }
2773
2768
  }
2774
2769
  class VideoChunkEncoder extends BaseEncoder {
2775
2770
  static DEFAULT_HIGH_WATER_MARK = 2;
@@ -2797,7 +2792,10 @@ class VideoChunkEncoder extends BaseEncoder {
2797
2792
  }
2798
2793
  async isConfigSupported(config) {
2799
2794
  const result = await VideoEncoder.isConfigSupported(config);
2800
- return { supported: result.supported ?? false };
2795
+ return {
2796
+ supported: result.supported ?? false,
2797
+ config: result.config
2798
+ };
2801
2799
  }
2802
2800
  createEncoder(init) {
2803
2801
  return new VideoEncoder(init);
@@ -3345,6 +3343,8 @@ class ExportWorker {
3345
3343
  this.composer = new VideoComposer(compose);
3346
3344
  this.encoder = new VideoChunkEncoder(encode);
3347
3345
  await this.encoder.initialize();
3346
+ this.composer.resetTraceStats();
3347
+ this.encoder.resetTraceStats();
3348
3348
  this.channel.state = WorkerState.Ready;
3349
3349
  return { success: true };
3350
3350
  }
@@ -3483,6 +3483,14 @@ class ExportWorker {
3483
3483
  if (this.encoder) {
3484
3484
  await this.encoder.flush();
3485
3485
  }
3486
+ if (this.composer && this.encoder) {
3487
+ const c = this.composer.getTraceStats();
3488
+ const e = this.encoder.getTraceStats();
3489
+ const msPerFrame = c.composeFrames > 0 ? (c.composeMs / c.composeFrames).toFixed(1) : "0";
3490
+ console.info(
3491
+ `[ExportWorker] [trace:total] compose ${(c.composeMs / 1e3).toFixed(2)}s (${c.composeFrames}f, ${msPerFrame}ms/f) | encode ${e.frames}f backpressure ${(e.backpressureMs / 1e3).toFixed(2)}s flush-drain ${(e.flushMs / 1e3).toFixed(2)}s`
3492
+ );
3493
+ }
3486
3494
  return { success: true };
3487
3495
  }
3488
3496
  async handleDispose() {
@@ -3548,4 +3556,4 @@ const export_worker = null;
3548
3556
  export {
3549
3557
  export_worker as default
3550
3558
  };
3551
- //# sourceMappingURL=export.worker.CWPrx2Nf.js.map
3559
+ //# sourceMappingURL=export.worker.CO7nW1W0.js.map