audio.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. from .__load__ import *
  3. import json
  4. import time
  5. from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
  6. from aliyunsdkcore.client import AcsClient
  7. from aliyunsdkcore.request import CommonRequest
  8. class Audio(Base):
  9. def __init__(self):
  10. self.akId = 'LTAI5tCFiVxuXz39MMkXFcMm'
  11. self.akSecret = 'sdK3jVSrrqzz2nONAGyd6kvXZZwkie'
  12. self.appKey = 'm0mDna21AWao7b0A'
  13. self.region = 'cn-beijing'
  14. self.client = AcsClient(self.akId, self.akSecret, self.region)
  15. # 固定配置
  16. self.PRODUCT = 'nls-filetrans'
  17. self.DOMAIN = f'filetrans.{self.region}.aliyuncs.com'
  18. self.API_VERSION = '2018-08-17'
  19. self.POST_REQUEST_ACTION = 'SubmitTask'
  20. self.GET_REQUEST_ACTION = 'GetTaskResult'
  21. def json(self):
  22. if not self.file:
  23. return False
  24. # 提交任务
  25. task = {
  26. 'appkey': self.appKey,
  27. 'file_link': self.file,
  28. 'version': '4.0',
  29. 'enable_words': True,
  30. 'auto_split': True,
  31. }
  32. postRequest = CommonRequest()
  33. postRequest.set_domain(self.DOMAIN)
  34. postRequest.set_version(self.API_VERSION)
  35. postRequest.set_product(self.PRODUCT)
  36. postRequest.set_action_name(self.POST_REQUEST_ACTION)
  37. postRequest.set_method('POST')
  38. postRequest.add_body_params('Task', json.dumps(task))
  39. try:
  40. postResponse = self.client.do_action_with_exception(postRequest)
  41. postResponse = json.loads(postResponse)
  42. if postResponse.get('StatusText') != 'SUCCESS':
  43. raise Exception(f'提交任务失败: {postResponse}')
  44. taskId = postResponse['TaskId']
  45. except (ServerException, ClientException) as e:
  46. raise Exception(f'提交任务异常: {str(e)}')
  47. # 查询任务结果
  48. getRequest = CommonRequest()
  49. getRequest.set_domain(self.DOMAIN)
  50. getRequest.set_version(self.API_VERSION)
  51. getRequest.set_product(self.PRODUCT)
  52. getRequest.set_action_name(self.GET_REQUEST_ACTION)
  53. getRequest.set_method('GET')
  54. getRequest.add_query_param('TaskId', taskId)
  55. statusText = ''
  56. result_json = {}
  57. while True:
  58. try:
  59. getResponse = self.client.do_action_with_exception(getRequest)
  60. getResponse = json.loads(getResponse)
  61. statusText = getResponse.get('StatusText', '')
  62. if statusText in ['RUNNING', 'QUEUEING']:
  63. time.sleep(5)
  64. continue
  65. else:
  66. result_json = getResponse
  67. break
  68. except (ServerException, ClientException) as e:
  69. raise Exception(f'查询任务异常: {str(e)}')
  70. if statusText != 'SUCCESS':
  71. raise Exception(f'识别失败: {result_json}')
  72. # 处理成单词级别 SRT/WebVTT 风格 JSON
  73. final_result = []
  74. if 'Result' in result_json and 'Words' in result_json['Result']:
  75. for w in result_json['Result']['Words']:
  76. start = w.get('BeginTime', 0) / 1000.0 # 毫秒转秒
  77. end = w.get('EndTime', 0) / 1000.0
  78. text = w.get('Word', '').strip()
  79. if text:
  80. final_result.append({
  81. "start": start,
  82. "end": end,
  83. "text": text
  84. })
  85. return final_result