您的位置:首页 > 游戏 > 手游 > 家装公司装修_凡客诚品为什么没落了_电脑培训中心_seo每天一贴博客

家装公司装修_凡客诚品为什么没落了_电脑培训中心_seo每天一贴博客

2025/1/11 10:15:59 来源:https://blog.csdn.net/weixin_51891232/article/details/144811190  浏览:    关键词:家装公司装修_凡客诚品为什么没落了_电脑培训中心_seo每天一贴博客
家装公司装修_凡客诚品为什么没落了_电脑培训中心_seo每天一贴博客

1、进入自己的项目

复制APPID、APISecret、APIKey

2、添加好听发音人

复制vcn参数

3、需要替换代码部分:

换自己喜欢的发声人的参数

换上自己的APPID、APISecret、APIKey

4、完整代码:

# -*- coding:utf-8 -*-
import _thread as thread
import base64
import hashlib
import hmac
import wave
import json
import os
import ssl
from datetime import datetime
from time import mktime
from urllib.parse import urlencode
import websocketclass Ws_Param:def __init__(self, APPID, APIKey, APISecret, Text):self.APPID = APPIDself.APIKey = APIKeyself.APISecret = APISecretself.Text = Textself.CommonArgs = {"app_id": self.APPID}self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "x4_lingyuyan", "tte": "utf8"}self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-8')), "UTF8")}def create_url(self):url = 'wss://tts-api.xfyun.cn/v2/tts'date = datetime.now().strftime("%a, %d %b %Y %H:%M:%S GMT")signature_origin = f"host: ws-api.xfyun.cn\ndate: {date}\nGET /v2/tts HTTP/1.1"signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),hashlib.sha256).digest()signature_sha = base64.b64encode(signature_sha).decode()authorization_origin = f"api_key=\"{self.APIKey}\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"{signature_sha}\""authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode()v = {"authorization": authorization, "date": date, "host": "ws-api.xfyun.cn"}return url + '?' + urlencode(v)def on_message(ws, message):try:message = json.loads(message)code = message.get("code")if code != 0:print(f"错误:{message.get('message')},代码:{code}")returnaudio = base64.b64decode(message["data"].get("audio", b""))with open(output_pcm, 'ab') as f:f.write(audio)if message["data"].get("status") == 2:print("WebSocket连接已关闭。")ws.close()except Exception as e:print("处理消息时出错:", e)def on_error(ws, error):print("WebSocket错误:", error)def on_close(ws, close_status_code, close_msg):print(f"WebSocket关闭,状态码:{close_status_code},关闭信息:{close_msg}")def on_open(ws):def run():ws.send(json.dumps({"common": wsParam.CommonArgs, "business": wsParam.BusinessArgs, "data": wsParam.Data}))if os.path.exists(output_pcm):os.remove(output_pcm)thread.start_new_thread(run, ())if __name__ == "__main__":input_txt = "在时光长河的悄然流淌里,命运的丝线将你我缠绕。还记得初次相遇,阳光正好,你就那样带着一身光芒,不经意间走进了我的视线,从此,我的目光便再也无法从你身上移开。"output_pcm = "output.pcm"output_wav = "output.wav"wsParam = Ws_Param(APPID='###', APISecret='###',APIKey='###', Text=input_txt)ws = websocket.WebSocketApp(wsParam.create_url(), on_message=on_message, on_error=on_error, on_close=on_close)ws.on_open = on_openwebsocket.enableTrace(False)ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})with open(output_pcm, 'rb') as pcmfile:pcmdata = pcmfile.read()with wave.open(output_wav, 'wb') as wavfile:wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE'))wavfile.writeframes(pcmdata)print("转换完成。")

3、播放MAV文件

from playsound import playsound# 播放 WAV 文件
playsound("output.wav")

4、转MP3文件

from pydub import AudioSegmentsong = AudioSegment.from_wav("output.wav")
song.export("demo.mp3", format="mp3")

感谢您的支持和关注!!!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com