[ PROMPT_NODE_24846 ]
Heygen Best Practices 资源
[ SKILL_DOCUMENTATION ]
# 资源上传与管理
HeyGen 允许你上传自定义资源(图像、视频、音频)以用于视频生成,例如背景、会说话的照片源以及自定义音频。
## 上传流程
资源上传使用两步流程:
1. 从 HeyGen 获取预签名上传 URL
2. 将文件上传到预签名 URL
## 获取上传 URL
### 请求字段
| 字段 | 类型 | 必填 | 描述 |
|-------|------|:---:|-------------|
| `content_type` | string | ✓ | 待上传文件的 MIME 类型 |
### curl
bash
curl -X POST "https://api.heygen.com/v1/asset"
-H "X-Api-Key: $HEYGEN_API_KEY"
-H "Content-Type: application/json"
-d '{"content_type": "image/jpeg"}'
### TypeScript
typescript
interface AssetUploadRequest {
content_type: string; // 必填
}
interface AssetUploadResponse {
error: null | string;
data: {
url: string;
asset_id: string;
};
}
async function getUploadUrl(contentType: string): Promise {
const response = await fetch("https://api.heygen.com/v1/asset", {
method: "POST",
headers: {
"X-Api-Key": process.env.HEYGEN_API_KEY!,
"Content-Type": "application/json",
},
body: JSON.stringify({ content_type: contentType }),
});
const json: AssetUploadResponse = await response.json();
if (json.error) {
throw new Error(json.error);
}
return json.data;
}
### Python
python
import requests
import os
def get_upload_url(content_type: str) -> dict:
response = requests.post(
"https://api.heygen.com/v1/asset",
headers={
"X-Api-Key": os.environ["HEYGEN_API_KEY"],
"Content-Type": "application/json"
},
json={"content_type": content_type}
)
data = response.json()
if data.get("error"):
raise Exception(data["error"])
return data["data"]
## 支持的内容类型
| 类型 | Content-Type | 使用场景 |
|------|--------------|----------|
| JPEG | `image/jpeg` | 背景、会说话的照片 |
| PNG | `image/png` | 背景、覆盖层 |
| MP4 | `video/mp4` | 视频背景 |
| MP3 | `audio/mpeg` | 自定义音频输入 |
| WAV | `audio/wav` | 自定义音频输入 |
## 上传文件
### TypeScript
typescript
import fs from "fs";
async function uploadFile(filePath: string, contentType: string): Promise {
// 1. 获取上传 URL
const { url, asset_id } = await getUploadUrl(contentType);
// 2. 读取文件
const fil