fix: export frame counter exceeding total frames

This commit is contained in:
Theodor Peifer
2026-04-10 22:24:37 +02:00
parent 68295b21ec
commit d21dd1cbf1
3 changed files with 20 additions and 7 deletions
+2 -2
View File
@@ -174,11 +174,11 @@ export class GifExporter {
});
// Calculate effective duration and frame count (excluding trim regions)
const effectiveDuration = this.streamingDecoder.getEffectiveDuration(
const { effectiveDuration, totalFrames } = this.streamingDecoder.getExportMetrics(
this.config.frameRate,
this.config.trimRegions,
this.config.speedRegions,
);
const totalFrames = Math.ceil(effectiveDuration * this.config.frameRate);
// Calculate frame delay in milliseconds (gif.js uses ms)
const frameDelay = Math.round(1000 / this.config.frameRate);
+16 -3
View File
@@ -536,11 +536,24 @@ export class StreamingVideoDecoder {
return segments;
}
getEffectiveDuration(trimRegions?: TrimRegion[], speedRegions?: SpeedRegion[]): number {
getExportMetrics(
targetFrameRate: number,
trimRegions?: TrimRegion[],
speedRegions?: SpeedRegion[],
): { effectiveDuration: number; totalFrames: number } {
if (!this.metadata) throw new Error("Must call loadMetadata() first");
const trimSegments = this.computeSegments(this.metadata.duration, trimRegions);
const speedSegments = this.splitBySpeed(trimSegments, speedRegions);
return speedSegments.reduce((sum, seg) => sum + (seg.endSec - seg.startSec) / seg.speed, 0);
const segments = this.splitBySpeed(trimSegments, speedRegions);
return {
effectiveDuration: segments.reduce(
(sum, seg) => sum + (seg.endSec - seg.startSec) / seg.speed,
0,
),
totalFrames: segments.reduce(
(sum, seg) => sum + Math.ceil(((seg.endSec - seg.startSec) / seg.speed) * targetFrameRate),
0,
),
};
}
private splitBySpeed(
+2 -2
View File
@@ -157,11 +157,11 @@ export class VideoExporter {
this.muxer = muxer;
await muxer.initialize();
const effectiveDuration = streamingDecoder.getEffectiveDuration(
const { effectiveDuration, totalFrames } = streamingDecoder.getExportMetrics(
this.config.frameRate,
this.config.trimRegions,
this.config.speedRegions,
);
const totalFrames = Math.ceil(effectiveDuration * this.config.frameRate);
const readEndSec = Math.max(videoInfo.duration, videoInfo.streamDuration ?? 0) + 0.5;
console.log("[VideoExporter] Original duration:", videoInfo.duration, "s");