Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648170
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-09-23 11:55:46

#!/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 = '欢迎使用标贝开发平台。'

    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 = 跟单网gendan5.comget_audio(data)

        #保存音频文件

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

            audio.write(content)

        print("task finished successfully")

    except Exception as e:

        print(e)

阅读(696) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~