如何用Python語音合成,以及文字轉語音~

專注的阿熊發表於2022-09-23

#!/usr/bin/env python

# coding: utf-8

import requests

import json

import argparse

# 獲取 access_token 用於鑑權

def get_access_token(client_secret, client_id):

     grant_type = "client_credentials"

     url = "{}&client_secret={}&client_id={}".format(grant_type, client_secret, client_id)

     response = requests.post(url)

     access_token = json.loads(response.text).get('access_token')

     return access_token

# 獲取轉換後音訊

def get_audio(data):

     url = "{}&domain={}&language={}&voice_name={}&text={}&audiotype={}".format(data['access_domain'], data['domain'], data['language'], data['voice_name'], data['text'], data['audiotype'])

     response = requests.post(url)

     content_type = response.headers['Content-Type']

     if 'audio' not in content_type:

         raise Exception(response.text)

     return response.content

# 獲取命令列輸入引數

def get_args():

     text = 外匯跟單gendan5.com' 歡迎使用標貝開發平臺。 '

     parser = argparse.ArgumentParser(description='ASR')

     parser.add_argument('-client_secret', type=str, required=True)

     parser.add_argument('-client_id', type=str, required=True)

     parser.add_argument('-file_save_path', type=str, required=True)

     parser.add_argument('--text', type=str, default=text)

     parser.add_argument('--audiotype', type=str, default='6')

     parser.add_argument('--domain', type=str, default='1')

     parser.add_argument('--language', type=str, default='zh')

     parser.add_argument('--voice_name', type=str, default='Lingling')

     args = parser.parse_args()

     return args

if __name__ == '__main__':

     try:

         args = get_args()

         # 獲取 access_token

         client_secret = args.client_secret

         client_id = args.client_id

         access_token = get_access_token(client_secret, client_id)

         # 讀取引數

         audiotype = args.audiotype

         domain = args.domain

         language = args.language

         voice_name = args.voice_name

         text = args.text

         data = {'access_domain': access_token, 'audiotype': audiotype, 'domain': domain, 'language': language,

                 'voice_name': voice_name, 'text': text}

         content = get_audio(data)

         # 儲存音訊檔案

         with open('test.wav', 'wb') as audio:

             audio.write(content)

         print("task finished successfully")

     except Exception as e:

         print(e)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2915928/,如需轉載,請註明出處,否則將追究法律責任。

相關文章