Calculating Audio Duration from PCM Buffer
Calculate the exact duration of raw PCM audio for frame-precise video synchronization.
Duration in Seconds
function calculateDurationInSeconds(pcmBuffer: Buffer, sampleRate: number, channels: number, bytesPerSample: number): number {
const bytesPerSecond = sampleRate * channels * bytesPerSample;
return pcmBuffer.length / bytesPerSecond;
}
// Example: Gemini TTS audio
const duration = calculateDurationInSeconds(pcmBuffer, 24000, 1, 2);
Duration in Frames (Remotion)
For video synchronization at 30 FPS:
function calculateDurationInFrames(pcmBuffer: Buffer, fps: number = 30): number {
const AUDIO_CONFIG = {
SAMPLE_RATE: 24000,
CHANNELS: 1,
BYTES_PER_SAMPLE: 2,
};
const durationInSeconds = pcmBuffer.length /
(AUDIO_CONFIG.SAMPLE_RATE * AUDIO_CONFIG.CHANNELS * AUDIO_CONFIG.BYTES_PER_SAMPLE);
return Math.round(durationInSeconds * fps);
}
Complete Audio Service
class AudioService {
private readonly SAMPLE_RATE = 24000;
private readonly CHANNELS = 1;
private readonly BYTES_PER_SAMPLE = 2;
private readonly FPS = 30;
calculateDurationInSeconds(pcmBuffer: Buffer): number {
const bytesPerSecond = this.SAMPLE_RATE * this.CHANNELS * this.BYTES_PER_SAMPLE;
return pcmBuffer.length / bytesPerSecond;
}
calculateDurationInFrames(pcmBuffer: Buffer): number {
const durationInSeconds = this.calculateDurationInSeconds(pcmBuffer);
return Math.round(durationInSeconds * this.FPS);
}
formatDuration(frames: number): string {
const seconds = frames / this.FPS;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
}
}
Example Calculations
| Buffer Size | Duration (seconds) | Frames (30 FPS) |
|---|---|---|
| 48,000 | 1.0 | 30 |
| 96,000 | 2.0 | 60 |
| 144,000 | 3.0 | 90 |
| 240,000 | 5.0 | 150 |
| 480,000 | 10.0 | 300 |
Formula Breakdown
duration (seconds) = bufferLength / (sampleRate × channels × bytesPerSample)
duration (frames) = duration (seconds) × FPS
For Gemini TTS (24kHz, mono, 16-bit):
duration (seconds) = bufferLength / (24000 × 1 × 2)
= bufferLength / 48000
duration (frames) = (bufferLength / 48000) × 30
= bufferLength / 1600
Usage in Video Generation
// After generating audio
const pcmBuffer = await generateAudio(narration);
const audioFrames = audioService.calculateDurationInFrames(pcmBuffer);
// Scene duration = audio frames + cross-fade frames
const scene = {
durationInFrames: audioFrames + 30, // Add 1-second cross-fade
audioUuid: saveAudio(pcmToWav(pcmBuffer)),
};