asciify-engine 1.0.49 → 1.0.50

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.
package/dist/index.cjs CHANGED
@@ -694,15 +694,15 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
694
694
  }
695
695
  return { frame, cols, rows };
696
696
  }
697
- async function videoToAsciiFrames(video, options, targetWidth, targetHeight, targetFps = 12, maxDuration = 10, onProgress) {
698
- const duration = Math.min(video.duration, maxDuration);
697
+ async function videoToAsciiFrames(video, options, targetWidth, targetHeight, targetFps = 12, maxDuration = 10, onProgress, startTime = 0) {
698
+ const duration = Math.min(video.duration - startTime, maxDuration);
699
699
  const totalFrames = Math.ceil(duration * targetFps);
700
700
  const frames = [];
701
701
  let cols = 0;
702
702
  let rows = 0;
703
703
  for (let i = 0; i < totalFrames; i++) {
704
- const time = i / targetFps;
705
- if (time > duration) break;
704
+ const time = startTime + i / targetFps;
705
+ if (time > startTime + duration) break;
706
706
  video.currentTime = time;
707
707
  await new Promise((resolve) => {
708
708
  const handler = () => {
@@ -1089,7 +1089,9 @@ async function asciifyGif(source, canvas, { fontSize = 10, artStyle = "classic",
1089
1089
  cancelAnimationFrame(animId);
1090
1090
  };
1091
1091
  }
1092
- async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic", options = {}, fitTo, preExtract = false, onReady, onFrame } = {}) {
1092
+ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic", options = {}, fitTo, preExtract = false, trim, onReady, onFrame } = {}) {
1093
+ const trimStart = trim?.start ?? 0;
1094
+ const trimEnd = trim?.end;
1093
1095
  const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[artStyle], ...options, fontSize };
1094
1096
  const ctx = canvas.getContext("2d");
1095
1097
  if (!ctx) throw new Error("asciifyVideo: could not get 2d context from canvas.");
@@ -1110,8 +1112,8 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1110
1112
  video2 = source;
1111
1113
  }
1112
1114
  if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight);
1113
- onReady?.(video2);
1114
- const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height);
1115
+ const maxDur = trimEnd !== void 0 ? trimEnd - trimStart : 10;
1116
+ const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height, void 0, maxDur, void 0, trimStart);
1115
1117
  let cancelled2 = false, animId2, i = 0, last = performance.now();
1116
1118
  let firstFrame2 = true;
1117
1119
  const interval = 1e3 / fps;
@@ -1167,6 +1169,23 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1167
1169
  if (video.paused) await video.play().catch(() => {
1168
1170
  });
1169
1171
  }
1172
+ if (trimStart > 0) {
1173
+ video.currentTime = trimStart;
1174
+ await new Promise((resolve) => {
1175
+ const h = () => {
1176
+ video.removeEventListener("seeked", h);
1177
+ resolve();
1178
+ };
1179
+ video.addEventListener("seeked", h);
1180
+ });
1181
+ }
1182
+ let timeupdateHandler = null;
1183
+ if (trimEnd !== void 0) {
1184
+ timeupdateHandler = () => {
1185
+ if (video.currentTime >= trimEnd) video.currentTime = trimStart;
1186
+ };
1187
+ video.addEventListener("timeupdate", timeupdateHandler);
1188
+ }
1170
1189
  let ro = null;
1171
1190
  if (container) {
1172
1191
  const aspect = video.videoWidth / video.videoHeight;
@@ -1196,6 +1215,7 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1196
1215
  cancelled = true;
1197
1216
  cancelAnimationFrame(animId);
1198
1217
  ro?.disconnect();
1218
+ if (timeupdateHandler) video.removeEventListener("timeupdate", timeupdateHandler);
1199
1219
  if (ownedVideo) {
1200
1220
  video.pause();
1201
1221
  video.src = "";