You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Expo-AV在iOS录制的MP4调用OpenAI语音转写遇格式无效错误

问题:OpenAI语音转写接口返回"无效文件格式"错误

我调用OpenAI语音转写接口时收到如下错误:

message: "Invalid file format. Supported formats: ['m4a', 'mp3', 'webm', 'mp4', 'mpga', 'wav', 'mpeg']"

我的代码实现如下:

// needed for IoS
if (audioPermission) {
  await Audio.setAudioModeAsync({
    allowsRecordingIOS: true,
    playsInSilentModeIOS: true,
  });
}
Audio.IOSOutputFormat;
const newRecording = new Audio.Recording();

const recordingOptions = {
  ios: {
    extension: '.mp4', // Specify the audio file extension,
    audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH, // Specify the audio quality
  },
  android: {
    extension: '.mp4', // Specify the audio file extension,
  },
};
console.log('Starting Recording');
await newRecording.prepareToRecordAsync(recordingOptions);

await newRecording.startAsync();

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 MP4 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',
  };

  const fileData = await FileSystem.readAsStringAsync(recordingUri, {
    encoding: FileSystem.EncodingType.Base64,
  });
  const fileObject = new File([fileData], fileName, {
    type: 'audio/mp4',
  });

  const formData = new FormData();
  formData.append('file', fileObject, 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);
}

我用Expo AV录制了MP4格式的音频,调用接口时却被判定为格式无效。我的情况和一个Stack Overflow问题类似,但那个实现还没调用接口就崩溃了,尝试ChatGPT也没能找到解决方案。

内容的提问来源于stack exchange,提问作者Ibra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:33:10