如何通过OpenAI Whisper API获取单词时间戳?
解决Whisper API获取单词级时间戳的问题
要通过OpenAI Whisper API获取附带单词时间戳的转录结果,需使用API专属的参数配置——本地模型的word_timestamps=True参数在API中不适用,具体设置如下:
关键参数配置
response_format: 必须设置为verbose_json,这是获取详细转录元数据(包括时间戳)的前提timestamp_granularities: 设置为["word"],明确要求返回单词级别的时间戳
Python请求示例
from openai import OpenAI client = OpenAI() response = client.audio.transcriptions.create( model="whisper-1", file=open("your_audio_file.mp3", "rb"), response_format="verbose_json", timestamp_granularities=["word"] ) # 提取目标单词时间戳数据 word_timestamps = response.words print(word_timestamps)
返回结果示例
请求成功后,返回的verbose_json格式中,words字段即为你需要的结构化数据:
[ {"start": 0.0, "end": 0.5, "word": "hello"}, {"start": 0.5, "end": 1.0, "word": "world"} ]
失败原因排查
若此前尝试未成功,通常是因为未同时配置上述两个参数——response_format和timestamp_granularities缺一不可;另外需确保使用的是最新版OpenAI Python库,旧版本可能不支持timestamp_granularities参数。
内容的提问来源于stack exchange,提问作者SazCoR
相关产品推荐
相关产品推荐

