Created
January 5, 2025 06:43
-
-
Save GOROman/1ce3f2122576a4b21e081e9cd7fc7bfc to your computer and use it in GitHub Desktop.
OpenAI Whisper で WAVファイルから文字起こし
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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