ElevenLabs 配音生成
使用 ElevenLabs API 為 Remotion 影片生成專業 AI 配音。
先決條件
- 在
.env.local中設定ELEVEN_API_KEY
快速開始
# 從文字生成配音
node ~/.agents/skills/elevenlabs-remotion/generate.js --text "您的文字內容" --output public/audio/voiceover.mp3
# 使用旁白風格生成(更自然)
node ~/.agents/skills/elevenlabs-remotion/generate.js --text "您的文字" --character narrator --output voiceover.mp3
# 使用請求拼接生成多個場景
node ~/.agents/skills/elevenlabs-remotion/generate.js --scenes remotion/scenes.json --output-dir public/audio/project/
# 重新生成單一場景
node ~/.agents/skills/elevenlabs-remotion/generate.js --scenes scenes.json --scene scene2 --new-text "更新後的文字"
# 列出可用語音和角色預設
node ~/.agents/skills/elevenlabs-remotion/generate.js --list-voices
node ~/.agents/skills/elevenlabs-remotion/generate.js --list-characters
角色預設
使用角色預設可以獲得更自然的配音效果,而非直接照本宣科地朗讀螢幕文字:
| 角色 | 描述 | 最佳用途 |
|---|---|---|
literal | 完全照文字朗讀 | 螢幕文字、引言 |
narrator | 專業說書人,流暢迷人 | 解說、紀錄片 |
salesperson | 熱情、有說服力、充滿活力 | 行銷、廣告 |
expert | 權威、自信、知識淵博 | 法律內容、教學 |
conversational | 隨意、友好、自然 | 社交媒體、休閒內容 |
dramatic | 強烈、情感豐富、衝擊力強 | 鉤子、問題陳述 |
calm | 安撫、堅定、溫和 | 建立信任、結論 |
# 全域使用旁白風格
node ~/.agents/skills/elevenlabs-remotion/generate.js --scenes scenes.json --character narrator --output-dir public/audio/
# 或在 scenes.json 中按場景設定
{
"scenes": [
{ "id": "scene1", "text": "問題陳述", "character": "dramatic" },
{ "id": "scene2", "text": "解決方案", "character": "calm" }
]
}
基於場景的生成與請求拼接
使用 ElevenLabs 的請求拼接功能,生成具有一致語調的多個場景:
scenes.json 格式
{
"name": "product-demo",
"voice": "George",
"character": "narrator",
"scenes": [
{
"id": "scene1",
"text": "通用的文字轉語音聽起來很機械。您的品牌值得更好的。",
"duration": 4.5,
"character": "dramatic"
},
{
"id": "scene2",
"text": "透過語音複製,您可以使用自己的聲音創建無限的內容。",
"duration": 5.5
},
{
"id": "scene3",
"text": "錄製一個短樣本。複製它。在幾分鐘內創建專業的配音。",
"duration": 6,
"delay": 0.3
}
]
}
生成所有場景
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/product-demo-scenes.json \
--output-dir public/audio/product-demo/
這將創建:
product-demo-scene1.mp3到sceneN.mp3product-demo-combined.mp3(所有場景拼接)product-demo-info.json(包含時長的元數據)
單一場景重新生成
如果某個場景開始過早、時間點不對,或需要不同的文字:
# 使用新文字重新生成 scene2
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/scenes.json \
--scene scene2 \
--new-text "更新後的場景2文字" \
--output-dir public/audio/project/
# 使用不同角色重新生成 scene3
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/scenes.json \
--scene scene3 \
--character salesperson \
--output-dir public/audio/project/
# 僅重新生成(相同文字、相同角色)
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/scenes.json \
--scene scene1 \
--output-dir public/audio/project/
# 將縮圖嵌入 MP4 影片
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--embed-thumbnail public/videos/my-video.mp4 \
--thumbnail public/videos/my-thumbnail.png \
--output public/videos/my-video-with-thumb.mp4
此工具會自動:
- 使用先前場景的請求拼接,以保持一致的語調
- 使用新的元數據更新 info.json 檔案
- 如果提供了
--new-text,則更新 scenes.json
縮圖嵌入
將縮圖圖片嵌入 MP4 影片,以便 Twitter、YouTube 和影片播放器等平台顯示您自訂的縮圖,而非影片的第一幀。
將縮圖嵌入影片
# 基本用法 - 輸出到 video-thumb.mp4
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--embed-thumbnail public/videos/promo.mp4 \
--thumbnail public/videos/thumbnail.png
# 自訂輸出路徑
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--embed-thumbnail public/videos/promo.mp4 \
--thumbnail public/videos/thumbnail.png \
--output public/videos/promo-final.mp4
與 Remotion 結合的工作流程
# 1. 渲染您的影片
npx remotion render MyVideo public/videos/my-video.mp4
# 2. 渲染您的縮圖(使用 Still composition)
npx remotion still MyVideoThumbnail public/videos/my-thumbnail.png
# 3. 嵌入縮圖
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--embed-thumbnail public/videos/my-video.mp4 \
--thumbnail public/videos/my-thumbnail.png \
--output public/videos/my-video-final.mp4
支援的格式
- 影片: MP4 (H.264/H.265)
- 縮圖: PNG, JPG, JPEG
此嵌入過程使用 ffmpeg 的 -disposition:v:1 attached_pic 標誌,將縮圖設定為附加圖片,大多數影片播放器和平台都能識別。
時間驗證
該技能在生成後會自動使用 ffprobe 驗證時間:
檢查項目
| 檢查項目 | 閾值 | 描述 |
|---|---|---|
| 時長不匹配 | >15% | 如果實際時長與預期時長差異過大,則發出警告 |
| 開頭靜音 | >200ms | 音頻開始過晚(配音延遲) |
| 結尾靜音 | >500ms | 結尾有不必要的靜音 |
| 語速 | 2-4.5 wps | 最佳約 3 字詞/秒 |
驗證現有音頻
# 驗證專案中的所有場景
node ~/.agents/skills/elevenlabs-remotion/generate.js --validate public/audio/product-demo/
輸出範例:
🔍 正在驗證 product-demo(6 個場景)
❌ scene1: 3.00秒(預期: 4.5秒)
❌ 音頻比預期短 1.50秒
👍 8 個字詞 @ 3.1 字詞/秒
⚠️ scene2: 6.35秒(預期: 5.5秒)
⚠️ 開頭靜音: 235ms(可能開始過晚)
🐢 10 個字詞 @ 1.8 字詞/秒
✅ scene4: 4.36秒(預期: 4秒)
👍 9 個字詞 @ 2.3 字詞/秒
📊 總時長: 30.80秒(預期: 30.00秒)
更新的 info.json
驗證後,info.json 會包含實際測量值:
{
"scenes": [
{
"id": "scene1",
"duration": 4.5,
"actualDuration": 3.0,
"leadingSilence": 0.05,
"wordsPerSecond": 3.1
}
]
}
在您的 Remotion 合成中使用 actualDuration 以實現精確同步。
選項
| 選項 | 描述 | 預設值 |
|---|---|---|
--text, -t | 要轉換為語音的文字 | 必要(或 --file/--scenes) |
--file, -f | 從檔案讀取文字 | - |
--output, -o | 輸出檔案路徑 | output.mp3 |
--output-dir | 場景的輸出目錄 | public/audio |
--voice, -v | 語音名稱或 ID | George |
--model, -m | 模型 ID | eleven_multilingual_v2 |
--character, -c | 角色預設 | literal |
--scenes | 包含場景的 JSON 檔案 | - |
--scene | 重新生成單一場景 ID | - |
--new-text | 場景重新生成的新文字 | - |
--validate | 驗證現有音頻目錄 | - |
--skip-validation | 跳過自動驗證 | false |
--embed-thumbnail | 要嵌入縮圖的影片檔案 | - |
--thumbnail | 縮圖圖片檔案(PNG/JPG) | - |
--stability | 語音穩定性(0-1) | 因角色而異 |
--similarity | 語音相似度(0-1) | 因角色而異 |
--style | 風格誇張程度(0-1) | 因角色而異 |
--no-combined | 跳過合併檔案 | false |
推薦語音
| 語音 | 風格 | 最佳用途 |
|---|---|---|
George | 溫暖、迷人的英式發音 | 旁白、解說 |
Antoni | 專業、溫暖 | 法律內容、教學 |
Arnold | 權威、低沉 | 企業、嚴肅話題 |
Josh | 友好、健談 | 行銷、休閒內容 |
與 Remotion 整合
生成場景配音後,在您的 composition 中使用它們:
import { Audio, Sequence, staticFile } from "remotion";
// 使用單個場景音頻檔案以實現精確同步
const SCENE_DURATIONS = {
scene1: 4.5, // 從 info.json 取得
scene2: 5.5,
scene3: 8.0,
};
export const VideoWithVoiceover: React.FC = () => {
const { fps } = useVideoConfig();
const scene1Frames = Math.round(SCENE_DURATIONS.scene1 * fps);
const scene2Frames = Math.round(SCENE_DURATIONS.scene2 * fps);
return (
<>
<Sequence from={0} durationInFrames={scene1Frames}>
<Audio src={staticFile("audio/project/project-scene1.mp3")} />
<Scene1Visual />
</Sequence>
<Sequence from={scene1Frames} durationInFrames={scene2Frames}>
<Audio src={staticFile("audio/project/project-scene2.mp3")} />
<Scene2Visual />
</Sequence>
</>
);
};
最佳效果提示
- 使用角色預設:不要直接照本宣科地朗讀螢幕文字,使用
narrator或expert以獲得自然流暢的效果 - 標點符號很重要:使用句號表示停頓,逗號表示短暫停頓
- 數字:為了自然發音,請拼寫出數字("五百" 而非 "500")
- 縮寫:寫出完整詞語("二十四小時" 而非 "24h")
- 逐場景處理:不同的場景可以使用不同的角色(戲劇性的開場,平靜的行動呼籲)
- 微調:使用
--scene重新生成個別場景,無需重做所有工作 - 請求拼接:保持所有場景的語音一致
工作流程範例
# 1. 使用您的劇本創建 scenes.json
# 2. 使用旁白風格生成所有場景
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/my-video-scenes.json \
--character narrator \
--output-dir public/audio/my-video/
# 3. 在 Remotion 中預覽,發現 scene2 開始過早
# 4. 使用更新的文字重新生成 scene2
node ~/.agents/skills/elevenlabs-remotion/generate.js \
--scenes remotion/my-video-scenes.json \
--scene scene2 \
--new-text "稍微長一點的文字以填滿視覺時間" \
--output-dir public/audio/my-video/
# 5. 使用 info.json 中的新時長更新影片 composition
# 6. 重複直到時間點完美