mirror of
https://gitlab.rlp.net/proj-wise2526-video2document/video2document.git
synced 2026-06-15 18:01:52 +02:00
New Folder structure
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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<string> { //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"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user