### Complete Audio Transcription Pipeline Source: https://context7.com/brooooooklyn/whisper-node/llms.txt An end-to-end example demonstrating the full audio transcription process using Whisper. This includes downloading the model, processing audio, performing transcription with error handling, and monitoring progress. The output is saved as a JSON file. ```javascript import { readFile, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; import { Whisper, WhisperFullParams, WhisperSamplingStrategy, decodeAudioAsync, setupLogger, WhisperLogLevel } from '@napi-rs/whisper'; async function transcribeAudio(modelPath, audioPath, outputPath) { try { // Configure logging setupLogger((level, message) => { if (level >= WhisperLogLevel.Warn) { console.log(`[Whisper] ${message}`); } }); // Load model console.log('Loading Whisper model...'); const modelBuffer = await readFile(modelPath); const whisper = new Whisper(modelBuffer, { useGpu: false, flashAttn: false }); console.log('Model loaded:', { isMultilingual: whisper.isMultilingual(), vocabularySize: whisper.modelNVocab(), audioContextSize: whisper.modelNAudioCtx() }); // Decode audio console.log('Decoding audio file...'); const audioFile = await readFile(audioPath); const audioBuffer = await decodeAudioAsync(audioFile, audioPath); console.log(`Audio decoded: ${audioBuffer.length} samples (${(audioBuffer.length / 16000).toFixed(2)}s)`); // Configure transcription const params = new WhisperFullParams(WhisperSamplingStrategy.Greedy); params.language = 'en'; params.nThreads = 4; params.printProgress = false; params.singleSegment = false; params.noTimestamps = false; const segments = []; params.onNewSegment = (segment) => { segments.push(segment); console.log(`[${segment.start.toFixed(2)}s - ${segment.end.toFixed(2)}s] ${segment.text}`); }; let lastProgress = 0; params.onProgress = (progress) => { const percent = Math.floor(progress * 100); if (percent > lastProgress) { console.log(`Progress: ${percent}%`); lastProgress = percent; } }; // Transcribe console.log('Starting transcription...'); const transcription = whisper.full(params, audioBuffer); // Save results const output = { text: transcription, segments: segments, metadata: { duration: audioBuffer.length / 16000, language: params.language, timestamp: new Date().toISOString() } }; await writeFile(outputPath, JSON.stringify(output, null, 2)); console.log(`Transcription saved to ${outputPath}`); return output; } catch (error) { console.error('Transcription failed:', error); throw error; } } // Usage await transcribeAudio( './ggml-large-v3-turbo.bin', './podcast.mp3', './transcript.json' ); ``` -------------------------------- ### Setup Custom Logger for Whisper Source: https://context7.com/brooooooklyn/whisper-node/llms.txt Configures a custom logging handler for Whisper to capture internal logs for debugging and monitoring. It maps log levels to human-readable names and allows for custom handling, such as logging to the console or sending errors to a service. ```javascript import { setupLogger, WhisperLogLevel } from '@napi-rs/whisper'; // Set up custom logger setupLogger((level, message) => { const levelNames = { [WhisperLogLevel.None]: 'NONE', [WhisperLogLevel.Info]: 'INFO', [WhisperLogLevel.Warn]: 'WARN', [WhisperLogLevel.Error]: 'ERROR', [WhisperLogLevel.Debug]: 'DEBUG', [WhisperLogLevel.Cont]: 'CONT' }; const timestamp = new Date().toISOString(); const levelName = levelNames[level] || 'UNKNOWN'; console.log(`[${timestamp}] [${levelName}] ${message}`); // Log to file or external service if (level === WhisperLogLevel.Error) { // Send to error tracking service } }); console.log('Custom logger configured'); ``` -------------------------------- ### Initialize Whisper Instance with GPU Acceleration (JavaScript) Source: https://context7.com/brooooooklyn/whisper-node/llms.txt Initializes a Whisper instance by loading a pre-trained GGML model. Supports loading from a buffer or file path and configuring GPU acceleration parameters like device index and flash attention. ```javascript import { readFile } from 'node:fs/promises'; import { Whisper } from '@napi-rs/whisper'; // Load model from file const modelBuffer = await readFile('./ggml-large-v3-turbo.bin'); // Create Whisper instance with GPU acceleration const whisper = new Whisper(modelBuffer, { useGpu: true, gpuDevice: 0, flashAttn: false, dtwTokenTimestamps: false, dtwAheadsPreset: 0, dtwNTop: 4 }); // Access model properties console.log('Model vocabulary size:', whisper.modelNVocab()); console.log('Is multilingual:', whisper.isMultilingual()); console.log('Model type:', whisper.modelType()); ``` -------------------------------- ### Download Whisper Model (Bash) Source: https://github.com/brooooooklyn/whisper-node/blob/main/README.md This script downloads a specified Whisper GGML model. It requires the model name as an argument. ```bash ./scripts/download-ggml-model.sh large-v3-turbo ``` -------------------------------- ### Configure Whisper Transcription Parameters Source: https://context7.com/brooooooklyn/whisper-node/llms.txt The WhisperFullParams class allows comprehensive configuration of transcription behavior, including sampling strategies (Greedy, BeamSearch), language settings, output control, and performance tuning. It provides fine-grained control over various aspects of the transcription process. ```javascript import { WhisperFullParams, WhisperSamplingStrategy } from '@napi-rs/whisper'; // Create params with Greedy sampling const greedyParams = new WhisperFullParams(WhisperSamplingStrategy.Greedy); // Language and translation settings greedyParams.language = 'en'; // Target language code greedyParams.translate = false; // Translate to English greedyParams.detectLanguage = false; // Auto-detect language // Output control greedyParams.singleSegment = false; // Output as single segment greedyParams.printProgress = true; // Log progress to console greedyParams.printRealtime = false; // Real-time output greedyParams.printTimestamps = true; // Include timestamps greedyParams.noTimestamps = false; // Disable timestamps // Performance settings greedyParams.nThreads = 4; // Number of threads greedyParams.offsetMs = 0; // Start offset in milliseconds greedyParams.durationMs = 0; // Process duration (0 = all) // Advanced settings greedyParams.temperature = 0.0; // Sampling temperature greedyParams.maxLen = 0; // Max segment length (0 = auto) greedyParams.maxTokens = 0; // Max tokens per segment greedyParams.audioCtx = 0; // Audio context size greedyParams.suppressBlank = true; // Suppress blank outputs greedyParams.suppressNonSpeechTokens = false; greedyParams.initialPrompt = ''; // Initial prompt text greedyParams.suppressRegex = ''; // Regex to suppress tokens // BeamSearch alternative const beamParams = new WhisperFullParams(WhisperSamplingStrategy.BeamSearch); beamParams.language = 'en'; beamParams.nThreads = 4; console.log('Sampling strategy:', greedyParams.strategy); ``` -------------------------------- ### Speech to Text Conversion (JavaScript) Source: https://github.com/brooooooklyn/whisper-node/blob/main/README.md Converts an audio file to text using the Whisper model. It requires the Whisper model file and an audio file. Outputs the transcribed text and supports various configuration options for the transcription process. ```javascript import { readFile } from 'node:fs/promises' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { Whisper, WhisperFullParams, WhisperSamplingStrategy, decodeAudioAsync } from './index.js' const rootDir = join(fileURLToPath(import.meta.url), '..') const GGLM_LARGE = await readFile(join(rootDir, 'ggml-large-v3-turbo.bin')) const audio = await readFile(join(rootDir, '__test__/rolldown.wav')) const whisper = new Whisper(GGLM_LARGE) const audioBuffer = await decodeAudioAsync(audio, 'rolldown.wav') const whisperParams = new WhisperFullParams(WhisperSamplingStrategy.Greedy) whisperParams.language = 'en' whisperParams.printProgress = true whisperParams.singleSegment = false whisperParams.durationMs = 0 whisperParams.printRealtime = true whisperParams.onEncoderBegin = (state) => { console.info(Whisper.lang(state.fullLangId)) } whisperParams.onProgress = (progress) => { console.info(`Progress: ${progress}`) } whisperParams.onNewSegment = (segment) => { console.info(segment) } const output = whisper.full(whisperParams, audioBuffer) console.info(output) // Rolldown is a JavaScript/TypeScript bundler written in Rust intended to serve as the future bundler used in Vite. ``` -------------------------------- ### Perform Full Speech-to-Text Transcription (JavaScript) Source: https://context7.com/brooooooklyn/whisper-node/llms.txt Performs complete speech-to-text transcription on audio samples using configured parameters. Supports real-time progress callbacks, segment reporting, and language detection. ```javascript import { Whisper, WhisperFullParams, WhisperSamplingStrategy, decodeAudioAsync } from '@napi-rs/whisper'; import { readFile } from 'node:fs/promises'; const modelBuffer = await readFile('./ggml-large-v3-turbo.bin'); const audioFile = await readFile('./speech.wav'); const whisper = new Whisper(modelBuffer); const audioBuffer = await decodeAudioAsync(audioFile, 'speech.wav'); // Configure transcription parameters const params = new WhisperFullParams(WhisperSamplingStrategy.Greedy); params.language = 'en'; params.nThreads = 4; params.printProgress = true; params.printRealtime = true; params.singleSegment = false; params.translate = false; params.noTimestamps = false; params.temperature = 0.0; params.maxLen = 0; // Set up callbacks for real-time monitoring params.onEncoderBegin = (state) => { console.log('Encoder started, language ID:', state.fullLangId); console.log('Language:', Whisper.lang(state.fullLangId)); }; params.onProgress = (progress) => { console.log(`Transcription progress: ${(progress * 100).toFixed(2)}%`); }; params.onNewSegment = (segment) => { console.log(`[${segment.start}s - ${segment.end}s]: ${segment.text}`); }; // Execute transcription const transcription = whisper.full(params, audioBuffer); console.log('Final transcription:', transcription); ``` -------------------------------- ### Extract Audio from Video Files with whisper-node Source: https://context7.com/brooooooklyn/whisper-node/llms.txt The splitAudioFromVideo function extracts the audio track from video files using FFmpeg, returning PCM samples ready for transcription. It supports various video formats and allows for configurable logging levels. The extracted audio can then be transcribed using the Whisper model. ```javascript import { splitAudioFromVideo, AVLogLevel, Whisper, WhisperFullParams, WhisperSamplingStrategy } from '@napi-rs/whisper'; import { readFile, writeFile } from 'node:fs/promises'; const modelBuffer = await readFile('./ggml-large-v3-turbo.bin'); const whisper = new Whisper(modelBuffer); try { // Extract audio from video file with verbose logging const audioBuffer = splitAudioFromVideo( './video.mp4', AVLogLevel.Info ); console.log('Extracted audio samples:', audioBuffer.length); console.log('Duration:', (audioBuffer.length / 16000).toFixed(2), 'seconds'); // Transcribe extracted audio const params = new WhisperFullParams(WhisperSamplingStrategy.Greedy); params.language = 'en'; const transcription = whisper.full(params, audioBuffer); // Save transcription to file await writeFile('video-transcript.txt', transcription); console.log('Transcription saved'); } catch (error) { console.error('Video processing failed:', error); } ``` -------------------------------- ### Asynchronously Decode Audio for Whisper (JavaScript) Source: https://context7.com/brooooooklyn/whisper-node/llms.txt Decodes various audio formats (MP3, WAV, FLAC, OGG, etc.) into PCM Float32Array samples suitable for Whisper. Supports asynchronous operation and cancellation via AbortSignal. ```javascript import { decodeAudioAsync } from '@napi-rs/whisper'; import { readFile } from 'node:fs/promises'; try { // Read audio file const audioData = await readFile('./podcast.mp3'); // Create abort controller for cancellation const abortController = new AbortController(); // Decode audio asynchronously const pcmSamples = await decodeAudioAsync( audioData, 'podcast.mp3', abortController.signal ); console.log('Decoded samples:', pcmSamples.length); console.log('Duration (seconds):', pcmSamples.length / 16000); console.log('Sample rate: 16000 Hz (Whisper standard)'); // Use pcmSamples with Whisper // const transcription = whisper.full(params, pcmSamples); } catch (error) { if (error.name === 'AbortError') { console.log('Audio decoding cancelled'); } else { console.error('Decoding failed:', error); } } ``` -------------------------------- ### Synchronously Decode Audio Files with whisper-node Source: https://context7.com/brooooooklyn/whisper-node/llms.txt The decodeAudio function synchronously decodes audio files into PCM samples. It is suitable for smaller files where blocking the event loop is acceptable. It takes the audio data and filename as input and returns PCM samples. ```javascript import { decodeAudio } from '@napi-rs/whisper'; import { readFileSync } from 'node:fs'; // Read audio file synchronously const audioData = readFileSync('./short-clip.wav'); // Decode synchronously (blocks until complete) const pcmSamples = decodeAudio(audioData, 'short-clip.wav'); console.log('Decoded samples:', pcmSamples.length); console.log('Ready for Whisper processing'); ``` -------------------------------- ### Decode Audio to PCM Buffer (JavaScript) Source: https://github.com/brooooooklyn/whisper-node/blob/main/README.md Decodes various audio formats into a PCM buffer. This function supports multiple audio formats, with a full list available on the Symphonia homepage. It can be used asynchronously or synchronously. ```javascript import { readFile } from 'node:fs/promises' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { decodeAudioAsync } from './index.js' const rootDir = join(fileURLToPath(import.meta.url), '..') const audio = await readFile(join(rootDir, '__test__/rolldown.wav')) // there is also a sync version: `decodeAudio` const audioBuffer = await decodeAudioAsync(audio, 'rolldown.wav') ``` -------------------------------- ### Query and Convert Whisper Language Identifiers Source: https://context7.com/brooooooklyn/whisper-node/llms.txt The Whisper class provides static methods for querying and converting between language identifiers, codes, and full names supported by Whisper models. This allows developers to easily manage and utilize language information for transcription. ```javascript import { Whisper } from '@napi-rs/whisper'; // Get maximum language ID const maxId = Whisper.maxLangId(); console.log('Number of supported languages:', maxId + 1); // Convert language code to ID const germanId = Whisper.langId('de'); console.log('German language ID:', germanId); // Output: 2 // Convert language name to ID const germanIdByName = Whisper.langId('german'); console.log('German ID by name:', germanIdByName); // Output: 2 // Get language code from ID const langCode = Whisper.lang(2); console.log('Language code:', langCode); // Output: "de" // Get full language name from ID const langName = Whisper.langFull(2); console.log('Full language name:', langName); // Output: "german" // Enumerate all languages console.log(' Supported languages:'); for (let i = 0; i <= Whisper.maxLangId(); i++) { const code = Whisper.lang(i); const name = Whisper.langFull(i); if (code && name) { console.log(`${i}: ${code} (${name})`); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.