python 调用阿里云语音合成TTS

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
import requests
import hmac
import base64
import datetime
import tempfile
import shutil,os


class aliyun:


    def __init__(self):
        self.__aliyun_id = ""  # 个人用户 id
        self.__aliyun_secret = ""  # 个人用户 secret


    def __time(self):
        time = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
        return date


    def __md5_base64(self, body):
        hash = hashlib.md5()
        hash.update(body)
        str_hex = hash.digest()
        return base64.b64encode(str_hex)


    def __sha1_base64(self, str_to_sign, secret):
        hmacsha1 = hmac.new(secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha1)
        return base64.b64encode(hmacsha1.digest()).decode('utf-8')


    def send_request(self, phrase):
        options = {'method': 'POST',
                   'url': 'http://nlsapi.aliyun.com/speak?encode_type=mp3&voice_name=xiaoyun&volume=50',
                   'body': phrase.encode('utf-8')
                   }
        headers = {'Authorization': '','Content-type': 'text/plain','Accept': 'audio/mp3,application/json','Date': self.__time()}
		
        MD5 = self.__md5_base64(options['body']).decode('utf-8')
		
        feature = options['method'] + '\n' + headers['Accept'] + '\n' + MD5 + '\n' \
                  + headers['Content-type'] + '\n' + headers['Date']
        signa = self.__sha1_base64(feature, self.__aliyun_secret)


        headers['Authorization'] = "Dataplus " + self.__aliyun_id + ":" + signa


        request = requests.post(options['url'], data=options['body'], headers=headers)
        with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as file:
            file.write(request.content)
            tmpfile = file.name
            return tmpfile




if __name__ == '__main__':
    a = aliyun()
    text="统一这一任务对欧洲来说显然要艰巨得多。"
    file=a.send_request(text) 
    shutil.move(file,"test.mp3") # 文件位置

猜你喜欢

转载自blog.csdn.net/peyte1/article/details/80509120