Skip to main content

Quick start

This guide will help you transcribe your first audio file using Tafrigh.

Initialize the library

First, import and initialize Tafrigh with your Wit.ai API key:
import { init, transcribe } from 'tafrigh';

// Initialize with your Wit.ai API key
init({ apiKeys: ['your-wit-ai-key'] });
You can provide multiple API keys to enable concurrent processing and avoid rate limits:
init({ apiKeys: ['key1', 'key2', 'key3'] });

Transcribe an audio file

Now you can transcribe any audio or video file supported by FFmpeg:
import { init, transcribe } from 'tafrigh';

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

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

Understanding the output

The transcribe function returns an array of segments with timestamps:
[
  {
    text: "Hello world",
    start: 0,
    end: 2.5,
    confidence: 0.95,
    tokens: [
      { text: "Hello", start: 0, end: 1.2, confidence: 0.98 },
      { text: "world", start: 1.3, end: 2.5, confidence: 0.92 }
    ]
  },
  {
    text: "This is a test",
    start: 2.7,
    end: 4.2
  }
]
Each segment contains:
  • text - The transcribed text
  • start - Start time in seconds
  • end - End time in seconds
  • confidence (optional) - Confidence score between 0 and 1
  • tokens (optional) - Word-by-word breakdown with individual timestamps

Add progress tracking

Monitor transcription progress with callbacks:
import { init, transcribe } from 'tafrigh';

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

const transcript = await transcribe('./audio.mp3', {
  callbacks: {
    onPreprocessingStarted: async (filePath) => {
      console.log(`Preprocessing ${filePath}...`);
    },
    onPreprocessingProgress: async (percent) => {
      console.log(`Preprocessing: ${percent}% complete`);
    },
    onSplittingStarted: async (totalChunks) => {
      console.log(`Splitting into ${totalChunks} chunks...`);
    },
    onTranscriptionStarted: async (totalChunks) => {
      console.log(`Transcribing ${totalChunks} chunks...`);
    },
    onTranscriptionProgress: async (chunkIndex) => {
      console.log(`Transcribed chunk ${chunkIndex}`);
    },
    onTranscriptionFinished: async (transcripts) => {
      console.log(`Completed! Got ${transcripts.length} segments`);
    }
  }
});

Enable logging

Use any logger that implements the Logger interface:
import { init } from 'tafrigh';

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

Environment variables

You can also set API keys using environment variables:
export WIT_AI_API_KEYS="key1 key2 key3"
Then initialize without passing keys explicitly:
import { init, transcribe } from 'tafrigh';

// API keys will be loaded from WIT_AI_API_KEYS environment variable
init({ apiKeys: process.env.WIT_AI_API_KEYS.split(' ') });

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

Complete example

Here’s a complete example that puts it all together:
import { init, transcribe } from 'tafrigh';

// Initialize with multiple API keys for better performance
init({
  apiKeys: ['key1', 'key2', 'key3'],
  logger: console
});

try {
  const transcript = await transcribe('https://example.com/podcast.mp3', {
    // Use up to 5 concurrent workers
    concurrency: 5,
    
    // Configure audio splitting
    splitOptions: {
      chunkDuration: 60, // 60-second chunks
      silenceDetection: {
        silenceThreshold: -30,
        silenceDuration: 0.5
      }
    },
    
    // Configure noise reduction
    preprocessOptions: {
      noiseReduction: {
        dialogueEnhance: true,
        highpass: 200,
        lowpass: 3000
      }
    },
    
    // Track progress
    callbacks: {
      onTranscriptionProgress: async (chunkIndex) => {
        console.log(`Transcribed chunk ${chunkIndex}`);
      },
      onTranscriptionFinished: async (transcripts) => {
        console.log(`Done! Got ${transcripts.length} segments`);
      }
    }
  });
  
  // Use the transcript
  const fullText = transcript.map(segment => segment.text).join(' ');
  console.log('Full transcript:', fullText);
  
} catch (error) {
  console.error('Transcription failed:', error);
}

Next steps

Advanced configuration

Learn about all available configuration options

Error handling

Handle failures and resume failed transcriptions

Noise reduction

Optimize audio preprocessing for better accuracy

API reference

Explore the complete API documentation

Build docs developers (and LLMs) love