文字转语音 edge_tts

1、前沿

这是一款微软语音转文字的项目,不要电脑配置因为他是通过类爬虫方式把文字转语音,跟官方比起来速度会慢一点但是基本够用

https://github.com/rany2/edge-tts

2、代码封装

默认是通过命令行的方式不适合集成到我们自己的项目通过封装成接口的方式使用

复制代码
@router.get("/text-to-speech", tags=["语音合成"], summary="文字转语音")
async def text_to_speech(text: str):
    try:
        file_path = await edge_tts_async(text)
        return FileResponse(
            path=file_path, media_type="audio/mp3", filename=os.path.basename(file_path)
        )
    except Exception as e:
        return {"error": f"转换失败: {str(e)}"}


import edge_tts

async def edge_tts_async(text):
    num = random.randint(1, 100000000)
    static_dir = "static/audio"
    os.makedirs(static_dir, exist_ok=True)
    file_path = os.path.join(static_dir, f"{num}.mp3")

    # 从配置获取代理设置(如果有)
    proxy = None
    if "proxies" in GLOBAL_CONFIG and "proxy" in GLOBAL_CONFIG["proxies"]:
        proxy = GLOBAL_CONFIG["proxies"]["proxy"]
    
    try:
        # 使用edge_tts.Communicate类进行文字转语音
        communicate = edge_tts.Communicate(
            text,
            "zh-CN-XiaoyiNeural",
            proxy=proxy,
            connect_timeout=20,
            receive_timeout=60
        )
        
        # 保存音频文件
        await communicate.save(file_path)
        return file_path
    except Exception as e:
        log_error(f"文字转语音失败: {str(e)}")
        raise Exception(f"TTS conversion failed: {str(e)}")
相关推荐
不像程序员的程序媛25 分钟前
Nginx日志切分
服务器·前端·nginx
北原_春希35 分钟前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
尽意啊36 分钟前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜37 分钟前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive37 分钟前
Vue3使用ECharts
前端·javascript·echarts
竹秋…37 分钟前
echarts自定义tooltip中的内容
前端·javascript·echarts
宝贝露.37 分钟前
Axure引入Echarts图无法正常显示问题
前端·javascript·echarts
shmily麻瓜小菜鸡38 分钟前
前端文字转语音
前端
人良爱编程41 分钟前
Hugo的Stack主题配置记录03-背景虚化-导航栏-Apache ECharts创建地图
前端·javascript·apache·echarts·css3·html5
来颗仙人掌吃吃43 分钟前
解决Echarts设置宽度为100%发现宽度变为100px的问题(Echarts图标宽度自适应问题)
前端·javascript·echarts