python中对edge_tts的使用-代码
from sqlalchemy import create_engine, Column, Integer, String, LargeBinary,DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import time
import edge_tts
Base = declarative_base()
Session = None
def init_db():
global Session
# MySQL数据库连接字符串,根据实际情况修改参数
DATABASE_URI = 'mysql+mysqlconnector://root:1qaz!QAZ@127.0.0.1:3306/cms_base?charset=utf8'
engine = create_engine(DATABASE_URI, echo=True) # 设置echo=True以查看SQL语句
# 如果表不存在则创建新表
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
class WebPage(Base):
__tablename__ = 'biz_zh'
id = Column(Integer, primary_key=True, autoincrement=True)
url = Column(String(255), unique=True, nullable=False)
title = Column(String(255))
title_en = Column(String(255))
category = Column(String(255))
content = Column(String(20000))
content_en = Column(String(20000))
audio_file = Column(LargeBinary, nullable=True)
status = Column(String(255))
update_time = Column(DateTime, default=datetime.datetime.now())
def __repr__(self):
return f'WebPage(id={self.id}, url={self.url}, title={self.title})'
def main3():
# 中文文本
TEXT = "你好,这是一个测试,用于展示如何使用Edge TTS服务生成中文语音。"
# 选择中文语音,这里以Xiaoxiao为例
VOICE = "zh-CN-XiaoxiaoNeural"
with Session() as session:
# 查询所有需要翻译的网页
pages = session.query(WebPage).filter(WebPage.status == None).all()
#.filter(WebPage.content_en != None)
for page in pages:
print(f"开始生成语音:{page.id}")
communicate = edge_tts.Communicate(text = page.content, voice=VOICE)
communicate.save_sync(f'D:/pyvenv/zh_mp3/{page.id}-{page.title}.mp3')
page.status = 'zh_audio_generated'
session.commit()
#if(page.id == 10):
# break
time.sleep(1)
if __name__ == "__main__":
init_db()
main3()
import asyncio
import edge_tts
async def main4():
# 中文文本
TEXT = "中华上下五千年 The history of china about 5000 years。"
# 选择中文语音,这里以Xiaoxiao为例
VOICE = "en-US-AndrewNeural"
voices_manager = await edge_tts.VoicesManager.create()
num = 0
for voice in voices_manager.voices:
num = num + 1
print(f"Name: {voice['ShortName']}, Gender: {voice['Gender']},Locale: {voice['Locale']}, VoiceTag: {voice['VoiceTag']}")
VOICE = voice['ShortName']
communicate = edge_tts.Communicate(text = TEXT, voice=VOICE)
communicate.save_sync(f'D:/pyvenv/edgetts_mp3/{num}-{voice['ShortName']}-{voice['Gender']}.mp3')
time.sleep(1)
if __name__ == "__main__":
asyncio.run(main4())
原文地址:https://blog.csdn.net/weixin_42551921/article/details/145276429
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!