@pr0gramm/fluester
Version:
Node.js bindings for OpenAI's Whisper. Optimized for CPU.
58 lines (55 loc) • 1.81 kB
JavaScript
import { path as ffmpegPath } from "@ffmpeg-installer/ffmpeg";
import { path as ffprobePath } from "@ffprobe-installer/ffprobe";
import ffmpeg from "fluent-ffmpeg";
ffmpeg.setFfmpegPath(ffmpegPath);
ffmpeg.setFfprobePath(ffprobePath);
/**
* Converts a file to a file that whisper.cpp can work with.
*
* @param inputFile Any input file that ffmpeg supports. That means almost any audio or video file.
* @param outputFile
* @example
* ```
* const inputFile = "...";
* const outputFile = "output.wav";
* await convertFileToProcessableFile(inputFile, outputFile);
* // ...
* await client.detectLanguage(outputFile);
```
*/
export async function convertFileToProcessableFile(inputFile, outputFile, _options) {
// const signal = options?.signal;
// Taken from the whisper.cpp docs:
// ffmpeg -i input.mp3 -ar 16000 -ac 1 -c:a pcm_s16le output.wav
const command = ffmpeg(inputFile)
.audioFrequency(16000)
.audioChannels(1)
.audioCodec("pcm_s16le")
.output(outputFile);
return new Promise((resolve, reject) => {
command.once("end", resolve);
command.once("error", reject);
command.run();
});
}
// TODO: Not yet supported:
/*
async function pipeStreamToProcessableStream(
input: Readable,
output: Writable,
_options?: ConvertOptions,
): Promise<void> {
// const signal = options?.signal;
// Taken from the whisper.cpp docs:
// ffmpeg -i input.mp3 -ar 16000 -ac 1 -c:a pcm_s16le output.wav
const command = ffmpeg(input)
.audioFrequency(16000)
.audioChannels(1)
.audioCodec("pcm_s16le");
return new Promise((resolve, reject) => {
command.once("end", resolve);
command.once("error", reject);
command.pipe(output, { end: true });
});
}
*/