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

python豆包语音合成并播放

 豆包语音合成文档入口:账号登录-火山引擎

#coding=utf-8'''
requires Python 3.6 or later
pip install requests
'''
import base64
import json
import requests
from playsound import playsoundtts_file_name="doubaotts.mp3"
def doubao_tts(text):'''语音合成'''appid = "" #修改成自己的access_token= "" #修改成自己cluster = "volcano_tts"voice_type = "zh_female_daimengchuanmei_moon_bigtts" #音色:呆萌川妹host = "openspeech.bytedance.com"api_url = f"https://{host}/api/v1/tts"header = {"Authorization": f"Bearer;{access_token}"}request_json = {"app": {"appid": appid,"token": access_token,"cluster": cluster,},"user": {"uid": "uid123"},"audio": {"voice_type": voice_type,"encoding": "mp3","compression_rate": 1,"rate": 24000,"speed_ratio": 1.0,"volume_ratio": 1.0,"pitch_ratio": 1.0,"emotion": "happy","language": "cn"},"request": {"reqid": "uuid","text": text,"text_type": "plain","operation": "query","silence_duration": "125","with_frontend": "1","frontend_type": "unitTson","pure_english_opt": "1"}}file_name= tts_file_nametry:resp = requests.post(api_url, json.dumps(request_json), headers=header)# print(f"resp body: \n{resp.json()}")if "data" in resp.json():data = resp.json()["data"]file_to_save = open(file_name, "wb")file_to_save.write(base64.b64decode(data))return Trueelse:return Falseexcept Exception as ex:# ex.with_traceback()print(ex)doubao_tts("通过本文,我们了解了如何在Python中通过pygame库播放MP3文件的基本方法")
playsound(f'D:/2025_work/project/ai_bot_project/{tts_file_name}')

相关文章:

  • keil5软件配置以及使用技巧
  • Aladdin显卡多任务运行教程
  • 大模型应用_AutoGPT
  • 软件测试之接口测试详解
  • Linux CentOS 更改MySQL数据库目录位置
  • 生态篇|多总线融合与网关设计
  • 函数与数组---------C语言经典题目(1)
  • Vue 3 计算属性与侦听器深度解析:优雅处理响应式数据
  • ​​Nginx快速入门-3:工作流程和模块化
  • day1-小白学习JAVA(mac版)---(jdk安装和环境变量配置)
  • 【Reading Notes】(8.2)Favorite Articles from 2025 February
  • ModbusTCP 转 Profinet 主站网关
  • 从零实现Git安装、使用
  • FairMOT与MCFairMOT算法对比
  • 【web服务_负载均衡Nginx】二、Nginx 核心技术之负载均衡与反向代理
  • c++ static 和 extern 关键字
  • 论文阅读笔记——Mixtral of Experts
  • 期货交易躲过AI捕杀—期货反向跟单策略
  • 51单片机实验四:键盘检测原理及应用实现
  • Linux工具学习之【gcc/g++】
  • 三江购物:因自身商业需要,第二大股东阿里泽泰拟减持不超3%公司股份
  • 探索未来课堂更多可能,“人工智能课堂分析循证实验室”在沪成立
  • 南部战区回应菲护卫艇非法侵入中国黄岩岛领海:依法警告驱离
  • 接续驰援,中国政府援缅卫生防疫队出发赴缅
  • 两大跨国巨头称霸GLP-1市场,国产减肥药的机会在哪?
  • “一城双白金”就在脚下!这场半马将以最高标准打造