/

Deepgram WAV Transcription - Speech-to-Text API

Copy script

Copied

Copy script

Copied

Deepgram WAV Transcription - Speech-to-Text API

Transcribe any WAV file from a URL using Deepgram's powerful speech-to-text API. Features include speaker diarization, smart formatting, punctuation, multiple language support, and detailed analytics. Perfect for podcast transcription, meeting notes, interview processing, and audio content analysis. Returns full transcript with word-level timings and speaker identification.

Created by

andrew van beek - Keyboard Team

Requirements

deepgram.com logo

KEYBOARD_DEEPGRAM_API_KEY

Script

Copy script

Copied

Copy script

Copied

const { createClient } = require('@deepgram/sdk');

// Get Deepgram API key from environment
const apiKey = process.env.KEYBOARD_DEEPGRAM_API_KEY;
if (!apiKey) {
    throw new Error('KEYBOARD_DEEPGRAM_API_KEY environment variable not found');
}

// Initialize Deepgram client
const deepgram = createClient(apiKey);

// Configure transcription options
const transcriptionOptions = {
    model: {{model}},
    language: {{language}},
    smart_format: {{smartFormat}},
    punctuation: {{punctuation}},
    diarize: {{diarize}},
    utterances: true,
    paragraphs: true
};

console.log('šŸŽ¤ Starting Deepgram transcription...');
console.log('šŸ“ Audio URL: {{audioUrl}}');
console.log('āš™ļø  Model: {{model}}, Language: {{language}}');

try {
    // Transcribe the audio from URL
    const { result, error } = await deepgram.listen.prerecorded.transcribeUrl(
        { url: '{{audioUrl}}' },
        transcriptionOptions
    );
    
    if (error) {
        throw new Error(`Transcription failed: ${error.message}`);
    }
    
    // Extract results
    const channel = result.results.channels[0];
    const alternative = channel.alternatives[0];
    const transcript = alternative.transcript;
    const words = alternative.words || [];
    const paragraphs = alternative.paragraphs || {};
    
    // Calculate statistics
    const wordCount = words.length;
    const duration = words.length > 0 ? words[words.length - 1].end - words[0].start : 0;
    const speakingRate = duration > 0 ? Math.round(wordCount / duration * 60) : 0;
    
    // Handle different output formats
    const outputFormat = '{{outputFormat}}';
    
    if (outputFormat === 'transcript-only') {
        console.log('\nšŸ“ Transcript:\n');
        console.log(transcript);
    } else if (outputFormat === 'full-json') {
        console.log('\nšŸ“„ Full JSON Response:\n');
        console.log(JSON.stringify(result, null, 2));
    } else {
        // Default: summary format
        console.log('\nāœ… Transcription Complete!\n');
        console.log('šŸ“Š Statistics:');
        console.log(`- Duration: ${duration.toFixed(2)} seconds`);
        console.log(`- Word count: ${wordCount} words`);
        console.log(`- Speaking rate: ${speakingRate} words/minute`);
        console.log(`- Model used: {{model}}`);
        console.log(`- Language: {{language}}`);
        
        console.log('\nšŸ“ Transcript:\n');
        console.log(transcript);
        
        // Show first few words with timestamps if available
        if (words.length > 0) {
            console.log('\nā±ļø  First 5 words with timestamps:');
            words.slice(0, 5).forEach(word => {
                console.log(`- ${word.word} (${word.start}s - ${word.end}s)`);
            });
        }
        
        // Show speaker segments if diarization was enabled
        if ({{diarize}} && paragraphs.paragraphs && paragraphs.paragraphs.length > 0) {
            console.log('\nšŸ‘„ Speaker segments detected:', paragraphs.paragraphs.length);
        }
    }
    
    // Return structured result for further processing if needed
    return {
        success: true,
        transcript: transcript,
        metadata: {
            url: '{{audioUrl}}',
            model: '{{model}}',
            language: '{{language}}',
            duration: duration,
            wordCount: wordCount,
            speakingRate: speakingRate
        },
        words: words,
        paragraphs: paragraphs,
        raw: result
    };
    
} catch (error) {
    console.error('\nāŒ Error:', error.message);
    throw error;
}