I am trying the following:
- AN app with a record button, and stop, when stoped saves file as MP3, and then calls a transcript function. BUt I get all kinds of errors:
Sometimes I get this: Invalid file format. Supported formats: [‘flac’, ‘m4a’, ‘mp3’, ‘mp4’, ‘mpeg’, ‘mpga’, ‘oga’, ‘ogg’, ‘wav’, ‘webm’]", “param”: null, “type”: “invalid_request_error”}
Sometimes I get this:
body → file
field required (type=value_error.missing)", “param”: null, “type”: “invalid_request_error”}}
my code for the stop and transcribe functions is like this:
const stopRecording = async () => {
console.log('Stopping recording...');
setRecording(null);
setIsRecording(false);
await recording?.stopAndUnloadAsync();
const uri = recording?.getURI();
if (uri) {
const timestamp = Date.now();
try {
// Convert recording to MP3
const mp3FileUri = `${FileSystem.documentDirectory}recording_${timestamp}.mp3`;
await FileSystem.copyAsync({
from: uri,
to: mp3FileUri,
});
console.log('Recording saved as MP3:', mp3FileUri);
// Fetch duration of the recording
const { sound } = await Audio.Sound.createAsync({ uri: mp3FileUri });
const { durationMillis } = await sound.getStatusAsync();
await sound.unloadAsync();
await saveRecording(mp3FileUri, timestamp, durationMillis);
// Read the MP3 file content into a Base64 string
const mp3Base64String = await FileSystem.readAsStringAsync(mp3FileUri, {
encoding: FileSystem.EncodingType.Base64,
});
console.log('MP3 Base64 string length:', mp3Base64String.length);
// Convert Base64 string back to Blob
const mp3Blob = await base64ToBlob(mp3Base64String, 'audio/mpeg');
// Check the contents of the Blob
const mp3Base64Check = await readBlobAsBase64(mp3Blob);
console.log('MP3 Base64 check:', mp3Base64Check === mp3Base64String);
// Call the transcription function with the Blob object
await transcribeAudio(mp3Blob);
} catch (error) {
console.error('Failed to save recording as MP3:', error);
}
}
};
const transcribeAudio = async (file) => {
try {
const formData = new FormData();
formData.append('file', file, 'audio.mp3');
formData.append('model', 'whisper-1');
formData.append('response_format', 'verbose_json');
formData.append('timestamp_granularities', 'segment');
const headers = {
'Authorization': `Bearer ${openaiApiKey}`,
'Content-Type': 'multipart/form-data',
};
console.log('Transcribing audio...');
const response = await axios.post('https://api.openai.com/v1/audio/transcriptions', formData, { headers });
console.log('Transcription response:', response.data); // Log full response for debugging
// Extract the actual transcription text from the response
const transcriptionText = response.data.text;
console.log('Transcription text:', transcriptionText);
} catch (error) {
console.error('Failed to transcribe audio:', error);
if (error.response) {
console.log('Response data:', error.response.data);
}
}
};
const base64ToBlob = async (base64Data, contentType) => {
const response = await fetch(`data:${contentType};base64,${base64Data}`);
return await response.blob();
};
const readBlobAsBase64 = (blob) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
resolve(reader.result.split(',')[1]); // Split to remove the 'data:audio/mpeg;base64,' part
};
reader.onerror = reject;
reader.readAsDataURL(blob);
});
};
any idea how to send correctly the fileupload? it doesnt seem to accept base64, or a blob, I am completely lost