声音情绪分析 · 情绪描述 · 同步
同步音频最长 15 秒;更长音频请使用预录音 · PRE。
服务路径
服务地址: https://api.fourhz.cn
| 方法 / 协议 | 路径 | 内容类型 / 帧格式 | 鉴权 |
|---|---|---|---|
| POST | /v1/emotion-captioning/sync | multipart/form-data | Authorization: <API Key> |
请求参数
同步返回最终结果,HTTP 200;不返回待查询的 task_id。仅支持 PCM WAV,音频 80 ms–15 秒,文件不超过 20 MiB。服务端处理最长等待 120 秒。正常同步调用直接返回最终结果,不需要轮询;超时或断开不代表处理已取消,不要盲目重试。若响应提供 job_id 或 X-Job-Id,请保留用于排查;只有获得任务 ID 时才可查询对应任务。
audio:音频文件;mode:sec(默认,自然语言描述)或 ser(标签分类);language:结果语言提示,例如 zh 或 en。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
audio | file | 是 | 待分析的 PCM WAV 音频文件,80 ms–15 秒,最大 20 MiB。 |
mode | string | 否 | sec(默认,自然语言描述)或 ser(标签分类)。 |
language | string | 否 | 结果语言提示,例如 zh 或 en。 |
响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
status | string | 成功时为 succeeded。 |
result | object | 最终语气分析结果。 |
result.type | string? | final_emotion 表示一段情感结果。 |
result.mode | string | sec 为自然语言描述,ser 为标签分类。 |
result.text | string? | 模型返回的语气描述。 |
result.label | string? | 模型返回的情绪标签;缺失不等于中性。 |
result.duration_sec | number | 处理时长,秒。 |
result.language | string? | 服务返回的结果语言。 |
请求示例
Python 使用 requests;JavaScript 文件示例使用 Node.js 22+。长期 API Key 只用于服务端。
curl -X POST 'https://api.fourhz.cn/v1/emotion-captioning/sync' \
-H "Authorization: $FOURHZ_API_KEY" \
-F 'audio=@audio.wav' \
-F 'mode=sec' \
-F 'language=zh'# pip install requests
import os
import requests
from contextlib import ExitStack
url = "https://api.fourhz.cn/v1/emotion-captioning/sync"
headers = {"Authorization": os.environ["FOURHZ_API_KEY"]}
with ExitStack() as stack:
files = [
("audio", ("audio.wav", stack.enter_context(open("audio.wav", "rb"))))
]
fields = {"mode":"sec","language":"zh"}
response = requests.request("POST", url, headers=headers, timeout=300, files=files, data=fields)
response.raise_for_status()
print(response.json() if response.content else {"status": response.status_code})// Node.js 22+
import {readFile} from 'node:fs/promises';
const url = "https://api.fourhz.cn/v1/emotion-captioning/sync";
const headers = {"Authorization": process.env.FOURHZ_API_KEY};
const body = new FormData();
body.append("audio", new Blob([await readFile("audio.wav")]), "audio.wav");
body.append("mode", "sec");
body.append("language", "zh");
const response = await fetch(url, {method: "POST", headers, body, signal: AbortSignal.timeout(300000)});
if (!response.ok) throw new Error(await response.text());
console.log(response.status === 204 ? {status: 204} : await response.json());响应示例
HTTP 200,直接返回最终结果,无需轮询。分数与文本仅用于说明响应结构。
{
"status": "succeeded",
"result": {
"type": "final_emotion",
"mode": "sec",
"label": "Happy",
"text": "The speaker sounds excited and cheerful.",
"duration_sec": 2.68,
"language": "en"
}
}错误响应
所有 HTTP 错误正文都包含 error_code(机器可读的错误码)与 message(可读说明);历史字段(如 detail、code/msg、上游的 error 对象)同时保留,状态码不变。客户端请优先读取 error_code 与 message。
常见状态:400 参数或音频无效、401 鉴权失败、402 余额不足、413 文件过大、415 文件内容不是受支持的音频、429 限流或容量已满(遵循 Retry-After)、503 服务未配置或不可用。