Search code examples
node.jsaudioopenai-whisper

Convert audio buffer into readable stream to use in Whisper


I want to remove the step of saving the audio file locally and then reading the audio file after it is saved.

This is what I am currently doing which works perfectly

fs.writeFileSync("audio/audioFile.webm", audioBuffer);

var audioStream = fs.createReadStream("audio/audioFile.webm");

// then posting the audio stream in the Whisper Api at https://api.openai.com/v1/audio/transcriptions which is working perfectly

I can't help but think that I can surely remove the step of saving the file and send the buffer as an audio readable stream to the Whisper Api

Is this even possible?

I tried this and it doesn't work, the stream is a "Readable" and not a "ReadStream" which seems to be what the Whisper Api needs.

const { Readable } = require('stream');
var audioStream = Readable.from(audioBuffer);


Solution

  • The solution is to add the filename, surprisingly.

    const {Readable} = require('stream')
    ...
    const audioReadStream = Readable.from(audioBuffer);
    audioReadStream.path = `filename.webm`;
    

    You've run into a bug due to the way the API does mimeType detection: simply looks at the extension. The reason the read stream from a file works is that the filename is included, and thus the API can know the format.

    See here.