[ PROMPT_NODE_24854 ]
captions
[ SKILL_DOCUMENTATION ]
# 视频字幕
HeyGen 可以为您的视频自动生成字幕(副标题),从而提高可访问性和用户参与度。
## 启用字幕
在生成视频时可以启用字幕:
typescript
const videoConfig = {
video_inputs: [
{
character: {
type: "avatar",
avatar_id: "josh_lite3_20230714",
avatar_style: "normal",
},
voice: {
type: "text",
input_text: "Hello! This video will have automatic captions.",
voice_id: "1bd001e7e50f421d891986aad5158bc8",
},
},
],
// 字幕设置(可用性因套餐而异)
caption: true,
};
## 字幕配置选项
typescript
interface CaptionConfig {
// 启用/禁用字幕
enabled: boolean;
// 字幕样式
style?: {
font_family?: string;
font_size?: number;
font_color?: string;
background_color?: string;
position?: "top" | "bottom";
};
// 字幕生成的语言
language?: string;
}
## 字幕样式
### 基础字幕
typescript
const config = {
video_inputs: [...],
caption: true, // 使用默认样式启用
};
### 自定义样式字幕
typescript
const config = {
video_inputs: [...],
caption: {
enabled: true,
style: {
font_family: "Arial",
font_size: 32,
font_color: "#FFFFFF",
background_color: "rgba(0, 0, 0, 0.7)",
position: "bottom",
},
},
};
## 多语言字幕
对于不同语言的视频,字幕会根据语音语言自动生成:
typescript
// 西班牙语视频及西班牙语字幕
const spanishConfig = {
video_inputs: [
{
character: {
type: "avatar",
avatar_id: "josh_lite3_20230714",
avatar_style: "normal",
},
voice: {
type: "text",
input_text: "¡Hola! Este video tendrá subtítulos en español.",
voice_id: "spanish_voice_id",
},
},
],
caption: true,
};
## 使用 SRT 文件
### 下载 SRT
视频生成后,您可以下载 SRT 文件:
typescript
async function downloadSrt(videoId: string): Promise {
const response = await fetch(
`https://api.heygen.com/v1/video/${videoId}/srt`,
{ headers: { "X-Api-Key": process.env.HEYGEN_API_KEY! } }
);
if (!response.ok) {
throw new Error("Failed to download SRT");
}
return response.text();
}
### SRT 文件格式
标准 SRT 格式:
srt
1
00:00:0