Remotion Captions — 為影片加入動畫字幕
從 .SRT 檔案為 Remotion 影片添加動畫字幕與文字說明。支援 TikTok 風格的逐字高亮、卡拉 OK 特效等。
總覽
- 儲存庫: github.com/ahgsql/remotion-subtitles
- 語言: TypeScript / React
- 授權: 開源
- 安裝:
npm install remotion-subtitles - 需求: 需有 Remotion 專案
安裝
npm install remotion-subtitles
基本用法
從 SRT 檔案匯入
import { useSubtitles, SubtitleSequence } from "remotion-subtitles";
export const CaptionedVideo: React.FC = () => {
const subtitles = useSubtitles("./subtitles.srt");
return (
<AbsoluteFill>
<Video src={staticFile("video.mp4")} />
<SubtitleSequence subtitles={subtitles} />
</AbsoluteFill>
);
};
SRT 格式範例
1
00:00:00,000 --> 00:00:02,500
Welcome to Custom Agents
2
00:00:02,500 --> 00:00:05,000
The AI-powered email platform
3
00:00:05,000 --> 00:00:08,000
that handles your inbox automatically
字幕樣式設定
<SubtitleSequence
subtitles={subtitles}
style={{
position: "absolute",
bottom: 80,
left: 0,
right: 0,
textAlign: "center",
fontSize: 42,
fontWeight: "bold",
color: "white",
textShadow: "2px 2px 8px rgba(0,0,0,0.8)",
fontFamily: "Inter, sans-serif",
}}
/>
逐字高亮(TikTok 風格)
import { useCurrentFrame, interpolate } from "remotion";
export const WordHighlightCaption: React.FC<{
words: { text: string; startFrame: number; endFrame: number }[];
}> = ({ words }) => {
const frame = useCurrentFrame();
return (
<div style={{
position: "absolute",
bottom: 100,
width: "100%",
textAlign: "center",
display: "flex",
justifyContent: "center",
gap: 8,
flexWrap: "wrap",
padding: "0 60px",
}}>
{words.map((word, i) => {
const isActive = frame >= word.startFrame && frame <= word.endFrame;
const scale = isActive
? interpolate(frame, [word.startFrame, word.startFrame + 3], [1, 1.15], { extrapolateRight: "clamp" })
: 1;
return (
<span
key={i}
style={{
fontSize: 48,
fontWeight: 800,
color: isActive ? "#FFD700" : "white",
textShadow: isActive
? "0 0 20px rgba(255,215,0,0.5)"
: "2px 2px 4px rgba(0,0,0,0.8)",
transform: `scale(${scale})`,
transition: "color 0.1s",
display: "inline-block",
}}
>
{word.text}
</span>
);
})}
</div>
);
};
手動設定字幕時間軸
如果你沒有 SRT 檔案,也可以手動定義字幕:
const captions = [
{ text: "Welcome to Custom Agents", startFrame: 0, endFrame: 75 },
{ text: "AI-powered email automation", startFrame: 80, endFrame: 150 },
{ text: "Create agents in minutes", startFrame: 155, endFrame: 240 },
];
export const ManualCaptions: React.FC = () => {
const frame = useCurrentFrame();
const current = captions.find(
(c) => frame >= c.startFrame && frame <= c.endFrame
);
if (!current) return null;
const progress = interpolate(
frame,
[current.startFrame, current.startFrame + 10],
[0, 1],
{ extrapolateRight: "clamp" }
);
return (
<div style={{
position: "absolute",
bottom: 80,
width: "100%",
textAlign: "center",
opacity: progress,
transform: `translateY(${(1 - progress) * 10}px)`,
}}>
<span style={{
fontSize: 44,
fontWeight: 700,
color: "white",
backgroundColor: "rgba(0,0,0,0.6)",
padding: "8px 24px",
borderRadius: 8,
}}>
{current.text}
</span>
</div>
);
};
最佳實踐
- 放置於底部 — 標準字幕位置為底部中央
- 使用對比色 — 白色文字搭配深色陰影或半透明背景
- 大字體 — 最小 40-50px,確保影片中可讀
- 簡短字句 — 最多兩行,每個字幕 6-8 個單詞
- 時間緩衝 — 在音訊開始前 2-3 影格啟動字幕
- 進場動畫 — 使用淡入/滑入效果,退出時不帶動畫
- 粗體字重 — 700+ 字重以確保影片可讀性
來源
- remotion-subtitles: github.com/ahgsql/remotion-subtitles
- Remotion Audiogram 範本: github.com/remotion-dev/template-audiogram