'commit'
This commit is contained in:
Binary file not shown.
@@ -50,11 +50,37 @@ request_json = {
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
resp = requests.post(api_url, json.dumps(request_json), headers=header)
|
resp = requests.post(api_url, json=request_json, headers=header)
|
||||||
print(f"resp body: \n{resp.json()}")
|
print(f"resp body: {resp.json()}")
|
||||||
if "data" in resp.json():
|
|
||||||
data = resp.json()["data"]
|
# 检查响应状态
|
||||||
file_to_save = open("test_submit.mp3", "wb")
|
if resp.status_code == 200:
|
||||||
file_to_save.write(base64.b64decode(data))
|
resp_data = resp.json()
|
||||||
|
if resp_data.get("code") == 3000 and "data" in resp_data:
|
||||||
|
data = resp_data["data"]
|
||||||
|
|
||||||
|
# 检查data是否为空
|
||||||
|
if data:
|
||||||
|
try:
|
||||||
|
# 解码Base64数据
|
||||||
|
audio_data = base64.b64decode(data)
|
||||||
|
|
||||||
|
# 保存为MP3文件
|
||||||
|
output_file = "test_tts_output.mp3"
|
||||||
|
with open(output_file, "wb") as f:
|
||||||
|
f.write(audio_data)
|
||||||
|
print(f"音频文件已成功保存为: {output_file}")
|
||||||
|
except Exception as decode_error:
|
||||||
|
print(f"Base64解码或文件保存失败: {str(decode_error)}")
|
||||||
|
# 打印数据前100个字符,以便调试
|
||||||
|
print(f"数据前100个字符: {data[:100]}")
|
||||||
|
else:
|
||||||
|
print("错误: 返回的data字段为空")
|
||||||
|
else:
|
||||||
|
print(f"API返回错误: code={resp_data.get('code')}, message={resp_data.get('message')}")
|
||||||
|
else:
|
||||||
|
print(f"HTTP请求失败: 状态码={resp.status_code}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
e.with_traceback()
|
print(f"发生异常: {str(e)}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
Reference in New Issue
Block a user