"""
本示例用于快速克隆音色并获取试听文件。
注意:需要先将密钥信息写入环境变量 `MINIMAX_API_KEY`,
并将"<your_custom_voice_id>"替换为您定义的音色 id。
"""
import json
import requests
import os
api_key = os.getenv("MINIMAX_API_KEY")
upload_url = "https://api.minimaxi.com/v1/files/upload"
clone_url = "https://api.minimaxi.com/v1/voice_clone"
headers = {"Authorization": f"Bearer {api_key}"}
with open("/path/to/clone_input.mp3", "rb") as f:
files = {"file": ("clone_input.mp3", f)}
data = {"purpose": "voice_clone"}
response = requests.post(upload_url, headers=headers, data=data, files=files)
file_id = response.json()["file"]["file_id"]
print(f"File ID of the cloned audio: {file_id}")
# 2. 上传示例音频
with open("/path/to/clone_prompt.mp3", "rb") as f:
files = {"file": ("clone_prompt.mp3", f)}
data = {"purpose": "prompt_audio"}
response = requests.post(upload_url, headers=headers, data=data, files=files)
prompt_file_id = response.json()["file"]["file_id"]
print(f"File ID of the prompt audio: {prompt_file_id}")
# 3. 进行音色克隆
clone_payload = {
"file_id": file_id,
"voice_id": "<your_custom_voice_id>",
"clone_prompt": {
"prompt_audio": prompt_file_id,
"prompt_text": "后来认为啊,是有人抓这鸡,可是抓鸡的地方呢没人听过鸡叫。"
},
"text": "大兄弟,听您口音不是本地人吧,头回来天津卫,啊,待会您可甭跟着导航走,那玩意儿净给您往大马路上绕。",
"model": "speech-2.6-hd"
}
clone_headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
response = requests.post(clone_url, headers=clone_headers, json=clone_payload)
print(response.text)