Implement audio transcription using the Whisper API.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer

Implementation:

import OpenAI from 'openai';
import fs from 'fs';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function transcribeAudio(audioFilePath) {
  try {
    const transcription = await openai.audio.transcriptions.create({
      file: fs.createReadStream(audioFilePath),
      model: "whisper-1",
      language: "en", // Optional: specify language
      response_format: "text" // or 'json', 'verbose_json', 'srt', 'vtt'
    });

    return transcription;
  } catch (error) {
    console.error("Transcription failed:", error);
  }
}

// Usage
// const text = await transcribeAudio("meeting_recording.mp3");
// console.log(text);