fileTrans.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf8 -*-
  2. import json
  3. import time
  4. import os
  5. import re
  6. from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
  7. from aliyunsdkcore.client import AcsClient
  8. from aliyunsdkcore.request import CommonRequest
  9. def fileTrans(akId, akSecret, appKey, fileLink):
  10. # 地域ID,固定值。
  11. REGION_ID = "cn-beijing"
  12. PRODUCT = "nls-filetrans"
  13. DOMAIN = "filetrans.cn-beijing.aliyuncs.com"
  14. API_VERSION = "2018-08-17"
  15. POST_REQUEST_ACTION = "SubmitTask"
  16. GET_REQUEST_ACTION = "GetTaskResult"
  17. # 请求参数
  18. KEY_APP_KEY = "appkey"
  19. KEY_FILE_LINK = "file_link"
  20. KEY_VERSION = "version"
  21. KEY_ENABLE_WORDS = "enable_words"
  22. # 是否开启智能分轨
  23. KEY_AUTO_SPLIT = "auto_split"
  24. # 响应参数
  25. KEY_TASK = "Task"
  26. KEY_TASK_ID = "TaskId"
  27. KEY_STATUS_TEXT = "StatusText"
  28. KEY_RESULT = "Result"
  29. # 状态值
  30. STATUS_SUCCESS = "SUCCESS"
  31. STATUS_RUNNING = "RUNNING"
  32. STATUS_QUEUEING = "QUEUEING"
  33. # 创建AcsClient实例
  34. client = AcsClient(akId, akSecret, REGION_ID)
  35. # 提交录音文件识别请求
  36. postRequest = CommonRequest()
  37. postRequest.set_domain(DOMAIN)
  38. postRequest.set_version(API_VERSION)
  39. postRequest.set_product(PRODUCT)
  40. postRequest.set_action_name(POST_REQUEST_ACTION)
  41. postRequest.set_method('POST')
  42. task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True}
  43. task = json.dumps(task)
  44. postRequest.add_body_params(KEY_TASK, task)
  45. taskId = ""
  46. try:
  47. postResponse = client.do_action_with_exception(postRequest)
  48. postResponse = json.loads(postResponse)
  49. statusText = postResponse[KEY_STATUS_TEXT]
  50. if statusText == STATUS_SUCCESS:
  51. taskId = postResponse[KEY_TASK_ID]
  52. else:
  53. print(f"录音文件 {fileLink} 识别请求失败!")
  54. return None
  55. except ServerException as e:
  56. print(e)
  57. return None
  58. except ClientException as e:
  59. print(e)
  60. return None
  61. # 创建CommonRequest,设置任务ID。
  62. getRequest = CommonRequest()
  63. getRequest.set_domain(DOMAIN)
  64. getRequest.set_version(API_VERSION)
  65. getRequest.set_product(PRODUCT)
  66. getRequest.set_action_name(GET_REQUEST_ACTION)
  67. getRequest.set_method('GET')
  68. getRequest.add_query_param(KEY_TASK_ID, taskId)
  69. # 提交录音文件识别结果查询请求
  70. statusText = ""
  71. while True:
  72. try:
  73. getResponse = client.do_action_with_exception(getRequest)
  74. getResponse = json.loads(getResponse)
  75. statusText = getResponse[KEY_STATUS_TEXT]
  76. if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING:
  77. time.sleep(10)
  78. else:
  79. break
  80. except ServerException as e:
  81. print(e)
  82. return None
  83. except ClientException as e:
  84. print(e)
  85. return None
  86. if statusText == STATUS_SUCCESS:
  87. result = getResponse[KEY_RESULT]
  88. return result
  89. else:
  90. print(f"录音文件 {fileLink} 识别失败!")
  91. return None
  92. def batchProcessFiles(akId, akSecret, appKey, baseUrl, fileNames, outputFile):
  93. results = {}
  94. for fileName in fileNames:
  95. fileLink = f"{baseUrl}{fileName}"
  96. print(f"处理文件:{fileLink}")
  97. result = fileTrans(akId, akSecret, appKey, fileLink)
  98. if result:
  99. fileName=re.sub(r"\.wav$", "", fileName)
  100. results[fileName] = result['Sentences']
  101. print("-" * 40)
  102. # 确保目录存在
  103. outputDir = os.path.dirname(outputFile)
  104. if not os.path.exists(outputDir):
  105. os.makedirs(outputDir)
  106. # 将结果写入 JSON 文件
  107. with open(outputFile, 'w', encoding='utf8') as f:
  108. json.dump(results, f, ensure_ascii=False, indent=4)
  109. print(f"所有结果已保存到 {outputFile}")
  110. if __name__ == "__main__":
  111. accessKeyId = "LTAI5tQ2HmiHCygZkt5BYrYR"
  112. accessKeySecret = "KhmxTd14SUcXafpFk5yofA43FoeM99"
  113. appKey = "OKt6jogp6fRjHQVp"
  114. # 录音文件基础 URL
  115. baseUrl = "https://static.fuxicarbon.com/fullVoice/"
  116. # 录音文件名称列表(假设你已经知道或生成了文件名)
  117. fileNames = [
  118. "230027-I-0155-18804546916-S.wav",
  119. # 添加更多文件名称
  120. ]
  121. # 输出 JSON 文件名称
  122. outputFile = "/Users/yushanghui/hongshantianping/git/dataTools/voice/results.json"
  123. # 执行批量处理
  124. batchProcessFiles(accessKeyId, accessKeySecret, appKey, baseUrl, fileNames, outputFile)