Skip to content

Commit da1bfac

Browse files
committed
fix(renderer): keep the full video length when audio ends early
-shortest cut the output at the end of the mixed audio, so a composition with a silent ending lost its last frames. Pad the mix with apad and bound the output with -t <frames / fps> instead. Also resample the audio to 48 kHz (low-rate sources such as 24 kHz TTS MP3s were passed through as is), and leave atrim open when a clip has no finite duration rather than emitting duration=Infinity.
1 parent 6d6aca2 commit da1bfac

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

‎packages/renderer/src/stitch-frames-to-video.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export async function stitchFramesToVideo(options: StitchOptions): Promise<void>
5656
const ext = imageFormat === 'jpeg' ? 'jpeg' : 'png';
5757
const inputPattern = path.join(framesDir, `frame-%06d.${ext}`);
5858

59+
// The video length is set by the rendered frames; audio is padded or cut to it.
60+
const frameFilePattern = new RegExp(`^frame-\\d{6}\\.${ext}$`);
61+
const frameCount = fs.readdirSync(framesDir).filter((f) => frameFilePattern.test(f)).length;
62+
const videoDurationSec = frameCount / fps;
63+
5964
// Resolve audio sources to absolute file paths, filtering out missing files
6065
const resolvedAudio: Array<AudioSourceInfo & { filePath: string }> = [];
6166
if (assetsDir && audioSources.length > 0) {
@@ -107,8 +112,11 @@ export async function stitchFramesToVideo(options: StitchOptions): Promise<void>
107112
// Build per-source filter chain: trim → atempo → adelay → volume
108113
const filters: string[] = [];
109114

110-
// Trim: skip into source file and limit duration
111-
filters.push(`atrim=start=${startFromSec.toFixed(6)}:duration=${durationSec.toFixed(6)}`);
115+
// Trim: skip into source file and limit duration. An unbounded clip
116+
// is left open; -t on the output cuts it at the end of the video.
117+
filters.push(Number.isFinite(durationSec)
118+
? `atrim=start=${startFromSec.toFixed(6)}:duration=${durationSec.toFixed(6)}`
119+
: `atrim=start=${startFromSec.toFixed(6)}`);
112120
filters.push('asetpts=PTS-STARTPTS');
113121

114122
// Playback rate adjustment via atempo (supports 0.5–100, chain for wider range)
@@ -131,17 +139,13 @@ export async function stitchFramesToVideo(options: StitchOptions): Promise<void>
131139
mixInputs.push(`[${label}]`);
132140
}
133141

134-
// Mix all audio tracks together
142+
// Mix all audio tracks together, then pad with silence so audio that
143+
// ends early does not shorten the video
135144
if (mixInputs.length === 1) {
136-
filterGraph = `${filterParts[0].replace(`[${mixInputs[0].slice(1, -1)}]`, '[aout]')}`.replace(
137-
filterParts[0],
138-
filterParts[0]
139-
);
140-
// Simpler: just rename the output label
141-
filterGraph = filterParts[0].slice(0, filterParts[0].lastIndexOf('[')) + '[aout]';
145+
filterGraph = filterParts[0] + `;${mixInputs[0]}apad[aout]`;
142146
} else {
143147
filterGraph = filterParts.join(';')
144-
+ `;${mixInputs.join('')}amix=inputs=${mixInputs.length}:duration=longest:normalize=0[aout]`;
148+
+ `;${mixInputs.join('')}amix=inputs=${mixInputs.length}:duration=longest:normalize=0,apad[aout]`;
145149
}
146150

147151
args.push('-filter_complex', filterGraph);
@@ -156,18 +160,21 @@ export async function stitchFramesToVideo(options: StitchOptions): Promise<void>
156160
}
157161
args.push('-pix_fmt', pixelFormat, '-movflags', '+faststart');
158162
if (resolvedAudio.length > 0) {
159-
args.push('-c:a', 'aac', '-b:a', '192k');
163+
args.push('-c:a', 'aac', '-b:a', '192k', '-ar', '48000');
160164
}
161165
} else {
162166
const encoder = videoEncoder ?? 'libvpx-vp9';
163167
args.push('-c:v', encoder, '-crf', String(crf), '-b:v', '0');
164168
if (resolvedAudio.length > 0) {
165-
args.push('-c:a', 'libopus', '-b:a', '128k');
169+
args.push('-c:a', 'libopus', '-b:a', '128k', '-ar', '48000');
166170
}
167171
}
168172

169-
// Ensure output duration matches the video (don't let audio extend it)
170-
args.push('-shortest');
173+
// Output duration matches the video: padded audio is cut here, and audio
174+
// that ends early no longer truncates the video (as -shortest did)
175+
if (resolvedAudio.length > 0) {
176+
args.push('-t', videoDurationSec.toFixed(6));
177+
}
171178

172179
args.push(outputPath);
173180

0 commit comments

Comments
 (0)