import { exec } from "child_process"; // Node.js built-in module import path from "path"; // Path module import fs from "fs"; // File system module import { fileURLToPath } from "url"; // To handle __dirname in ES modules const __filename = fileURLToPath(import.meta.url); // Get current file path const __dirname = path.dirname(__filename); // Get current directory path const transcriptsDir = path.resolve(__dirname, "../../../storage/transcriptions"); export class whisperLocal { // is called by transcribe.ts private whisperBinary: string; // Path to the whisper.cpp binary private modelPath: string; // Path to the model file constructor() { this.whisperBinary = path.resolve( __dirname, "whisper.cpp/build/bin/whisper-cli" //Path to the compiled whisper binary ); this.modelPath = path.resolve( __dirname, "whisper.cpp/models/ggml-base.en.bin" // Path to the English model file ); } async transcribe(audioPath: string): Promise { //asyncronous function to transcribe audio return new Promise((resolve, reject) => { if (!fs.existsSync(transcriptsDir)) { //if transcripts directory does not exist, create it fs.mkdirSync(transcriptsDir, { recursive: true }); } const outputBase = path.resolve( // Base path for output transcript files, name is same as audio file (video file) transcriptsDir, path.basename(audioPath, path.extname(audioPath)) ); const command = `"${this.whisperBinary}" -m "${this.modelPath}" -f "${audioPath}" -otxt -of "${outputBase}"`; // Command to execute whisper binary with model and audio file, outputting text file exec(command, (error, stdout, stderr) => { if (error) return reject(error); const outputTxt = `${outputBase}.txt`; if (fs.existsSync(outputTxt)) { const transcript = fs.readFileSync(outputTxt, "utf8"); resolve(transcript); } else { reject(new Error("No transcript file found")); } }); }); } }