当前位置: 首页 > news >正文

如何使用OPENAI的Whisper功能进行音频字母提取功能

首先你可以使用 Python 中的 requests 库来下载该音频文件,然后通过 open() 打开该文件并传递给 OpenAI Whisper API。

完整代码如下:

  1. 安装需要的库:
pip install openai requests
  1. Python 代码:
OPENAI_API_KEY= "your openai_api_key"client = OpenAI(api_key=OPENAI_API_KEY)response = requests.get(output_url)result = []with tempfile.NamedTemporaryFile(delete=False, suffix=".wav", dir=".") as temp_audio_file:temp_audio_file.write(response.content)temp_audio_file_path = temp_audio_file.nameprint(f"Audio file saved to temporary file: {temp_audio_file_path}")# 打开音频文件并进行转录with open(temp_audio_file_path, "rb") as audio_file:transcription = client.audio.transcriptions.create(file=audio_file,model="whisper-1",response_format="verbose_json",timestamp_granularities=["word"])for word_info in transcription.words:word = word_info.wordtask_start_time = word_info.starttask_end_time = word_info.endword_task = {"word": word,"start_time": task_start_time,"end_time": task_end_time}result.append(word_task)logger.info(f"打印结果:{result}")

1. 客户端初始化

client = OpenAI(api_key=OPENAI_API_KEY)
  • client 是一个与 OpenAI API 交互的客户端实例。api_key 是你用来认证的密钥,这里应该替换为你的 OpenAI API 密钥。
  • OpenAI 是 OpenAI 提供的 Python 客户端,可以用来访问 GPT-3、Whisper、DALL-E 等服务。

2. 下载音频文件

response = requests.get(output_url)
  • 通过 requests.get(output_url) 发送 HTTP GET 请求,下载指定 URL(output_url)的内容(应该是一个音频文件)。
  • response.content 获取的是该音频文件的二进制内容。

3. 保存为临时音频文件

with tempfile.NamedTemporaryFile(delete=False, suffix=".wav", dir=".") as temp_audio_file:     temp_audio_file.write(response.content) temp_audio_file_path = temp_audio_file.name print(f"Audio file saved to temporary file: {temp_audio_file_path}")
  • tempfile.NamedTemporaryFile 用于创建一个临时文件,并指定它的后缀为 .wav,表示这个临时文件将是一个 WAV 格式的音频文件。
  • delete=False 表示临时文件在关闭时不会自动删除(稍后手动删除)。
  • temp_audio_file.write(response.content) 将从 response 中下载的音频数据写入临时文件。
  • temp_audio_file.name 获取临时文件的路径,并将其存储到 temp_audio_file_path 变量中。
  • print 输出临时文件的路径,方便调试。

4. 使用 OpenAI Whisper 进行转录

with open(temp_audio_file_path, "rb") as audio_file:transcription = client.audio.transcriptions.create( file=audio_file, model="whisper-1", response_format="verbose_json", timestamp_granularities=["word"] )
  • 这部分代码打开刚刚创建的临时音频文件。
  • client.audio.transcriptions.create 调用 Whisper 模型进行音频转录:
    • file=audio_file:传递打开的音频文件。
    • model="whisper-1":使用 Whisper 模型进行音频转录。
    • response_format="verbose_json":指定返回的结果为详细的 JSON 格式。
    • timestamp_granularities=["word"]:指定返回每个单词的时间戳(开始时间和结束时间)。

5. 处理转录结果

for word_info in transcription.words: word = word_info.word task_start_time = word_info.start task_end_time = word_info.end word_task = { "word": word, "start_time": task_start_time, "end_time": task_end_time }         result.append(word_task)
  • transcription.words 是一个包含每个单词信息的列表。每个 word_info 包含:
    • word:转录出的单词。
    • start:该单词的开始时间(单位通常是秒)。
    • end:该单词的结束时间。
  • word_task 是一个字典,用来存储每个单词的信息:包括单词、开始时间和结束时间。
  • result.append(word_task) 将每个单词的信息添加到 result 列表中。

总结

  1. 下载音频文件:通过 requests 库从指定 URL 下载音频文件并保存为临时 .wav 文件。
  2. 使用 Whisper 进行转录:通过 OpenAI 的 Whisper 模型对音频进行转录,获取每个单词的开始和结束时间。
  3. 存储转录结果:将每个单词的时间戳信息存储到 result 列表中。
  4. 打印结果:通过日志记录器将转录结果打印出来。

可能的应用场景

  • 音频转录服务(如字幕生成、语音识别)。
  • 对音频进行更详细的时间戳标记,用于后续处理(如视频编辑、语音分析等)。
http://www.lryc.cn/news/538515.html

相关文章:

  • DFS算法篇:理解递归,熟悉递归,成为递归
  • 2025java常见面试题第一弹
  • JMeter工具介绍、元件和组件的介绍
  • 机舱卫生和空气质量改善
  • springBoot之环境变量
  • 萨班斯-奥克斯利法案(Sarbanes-Oxley Act, SOX):公司财务透明度的守护者(中英双语)
  • iOS 中使用 FFmpeg 的高级功能 - 滤镜(Filters)
  • tomcat html乱码
  • kubectl exec 实现的原理
  • Unity中可靠的UDP实现
  • CentOS 7操作系统部署KVM软件和创建虚拟机
  • Golang GORM系列:GORM分页和排序
  • WPF的MVVMLight框架
  • 微服务SpringCloudAlibaba组件sentinel教程【详解sentinel的使用以及流量控制、熔断降级、热点参数限流等,附有示例+代码】
  • ScoreFlow:通过基于分数的偏好优化掌握 LLM 智体工作流程
  • 数字水印嵌入及提取系统——基于小波变换GUI
  • 基于海思soc的智能产品开发(图像处理的几种需求)
  • 【R语言】聚类分析
  • Spring 项目接入 DeepSeek,分享两种超简单的方式!
  • docker 进阶命令(基于Ubuntu)
  • 机器学习数学基础:29.t检验
  • HarmonyNext上传用户相册图片到服务器
  • WebAssembly 3.0发布:浏览器端高性能计算迎来新突破!
  • 计算机组成原理—— 外围设备(十三)
  • 面试题之Vuex,sessionStorage,localStorage的区别
  • window中git bash使用conda命令
  • 象棋掉落动画(局部旋转动画技巧)
  • Pycharm 2024在解释器提供的python控制台中运行py文件
  • 课题推荐:高空长航无人机多源信息高精度融合导航技术研究
  • 《DeepSeek训练算法:开启高效学习的新大门》