Skip to main content

Function signature

async function transcribe(
  content: string | ReadStream,
  options?: TranscribeOptions
): Promise<Segment[]>
Transcribes audio content and returns an array of transcript segments with timestamps. This function handles preprocessing the audio, splitting it into chunks, and transcribing each chunk using Wit.ai’s API.

Parameters

content
string | ReadStream
required
The audio content to transcribe. Can be:
  • A file path (e.g., 'path/to/audio.mp3')
  • A URL to a remote audio file
  • A Node.js ReadStream object
options
TranscribeOptions
Configuration options for transcription

Returns

segments
Promise<Segment[]>
A promise that resolves to an array of transcript segments. Each segment contains:

Throws

TranscriptionError
Error
Thrown when one or more chunks fail to transcribe. The error object contains:
  • failures: Array of failed transcription attempts with error details
  • transcripts: Successfully transcribed segments
  • outputDir: Path to the temporary directory containing audio chunks
  • chunkFiles: Array of all audio chunk files
You can use resumeFailedTranscriptions() to retry failed chunks.
Error
Error
Thrown if options validation fails (e.g., invalid chunkDuration values).

Examples

Basic transcription

import { init, transcribe } from 'tafrigh';

init({ apiKeys: ['your-wit-ai-key'] });

const transcript = await transcribe('path/to/audio.mp3');

console.log(transcript);
// [
//   { text: "Hello world", start: 0, end: 2.5, confidence: 0.98 },
//   { text: "This is a test", start: 2.5, end: 5.0, confidence: 0.95 }
// ]

Concurrent transcription with multiple API keys

import { init, transcribe } from 'tafrigh';

init({
  apiKeys: ['key-1', 'key-2', 'key-3']
});

const transcript = await transcribe('large-audio-file.mp3', {
  concurrency: 3,
  splitOptions: {
    chunkDuration: 60
  }
});

Custom chunk duration and silence detection

const transcript = await transcribe('podcast.mp3', {
  splitOptions: {
    chunkDuration: 120,
    silenceDetection: {
      silenceThreshold: -35
    }
  }
});

Progress tracking with callbacks

let totalChunks = 0;
let processedChunks = 0;

const transcript = await transcribe('audio.mp3', {
  callbacks: {
    onTranscriptionStarted: async (total) => {
      totalChunks = total;
      console.log(`Starting transcription of ${total} chunks`);
    },
    onTranscriptionProgress: (index) => {
      processedChunks++;
      console.log(`Progress: ${processedChunks}/${totalChunks} chunks`);
    },
    onTranscriptionFinished: async (transcripts) => {
      console.log(`Completed! Total segments: ${transcripts.length}`);
    }
  }
});

Error handling and retry

import { init, transcribe, resumeFailedTranscriptions, TranscriptionError } from 'tafrigh';

init({ apiKeys: ['your-wit-ai-key'] });

try {
  const transcript = await transcribe('audio.mp3', {
    preventCleanup: true // Keep chunks for potential retry
  });
  console.log('Success:', transcript);
} catch (error) {
  if (error instanceof TranscriptionError) {
    console.log(`Failed chunks: ${error.failures.length}`);
    console.log(`Successful chunks: ${error.transcripts.length}`);
    
    // Retry failed chunks
    const result = await resumeFailedTranscriptions(error, {
      concurrency: 1
    });
    
    if (result.failures.length === 0) {
      console.log('All chunks transcribed successfully!');
      console.log(result.transcripts);
    }
  } else {
    throw error;
  }
}

Transcribing from a stream

import { createReadStream } from 'fs';
import { transcribe } from 'tafrigh';

const stream = createReadStream('audio.mp3');
const transcript = await transcribe(stream);

Transcribing from a URL

const transcript = await transcribe('https://example.com/audio.mp3');
The transcribe() function automatically cleans up temporary files after completion. Set preventCleanup: true if you need to preserve chunks for debugging or retry operations.
Wit.ai has rate limits and file size restrictions. Use multiple API keys with higher concurrency to improve throughput for large files.

Build docs developers (and LLMs) love