文本转语音接口的应用
需求
做视频需要配音,机器音即可
代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import os
def TTS(text,f_path):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
result = requests.get(f'https://tts.youdao.com/fanyivoice?word={text}&le=zh&keyfrom=speaker-target',headers = headers)
with open(f_path, 'wb+') as f:
f.write(result.content) # 在同级目录写入为mp3文件
return f_path
if __name__ == '__main__':
try:
bindata = TTS(text='''测试一下,你好世界!我是闫超.Hello,world!This is Yanchao.''',f_path='test.mp3')
except:
print('Error')
else:
os.startfile('test.mp3') #自动运行生成的mp3