diff --git a/src/lib/exporter/gifExporter.ts b/src/lib/exporter/gifExporter.ts index 747e34e..58ed693 100644 --- a/src/lib/exporter/gifExporter.ts +++ b/src/lib/exporter/gifExporter.ts @@ -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); diff --git a/src/lib/exporter/streamingDecoder.ts b/src/lib/exporter/streamingDecoder.ts index ee67576..c028832 100644 --- a/src/lib/exporter/streamingDecoder.ts +++ b/src/lib/exporter/streamingDecoder.ts @@ -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( diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts index d0affd1..dcfcc3e 100644 --- a/src/lib/exporter/videoExporter.ts +++ b/src/lib/exporter/videoExporter.ts @@ -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");