I m 从露天 transcript末点接回这一错误;
message: "The audio file could not be decoded or its format is not supported."
我做的是:
- Setting recording options to mp4;
- Move the new recording into a temporary file Directory;
- Create form data and call openai transcriptions endpoint;
发生的情况:
I error API API API API API API saying saying saying 说“音 文档不能编码或格式得不到支持”;
这就是我做的事情。
async function startRecording() {
try {
// needed for IoS
if (audioPermission) {
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true,
});
}
const newRecording = new Audio.Recording();
const recordingOptions = {
android: {
extension: .mp4 ,
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
},
ios: {
extension: .mp4 ,
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC,
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH,
},
};
console.log( Starting Recording )
await newRecording.prepareToRecordAsync(
recordingOptions
);
await newRecording.startAsync();
setRecording(newRecording);
setRecordingStatus( recording );
} catch (error) {
console.error( Failed to start recording , error);
}
}
async function stopRecording() {
try {
if (recordingStatus == recording ) {
console.log( Stopping recording.. );
await recording.stopAndUnloadAsync();
const recordingUri = recording.getURI();
console.log( Recording stopped and stored at URI , recordingUri);
console.log( OPENAI_API_KEY , OPENAI_API_KEY);
// Create a file name for the new recording
const fileName = `recording-${Date.now()}.mp4`;
// Calls openai translations endpoint
try {
const url = https://api.openai.com/v1/audio/transcriptions ;
const headers = {
Authorization : `Bearer ${OPENAI_API_KEY}`,
Content-Type : multipart/form-data ,
};
// Copy the recording file to a temporary directory
const tempDirectory = FileSystem.cacheDirectory + recordings/ ;
await FileSystem.makeDirectoryAsync(tempDirectory, { intermediates: true });
const tempRecordingUri = tempDirectory + fileName;
await FileSystem.copyAsync({
from: recordingUri,
to: tempRecordingUri,
});
const formData = new FormData();
formData.append( file , {
uri: tempRecordingUri,
name: fileName,
type: audio/mp4 , // Change to the desired audio format
});
formData.append( model , whisper-1 );
// const formData = new FormData();
// formData.append( file , fileData, fileName);
// formData.append( model , whisper-1 );
axios.post(url, formData, { headers })
.then(response => {
console.log( Transcription response: , response.data);
})
.catch(error => {
console.error( Error: , error);
});
} catch (error) {
console.error(error);
}
}
}
catch (error) {
console.error( Failed to stop recording , error);
}
}