如何在SpeechMatics API中使用fetch_data提交无音频参数的转写任务
Speechmatics API:使用fetch_data URL提交转录任务
问题说明
原本的代码通过本地音频文件路径作为audio参数提交语音转写任务,现在希望通过fetch_data配置指定远程音频URL来提交任务,但submit_job方法要求传入audio参数,请问如何在不依赖本地文件的情况下完成任务提交?
解决方案
当转录配置中包含fetch_data字段时,SpeechMatics API会直接从指定URL拉取音频文件,无需上传本地音频。此时只需将audio参数设为None,同时在配置中完善fetch_data的URL信息即可。
修改后的完整代码
from speechmatics.models import ConnectionSettings from speechmatics.batch_client import BatchClient from httpx import HTTPStatusError API_KEY = "YOUR_API_KEY" LANGUAGE = "en" REMOTE_AUDIO_URL = "https://your-audio-url-here/example.wav" # 替换为实际音频URL settings = ConnectionSettings( url="https://asr.api.speechmatics.com/v2", auth_token=API_KEY, ) # 定义包含fetch_data的转录参数 conf = { "type": "transcription", "transcription_config": { "language": LANGUAGE }, "fetch_data": { "url": REMOTE_AUDIO_URL } } # 使用上下文管理器打开客户端 with BatchClient(settings) as client: try: # 提交任务时将audio设为None job_id = client.submit_job( audio=None, transcription_config=conf, ) print(f'任务 {job_id} 提交成功,等待转录结果') # 生产环境建议使用通知机制替代轮询 transcript = client.wait_for_completion(job_id, transcription_format='txt') print(transcript) except HTTPStatusError: print('无效API密钥 - 检查代码顶部的API_KEY!')
关键说明
fetch_data中的url需填写可公开访问的音频文件URL,确保SpeechMatics服务能正常拉取到音频资源。- 配置
fetch_data后,将audio参数设为None即可满足submit_job的参数要求,同时触发API从指定URL获取音频的逻辑。
内容的提问来源于stack exchange,提问作者london_utku
相关产品推荐
相关产品推荐

