需求描述

  1. 使用python3调用百度tts,生成mp3文件
  2. 调用ffmpeg将mp3转为8k8bit的wav文件,用于FreeSWITCH的播放

实现步骤

申请百度tts账号

参考:链接

python实现代码

baidutts.pytts.py

文件:baidutts.py

  1. # coding=utf-8
  2. import sys
  3. import json
  4. from urllib.request import urlopen
  5. from urllib.request import Request
  6. from urllib.error import URLError
  7. from urllib.parse import urlencode
  8. from urllib.parse import quote_plus
  9. IS_PY3 = sys.version_info.major == 3
  10. # 替换你的 API_KEY
  11. API_KEY = 'zqyXXkMmH0grrVh7N5tPPxxx'
  12. # 替换你的 SECRET_KEY
  13. SECRET_KEY = 'KV7iADpCqm19q8xj9XkvaKZuHEorXxxx'
  14. TTS_URL = 'http://tsn.baidu.com/text2audio'
  15. """ TOKEN start """
  16. TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
  17. """
  18. 获取token
  19. """
  20. def fetch_token():
  21. params = {'grant_type': 'client_credentials',
  22. 'client_id': API_KEY,
  23. 'client_secret': SECRET_KEY}
  24. post_data = urlencode(params)
  25. if (IS_PY3):
  26. post_data = post_data.encode('utf-8')
  27. req = Request(TOKEN_URL, post_data)
  28. try:
  29. f = urlopen(req, timeout=5)
  30. result_str = f.read()
  31. except URLError as err:
  32. print('token http response http code : ' + str(err.code))
  33. result_str = err.read()
  34. if (IS_PY3):
  35. result_str = result_str.decode()
  36. result = json.loads(result_str)
  37. if 'access_token' in result.keys() and 'scope' in result.keys():
  38. if not 'audio_tts_post' in result['scope'].split(' '):
  39. print('please ensure has check the tts ability')
  40. exit()
  41. return result['access_token']
  42. else:
  43. print('please overwrite the correct API_KEY and SECRET_KEY')
  44. exit()
  45. """ TOKEN end """
  46. def get_tts(myText, fileName):
  47. token = fetch_token()
  48. tex = quote_plus(myText) # 此处TEXT需要两次urlencode
  49. params = {'tok': token, 'tex': tex, 'cuid': "quickstart",
  50. 'lan': 'zh', 'ctp': 1} # lan ctp 固定参数
  51. data = urlencode(params)
  52. req = Request(TTS_URL, data.encode('utf-8'))
  53. has_error = False
  54. try:
  55. f = urlopen(req)
  56. result_str = f.read()
  57. headers = dict((name.lower(), value) for name, value in f.headers.items())
  58. has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
  59. except URLError as err:
  60. print('http response http code : ' + str(err.code))
  61. result_str = err.read()
  62. has_error = True
  63. save_file = "error.txt" if has_error else fileName
  64. with open(save_file, 'wb') as of:
  65. of.write(result_str)
  66. if has_error:
  67. if IS_PY3:
  68. result_str = str(result_str, 'utf-8')
  69. print("tts api error:" + result_str)
  70. print("file saved as : " + save_file)

文件:tts.py

  1. from baidutts import get_tts
  2. import os
  3. text = "你好,你来自什么地方,那个地方漂亮吗"
  4. filemp3 = 'media/text.mp3'
  5. # 输出的8k8bit的文件
  6. finalwav = 'media/my.wav'
  7. # 使用百度tts生成对应的mp3文件
  8. get_tts(text, filemp3)
  9. # 将mp3转为8k8bit的wav文件
  10. cmd = f'ffmpeg -i {filemp3} -ar 8000 -ac 1 -acodec pcm_u8 {finalwav}'
  11. os.system(cmd)
  12. # 删除生成的中间文件
  13. os.remove(filemp3)