Assuming you have a video file named myvideo.webm, the first step is to extract the audio. To manage API limitations effectively, compress the audio as much as possible.
ffmpeg -i myvideo.webm -ac 1 -b:a 16k -map a output.webm
FFmpeg parameters explained:
- -i: Specifies the input file.
- -ac 1: Uses just one audio channel, meaning mono audio.
- -b:a 16k: Converts to a 16k bitrate.
- -map a: Extracts only the audio stream.
The result is a compressed audio file named output.webm, ready for subtitle generation.
import OpenAI, { toFile } from 'openai';
import fs from 'fs';
const openai = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
async function main() {
const file = await fs.readFileSync("output.webm"); // Read file
const result = await openai.audio.transcriptions.create({
model: 'whisper-1',
response_format: 'vtt',
file: toFile(file, "output.webm")
});
await fs.writeFileSync("subtitles.vtt", result); // Save subtitles
}
main();