tts_help.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: UTF-8 -*-
  2. # Python 2.x引入httplib模块。
  3. # import httplib
  4. # Python 3.x引入http.client模块。
  5. import http.client
  6. # Python 2.x引入urllib模块。
  7. # import urllib
  8. # Python 3.x引入urllib.parse模块。
  9. import urllib.parse
  10. import json
  11. def processGETRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
  12. host = 'nls-gateway-cn-shanghai.aliyuncs.com'
  13. url = 'https://' + host + '/stream/v1/tts'
  14. # 设置URL请求参数
  15. url = url + '?appkey=' + appKey
  16. url = url + '&token=' + token
  17. url = url + '&text=' + text
  18. url = url + '&format=' + format
  19. url = url + '&sample_rate=' + str(sampleRate)
  20. # voice 发音人,可选,默认是xiaoyun。
  21. # url = url + '&voice=' + 'xiaoyun'
  22. # volume 音量,范围是0~100,可选,默认50。
  23. # url = url + '&volume=' + str(50)
  24. # speech_rate 语速,范围是-500~500,可选,默认是0。
  25. # url = url + '&speech_rate=' + str(0)
  26. # pitch_rate 语调,范围是-500~500,可选,默认是0。
  27. # url = url + '&pitch_rate=' + str(0)
  28. print(url)
  29. # Python 2.x请使用httplib。
  30. # conn = httplib.HTTPSConnection(host)
  31. # Python 3.x请使用http.client。
  32. conn = http.client.HTTPSConnection(host)
  33. conn.request(method='GET', url=url)
  34. # 处理服务端返回的响应。
  35. response = conn.getresponse()
  36. print('Response status and response reason:')
  37. print(response.status ,response.reason)
  38. contentType = response.getheader('Content-Type')
  39. print(contentType)
  40. body = response.read()
  41. if 'audio/mpeg' == contentType :
  42. with open(audioSaveFile, mode='wb') as f:
  43. f.write(body)
  44. print('The GET request succeed!')
  45. else :
  46. print('The GET request failed: ' + str(body))
  47. conn.close()
  48. def processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
  49. host = 'nls-gateway-cn-shanghai.aliyuncs.com'
  50. url = 'https://' + host + '/stream/v1/tts'
  51. # 设置HTTPS Headers。
  52. httpHeaders = {
  53. 'Content-Type': 'application/json'
  54. }
  55. # 设置HTTPS Body。
  56. body = {'appkey': appKey, 'token': token, 'text': text, 'format': format, 'sample_rate': sampleRate}
  57. body = json.dumps(body)
  58. print('The POST request body content: ' + body)
  59. # Python 2.x请使用httplib。
  60. # conn = httplib.HTTPSConnection(host)
  61. # Python 3.x请使用http.client。
  62. conn = http.client.HTTPSConnection(host)
  63. conn.request(method='POST', url=url, body=body, headers=httpHeaders)
  64. # 处理服务端返回的响应。
  65. response = conn.getresponse()
  66. print('Response status and response reason:')
  67. print(response.status ,response.reason)
  68. contentType = response.getheader('Content-Type')
  69. print(contentType)
  70. body = response.read()
  71. if 'audio/mpeg' == contentType :
  72. with open(audioSaveFile, mode='wb') as f:
  73. f.write(body)
  74. print('The POST request succeed!')
  75. else :
  76. print('The POST request failed: ' + str(body))
  77. conn.close()
  78. appKey = 'KZChElSawT5Zue3n'
  79. token = 'fbb04d8f368e4394ac7172a1225c0720'
  80. text = '今天是周五,天气挺好的。'
  81. # 采用RFC 3986规范进行urlencode编码。
  82. textUrlencode = text
  83. # Python 2.x请使用urllib.quote。
  84. # textUrlencode = urllib.quote(textUrlencode, '')
  85. # Python 3.x请使用urllib.parse.quote_plus。
  86. textUrlencode = urllib.parse.quote_plus(textUrlencode)
  87. textUrlencode = textUrlencode.replace("+", "%20")
  88. textUrlencode = textUrlencode.replace("*", "%2A")
  89. textUrlencode = textUrlencode.replace("%7E", "~")
  90. print('text: ' + textUrlencode)
  91. audioSaveFile = '/Users/tzld/Desktop/video_rewriting/common/syAudio.MP3'
  92. format = 'MP3'
  93. sampleRate = 16000
  94. # GET请求方式
  95. processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)
  96. # POST请求方式
  97. # processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate)