Skip to main content

Overview

The Callbacks interface allows you to monitor and respond to various stages of the transcription process. It extends preprocessing and splitting callbacks from ffmpeg-simplified, providing a complete view of the entire audio processing pipeline.

Type definition

interface Callbacks extends PreprocessingCallbacks, SplitOnSilenceCallbacks {
    onTranscriptionFinished?: (transcripts: Segment[]) => Promise<void>;
    onTranscriptionProgress?: (chunkIndex: number) => void;
    onTranscriptionStarted?: (totalChunks: number) => Promise<void>;
}

Properties

Transcription callbacks

onTranscriptionStarted
(totalChunks: number) => Promise<void>
Called just before the transcription process begins.Parameters:
  • totalChunks: Total number of chunks to be transcribed
Use this to initialize progress tracking or logging.
onTranscriptionProgress
(chunkIndex: number) => void
Called when each individual chunk is transcribed.Parameters:
  • chunkIndex: Index of the current chunk being processed (0-based)
Use this to update progress indicators or track completion status.
onTranscriptionFinished
(transcripts: Segment[]) => Promise<void>
Called when all transcription is complete.Parameters:
  • transcripts: Array of all transcribed segments
This callback is only called when ALL chunks transcribe successfully. If any chunk fails, this callback will not be invoked.

Inherited callbacks

Usage examples

import { transcribe } from 'tafrigh';

const transcript = await transcribe('audio.mp3', {
  callbacks: {
    onTranscriptionStarted: async (totalChunks) => {
      console.log(`Starting transcription of ${totalChunks} chunks`);
    },
    onTranscriptionProgress: (chunkIndex) => {
      console.log(`Completed chunk ${chunkIndex + 1}`);
    },
    onTranscriptionFinished: async (transcripts) => {
      console.log(`Done! ${transcripts.length} segments transcribed`);
    }
  }
});

Error handling

When errors occur during transcription, the onTranscriptionFinished callback will not be called. Handle errors using try-catch:
import { transcribe, TranscriptionError } from 'tafrigh';

try {
  const transcript = await transcribe('audio.mp3', {
    callbacks: {
      onTranscriptionFinished: async (transcripts) => {
        // This is only called on complete success
        console.log('Success!');
      }
    }
  });
} catch (error) {
  if (error instanceof TranscriptionError) {
    // Some chunks may have succeeded
    console.log(`Partial results: ${error.transcripts.length} segments`);
    console.log(`Failed chunks: ${error.failures.length}`);
  }
}

TranscribeOptions

Configuration options including callbacks

Segment

Type passed to onTranscriptionFinished

Progress callbacks guide

Complete guide to using callbacks

Error handling

Learn about error handling strategies

Build docs developers (and LLMs) love