Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created January 5, 2025 06:43
Show Gist options
  • Save GOROman/1ce3f2122576a4b21e081e9cd7fc7bfc to your computer and use it in GitHub Desktop.
Save GOROman/1ce3f2122576a4b21e081e9cd7fc7bfc to your computer and use it in GitHub Desktop.
OpenAI Whisper で WAVファイルから文字起こし
import fs from 'fs';
import OpenAI from 'openai'; // 最新版ではこの形式を使用します
import dotenv from 'dotenv';
// 環境変数を読み込む
dotenv.config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // API キーを環境変数から取得
});
async function transcribeAudio(filePath) {
try {
// WAV ファイルを読み込む
const audioFile = fs.createReadStream(filePath);
// Whisper API にファイルを送信
const response = await openai.audio.transcriptions.create({
file: audioFile,
model: 'whisper-1',
});
// 文字起こし結果を出力
console.log('Transcription result:', response.text);
} catch (error) {
console.error('Error during transcription:', error.message);
if (error.response) {
console.error('Details:', error.response.data);
}
}
}
// 実行例
const audioFilePath = './output.wav'; // WAV ファイルのパス
transcribeAudio(audioFilePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment