Hey all! I am currently trying to download an mp3 from the web, then send it to the /transcriptions
endpoint, but I keep getting a 400 BAD_REQUEST
error.
Initially, I was getting an Invalid file format
error (even though I’m just sending an mp3), but I resolved that by first writing the mp3 file I downloaded from the web to a file on disk.
I am now using fs.createReadStream()
to read the mp3 file from disk and send to the OpenAI /transcriptions
endpoint, but, like I mentioned, I am getting a 400
error every time.
I have tried using the openai
package as well as just using axios
and I get the same result.
Here is my code:
app.get('/api/transcribeAudio', async (request, response) => {
// Download the audio file
const audioUrl = 'https://example.com/filename.mp3'
const outputFilePath = 'file.mp3'
let downloadResponse;
try {
downloadResponse = await axios.get(audioUrl, { responseType: 'arraybuffer' })
// Convert the arraybuffer to a Buffer
const mp3Buffer = Buffer.from(downloadResponse.data);
// Write the Buffer to a file
fs.writeFileSync(outputFilePath, mp3Buffer);
console.log('MP3 file saved successfully!');
} catch(e) {
console.log('Error downloading mp3', e)
}
// OpenAI api call using axios
let data = new FormData();
data.append("file", fs.createReadStream(outputFilePath));
data.append("model", "whisper-1");
data.append("language", "en");
let config = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.openai.com/v1/audio/transcriptions",
headers: {
Authorization:
`Bearer ${token}`,
"Content-Type": "multipart/form-data",
...data.getHeaders(),
},
data: data,
};
const response = await axios.request(config);
const transcription = response.data;
console.log('transcription', transcription)
// Using OpenAI library
const model = 'whisper-1';
const transcriptionResponse = await openai.createTranscription(
fs.createReadStream(outputFilePath),
model
);
console.log('transcriptionResponse', transcriptionResponse)
})
Any help is greatly appreciated!!!