Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Created June 29, 2024 05:30
Show Gist options
  • Save juanpablocs/115b9047617872b66e127d0ff10601f5 to your computer and use it in GitHub Desktop.
Save juanpablocs/115b9047617872b66e127d0ff10601f5 to your computer and use it in GitHub Desktop.
Extract subtitle from video with openia Api Whisper-1

Extract subtitle from video

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.

Generating Subtitles with OpenAI

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment