File size: 1,174 Bytes
30479ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio
import edge_tts
from fastapi import FastAPI, Query, HTTPException
from fastapi.responses import StreamingResponse
import io

app = FastAPI()

# 支持的中文常用声音列表
# zh-CN-XiaoxiaoNeural (晓晓: 亲切、自然)
# zh-CN-YunxiNeural (云希: 阳光、少年)
# zh-CN-YunjianNeural (云健: 沉稳、解说)

@app.get("/tts")
async def tts_endpoint(
    text: str = Query(..., description="要转换的文字"),
    voice: str = Query("zh-CN-XiaoxiaoNeural", description="声音名称")
):
    try:
        # 初始化 edge-tts
        communicate = edge_tts.Communicate(text, voice)
        
        # 将音频流写入内存流中
        audio_stream = io.BytesIO()
        async for chunk in communicate.stream():
            if chunk["type"] == "audio":
                audio_stream.write(chunk["data"])
        
        audio_stream.seek(0)
        
        # 返回 MP3 数据流
        return StreamingResponse(audio_stream, media_type="audio/mpeg")
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)