import base64
import json
import time
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.asr.v20190614 import asr_client, models
def send_task(path):
try:
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "asr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = asr_client.AsrClient(cred, "", clientProfile)
req = models.CreateRecTaskRequest()
fwave = open(path, mode='rb').read()
dataLen = len(fwave)
base64Wav = base64.b64encode(fwave).decode('utf8')
params = {
"EngineModelType": "8k_en",
"ChannelNum": 1,
"ResTextFormat": 0,
"SourceType": 1,
"Data": base64Wav, "DataLen": dataLen
}
req.from_json_string(json.dumps(params))
resp = client.CreateRecTask(req)
print(resp.to_json_string())
return json.loads(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
def get_task(t_id):
try:
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "asr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = asr_client.AsrClient(cred, "", clientProfile)
req = models.DescribeTaskStatusRequest()
params = {
"TaskId": int(t_id)
}
req.from_json_string(json.dumps(params))
resp = client.DescribeTaskStatus(req)
print(resp.to_json_string())
return resp.to_json_string()
except TencentCloudSDKException as err:
print(err)
def get_mp3_res_tengxun(path):
data = send_task(path)
for i in range(1, 15):
d = get_task(data.get('Data').get('TaskId'))
if d:
d = json.loads(d)
if d.get('Data').get('StatusStr') == 'success':
return d.get('Data').get('Result').split('] ')[-1].replace('\n', '')
time.sleep(2)
return None
改成自己的 信息就行