最新发布 安装edge-tts,部署自己的文本转语音服务

发布时间: 2025-01-06,浏览量:398

在linux服务器中执行命令


pip install edge-tts


或安装指定版本


pip install edge-tts==6.1.18


安装完成后就可以使用啦,使用命令


edge-tts --voice zh-CN-YunxiNeural --text "hello 大家好,这里是人工智能研究所" --write-media hello.mp3

查看支持的语音角色


edge-tts --list-voices
Name: af-ZA-AdriNeural Gender: Female
........
........
........
Name: zh-CN-XiaoxiaoNeural Gender: Female


使用python做个服务接口调用,使用前安装好相关引用pip install


pip install Flask;

pip install jsonify;

pip install uuid;

............

from flask import Flask, request, jsonify
import asyncio
import edge_tts
import os
import uuid

app = Flask(__name__)

@app.route('/tts', methods=['POST'])
def text_to_speech():
    data = request.json
    text = data.get('text')
    voice = data.get('voice', 'zh-CN-XiaoxiaoNeural')
    rate = data.get('rate', '+0%')
    volume = data.get('volume', '+0%')
    output_dir = '/opt/tts/'
    fname = str(uuid.uuid4()) + '.mp3'
    output_file = os.path.join(output_dir, fname)

    async def convert_text_to_speech():
        try:
            communicate = edge_tts.Communicate(text, voice, rate=rate, volume=volume)
            await communicate.save(output_file)
            return True
        except Exception as e:
            print(f"Error converting text to speech: {e}")
            return False

    success = asyncio.run(convert_text_to_speech())

    if success:
        response = {
            'code': 0,
            'msg': 'completed',
            'data': fname
        }
    else:
        response = {
            'code': 1,
            'msg': 'failed',
            'data': None
        }

    return jsonify(response)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)


将以上保存为app.py文件

运行服务

nohup python app.py &

调用服务

curl -X POST -H "Content-Type: application/json" -d '{"text": "你好,这里是人工智能研究所!", "voice": "zh-CN-XiaoxiaoNeural"}' http://localhost:5000/tts

热门文章 经典语录

热门文章 热门文章

查看更多