Communitygithub.com

YouTube 逐字稿下載器

當使用者提供 YouTube 網址或要求下載/取得 YouTube 逐字稿時,協助下載 YouTube 影片的逐字稿(字幕/隱藏式字幕)。

YouTube 逐字稿下載器 是什麼?

此技能專為 AI 代理設計,用於從 YouTube 影片下載逐字稿(字幕和隱藏式字幕)。它採用階層式策略:首先嘗試取得人工字幕(品質最佳),若無則改用自動產生字幕,最後再以 Whisper 語音轉錄作為備案。技能會自動檢查並安裝所需的 yt-dlp 工具,列出可用的字幕與語言,並下載為 VTT 格式。此外,它會將 VTT 檔案轉換為純文字,並移除 YouTube 自動產生的 VTT 檔案中常見的重複行,確保輸出的文字流暢且保有原始語序。整個過程注重使用者體驗,會在執行耗時或大量下載前(如使用 Whisper 轉錄時)取得使用者確認。此技能支援 macOS、Linux 以及任何可透過 pip 安裝的系統。

相容平台✓Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/IGTA-Tech/claude-skills-research/tree/HEAD/tapestry-skills-for-claude-code/youtube-transcript

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

YouTube 逐字稿下載器 是做什麼的?

此技能使用 yt-dlp 協助從 YouTube 影片下載逐字稿(字幕/隱藏式字幕)。

使用時機

當使用者有以下情況時,請啟用此技能:

  • 提供 YouTube 網址並要求取得逐字稿
  • 要求「從 YouTube 下載逐字稿」
  • 想要「取得隱藏式字幕」或「取得字幕」
  • 要求「轉錄 YouTube 影片」
  • 需要 YouTube 影片的文字內容

運作方式

優先順序:

  1. 檢查是否已安裝 yt-dlp - 若未安裝則進行安裝
  2. 列出可用的字幕 - 查看實際有哪些字幕可用
  3. 先嘗試人工字幕(--write-sub)- 品質最高
  4. 備援使用自動產生字幕(--write-auto-sub)- 通常都有
  5. 最後手段:Whisper 語音轉錄 - 若無任何字幕(需使用者確認)
  6. 確認下載完成 並告知使用者檔案儲存位置
  7. 選擇性地清理 VTT 格式,若使用者需要純文字

安裝檢查

重要:請務必先檢查是否已安裝 yt-dlp:

which yt-dlp || command -v yt-dlp

若未安裝

根據系統自動嘗試安裝:

macOS(Homebrew):

brew install yt-dlp

Linux(apt/Debian/Ubuntu):

sudo apt update && sudo apt install -y yt-dlp

替代方案(pip - 適用所有系統):

pip3 install yt-dlp
# 或
python3 -m pip install yt-dlp

若安裝失敗:告知使用者需手動安裝 yt-dlp,並提供來自 https://github.com/yt-dlp/yt-dlp#installation 的安裝說明

檢查可用字幕

在嘗試下載之前,請務必先執行此步驟:

yt-dlp --list-subs "YOUTUBE_URL"

這會顯示有哪些類型的字幕可用,且不需下載任何內容。請查看:

  • 人工字幕(品質較佳)
  • 自動產生字幕(通常都有)
  • 可用的語言

下載策略

選項 1:人工字幕(首選)

優先嘗試此項 - 品質最高,由人類建立:

yt-dlp --write-sub --skip-download --output "OUTPUT_NAME" "YOUTUBE_URL"

選項 2:自動產生字幕(備援)

若無人工字幕可用:

yt-dlp --write-auto-sub --skip-download --output "OUTPUT_NAME" "YOUTUBE_URL"

兩個指令都會建立 .vtt 檔案(WebVTT 字幕格式)。

選項 3:Whisper 語音轉錄(最後手段)

僅在人工字幕與自動產生字幕均不可用時才使用此選項。

步驟 1:顯示檔案大小並請求確認

# 取得音訊檔案大小估算
yt-dlp --print "%(filesize,filesize_approx)s" -f "bestaudio" "YOUTUBE_URL"

# 或取得影片長度以進行估算
yt-dlp --print "%(duration)s %(title)s" "YOUTUBE_URL"

重要:向使用者顯示檔案大小並詢問:「此影片沒有可用的字幕。我可以下載音訊(約 X MB)並使用 Whisper 進行轉錄。您要繼續嗎?」

在繼續之前,請等待使用者確認。

步驟 2:檢查 Whisper 是否已安裝

command -v whisper

若未安裝,詢問使用者:「Whisper 尚未安裝。是否安裝?需執行 pip install openai-whisper(模型約需 1-3GB 空間)。這是一次性安裝。」

在安裝之前,請等待使用者確認。

若獲得同意則進行安裝:

pip3 install openai-whisper

步驟 3:僅下載音訊

yt-dlp -x --audio-format mp3 --output "audio_%(id)s.%(ext)s" "YOUTUBE_URL"

步驟 4:使用 Whisper 進行轉錄

# 自動偵測語言(建議)
whisper audio_VIDEO_ID.mp3 --model base --output_format vtt

# 或指定語言(若已知)
whisper audio_VIDEO_ID.mp3 --model base --language en --output_format vtt

模型選項(目前請使用 base):

  • tiny - 最快,精確度最低(約 1GB)
  • base - 平衡良好(約 1GB)← 使用此選項
  • small - 較佳精確度(約 2GB)
  • medium - 非常好(約 5GB)
  • large - 最佳精確度(約 10GB)

步驟 5:清理

轉錄完成後,詢問使用者:「轉錄完成!是否要刪除音訊檔案以節省空間?」

若是:

rm audio_VIDEO_ID.mp3

取得影片資訊

擷取影片標題(用於檔案名稱)

yt-dlp --print "%(title)s" "YOUTUBE_URL"

使用此資訊根據影片標題建立有意義的檔案名稱。為確保檔案系統相容性,請清理標題:

  • 將 / 取代為 -
  • 取代可能造成問題的特殊字元
  • 考慮使用清理後的版本:$(yt-dlp --print "%(title)s" "URL" | tr '/' '-' | tr ':' '-')

後續處理

轉換為純文字(建議)

YouTube 自動產生的 VTT 檔案包含重複行,因為字幕會以重疊的時間戳記逐步顯示。在轉換為純文字時,請務必在保留原始發言順序的同時移除重複內容。

python3 -c "
import sys, re
seen = set()
with open('transcript.en.vtt', 'r') as f:
    for line in f:
        line = line.strip()
        if line and not line.startswith('WEBVTT') and not line.startswith('Kind:') and not line.startswith('Language:') and '-->' not in line:
            clean = re.sub('<[^>]*>', '', line)
            clean = clean.replace('&amp;', '&').replace('&gt;', '>').replace('&lt;', '<')
            if clean and clean not in seen:
                print(clean)
                seen.add(clean)
" > transcript.txt

使用影片標題進行完整後續處理

# 取得影片標題
VIDEO_TITLE=$(yt-dlp --print "%(title)s" "YOUTUBE_URL" | tr '/' '_' | tr ':' '-' | tr '?' '' | tr '"' '')

# 尋找 VTT 檔案
VTT_FILE=$(ls *.vtt | head -n 1)

# 轉換並移除重複
python3 -c "
import sys, re
seen = set()
with open('$VTT_FILE', 'r') as f:
    for line in f:
        line = line.strip()
        if line and not line.startswith('WEBVTT') and not line.startswith('Kind:') and not line.startswith('Language:') and '-->' not in line:
            clean = re.sub('<[^>]*>', '', line)
            clean = clean.replace('&amp;', '&').replace('&gt;', '>').replace('&lt;', '<')
            if clean and clean not in seen:
                print(clean)
                seen.add(clean)
" > "${VIDEO_TITLE}.txt"

echo "✓ 已儲存至:${VIDEO_TITLE}.txt"

# 清理 VTT 檔案
rm "$VTT_FILE"
echo "✓ 已清理暫存的 VTT 檔案"

輸出格式

  • VTT 格式(.vtt):包含時間戳記與格式,適合用於影片播放器
  • 純文字(.txt):僅有文字內容,適合閱讀或分析

提示

  • 檔案名稱將為 {output_name}.{language_code}.vtt(例如 transcript.en.vtt)
  • 大多數 YouTube 影片都有自動產生的英文字幕
  • 部分影片可能有多種語言選項
  • 若無自動字幕,可嘗試使用 --write-sub 來取得人工字幕

完整工作流程範例

VIDEO_URL="https://www.youtube.com/watch?v=dQw4w9WgXcQ"

# 取得影片標題作為檔案名稱
VIDEO_TITLE=$(yt-dlp --print "%(title)s" "$VIDEO_URL" | tr '/' '_' | tr ':' '-' | tr '?' '' | tr '"' '')
OUTPUT_NAME="transcript_temp"

# ============================================
# 步驟 1:檢查是否已安裝 yt-dlp
# ============================================
if ! command -v yt-dlp &> /dev/null; then
    echo "找不到 yt-dlp,正在嘗試安裝..."
    if command -v brew &> /dev/null; then
        brew install yt-dlp
    elif command -v apt &> /dev/null; then
        sudo apt update && sudo apt install -y yt-dlp
    else
        pip3 install yt-dlp
    fi
fi

# ============================================
# 步驟 2:列出可用的字幕
# ============================================
echo "正在檢查可用的字幕..."
yt-dlp --list-subs "$VIDEO_URL"

# ============================================
# 步驟 3:先嘗試人工字幕
# ============================================
echo "正在嘗試下載人工字幕..."
if yt-dlp --write-sub --skip-download --output "$OUTPUT_NAME" "$VIDEO_URL" 2>/dev/null; then
    echo "✓ 人工字幕下載成功!"
    ls -lh ${OUTPUT_NAME}.*
else
    # ============================================
    # 步驟 4:備援使用自動產生字幕
    # ============================================
    echo "無人工字幕可用。正在嘗試自動產生字幕..."
    if yt-dlp --write-auto-sub --skip-download --output "$OUTPUT_NAME" "$VIDEO_URL" 2>/dev/null; then
        echo "✓ 自動產生字幕下載成功!"
        ls -lh ${OUTPUT_NAME}.*
    else
        # ============================================
        # 步驟 5:最後手段 - Whisper 語音轉錄
        # ============================================
        echo "⚠ 此影片無可用字幕。"

        # 取得檔案大小
        FILE_SIZE=$(yt-dlp --print "%(filesize_approx)s" -f "bestaudio" "$VIDEO_URL")
        DURATION=$(yt-dlp --print "%(duration)s" "$VIDEO_URL")
        TITLE=$(yt-dlp --print "%(title)s" "$VIDEO_URL")

        echo "影片:$TITLE"
        echo "長度:$((DURATION / 60)) 分鐘"
        echo "音訊大小:~$((FILE_SIZE / 1024 / 1024)) MB"
        echo ""
        echo "是否要下載音訊並使用 Whisper 進行轉錄?(y/n)"
        read -r RESPONSE

        if [[ "$RESPONSE" =~ ^[Yy]$ ]]; then
            # 檢查 Whisper
            if ! command -v whisper &> /dev/null; then
                echo "Whisper 尚未安裝。立即安裝?(約需 1-3GB 空間)(y/n)"
                read -r INSTALL_RESPONSE
                if [[ "$INSTALL_RESPONSE" =~ ^[Yy]$ ]]; then
                    pip3 install openai-whisper
                else
                    echo "沒有 Whisper 無法繼續。結束。"
                    exit 1
                fi
            fi

            # 下載音訊
            echo "正在下載音訊..."
            yt-dlp -x --audio-format mp3 --output "audio_%(id)s.%(ext)s" "$VIDEO_URL"

            # 取得實際的音訊檔案名稱
            AUDIO_FILE=$(ls audio_*.mp3 | head -n 1)

            # 轉錄
            echo "正在使用 Whisper 進行轉錄(可能需要幾分鐘)..."
            whisper "$AUDIO_FILE" --model base --output_format vtt

            # 清理
            echo "轉錄完成!是否刪除音訊檔案?(y/n)"
            read -r CLEANUP_RESPONSE
            if [[ "$CLEANUP_RESPONSE" =~ ^[Yy]$ ]]; then
                rm "$AUDIO_FILE"
                echo "音訊檔案已刪除。"
            fi

            ls -lh *.vtt
        else
            echo "轉錄已取消。"
            exit 0
        fi
    fi
fi

# ============================================
# 步驟 6:轉換為可讀取的純文字並移除重複內容
# ============================================
VTT_FILE=$(ls ${OUTPUT_NAME}*.vtt 2>/dev/null || ls *.vtt | head -n 1)
if [ -f "$VTT_FILE" ]; then
    echo "正在轉換為可讀格式並移除重複內容..."
    python3 -c "
import sys, re
seen = set()
with open('$VTT_FILE', 'r') as f:
    for line in f:
        line = line.strip()
        if line and not line.startswith('WEBVTT') and not line.startswith('Kind:') and not line.startswith('Language:') and '-->' not in line:
            clean = re.sub('<[^>]*>', '', line)
            clean = clean.replace('&amp;', '&').replace('&gt;', '>').replace('&lt;', '<')
            if clean and clean not in seen:
                print(clean)
                seen.add(clean)
" > "${VIDEO_TITLE}.txt"
    echo "✓ 已儲存至:${VIDEO_TITLE}.txt"

    # 清理暫存的 VTT 檔案
    rm "$VTT_FILE"
    echo "✓ 已清理暫存的 VTT 檔案"
else
    echo "⚠ 找不到 VTT 檔案可轉換"
fi

echo "✓ 完成!"

注意:此完整工作流程涵蓋所有情況,並在每個決策點提供適當的錯誤檢查與使用者提示。

錯誤處理

常見問題與解決方案:

1. yt-dlp 未安裝

  • 根據系統(Homebrew/apt/pip)自動嘗試安裝
  • 若安裝失敗,提供手動安裝連結
  • 在繼續之前確認安裝成功

2. 無可用字幕

  • 先列出可用的字幕以進行確認
  • 嘗試 --write-sub 和 --write-auto-sub 兩種方式
  • 若兩者皆失敗,提供 Whisper 轉錄選項
  • 在下載音訊前顯示檔案大小並請求使用者確認

3. 無效或私人影片

  • 檢查 URL 格式是否正確:https://www.youtube.com/watch?v=VIDEO_ID
  • 部分影片可能設為私人、有年齡限制或因地區限制無法觀看
  • 告知使用者 yt-dlp 回報的具體錯誤

4. Whisper 安裝失敗

  • 可能需要系統相依套件(ffmpeg、rust)
  • 提供備援方案:「請手動安裝:pip3 install openai-whisper」
  • 檢查可用的磁碟空間(模型依大小約需 1-10GB)

5. 下載中斷或失敗

  • 檢查網路連線
  • 確認有足夠的磁碟空間
  • 若遇到 SSL 問題,可嘗試加上 --no-check-certificate 參數重新下載

6. 多種字幕語言

  • 預設情況下,yt-dlp 會下載所有可用的語言
  • 可使用 --sub-langs en 指定僅下載英文
  • 先使用 --list-subs 列出可用的語言

最佳實務:

  • ✅ 在下載前務必檢查哪些內容可用(--list-subs)
  • ✅ 在進入下一步之前確認每一步是否成功
  • ✅ 在進行大量下載(音訊檔案、Whisper 模型)前詢問使用者
  • ✅ 處理完成後清理暫存檔案
  • ✅ 在每個階段提供清晰的回饋,告知使用者正在進行的操作
  • ✅ 優雅地處理錯誤,並提供有用的訊息

Individual skills in this repo

This repo contains 7 individual skills — each has its own dedicated page.

IGTA-Tech/claude-skills-research

Use when localizing apps, using String Catalogs, generating type-safe symbols (Xcode 26+), handling plurals, RTL layouts, locale-aware formatting, or migrating from .strings files - comprehensive i18n patterns for Xcode 15-26

IGTA-Tech/claude-skills-research

Use when implementing SwiftUI animations, understanding VectorArithmetic, using @Animatable macro, choosing between spring and timing curve animations, or debugging animation behavior - comprehensive animation reference from iOS 13 through iOS 26

IGTA-Tech/claude-skills-research

Use when CAAnimation completion handler doesn't fire, spring physics look wrong on device, animation duration mismatches actual time, gesture + animation interaction causes jank, or timing differs between simulator and real hardware - systematic CAAnimation diagnosis with CATransaction patterns, frame rate awareness, and device-specific behavior

IGTA-Tech/claude-skills-research

This skill should be used when comparing two videos to analyze compression results or quality differences. Generates interactive HTML reports with quality metrics (PSNR, SSIM) and frame-by-frame visual comparisons. Triggers when users mention "compare videos", "video quality", "compression analysis", "before/after compression", or request quality assessment of compressed videos.

IGTA-Tech/claude-skills-research

Download YouTube videos and HLS streams (m3u8) from platforms like Mux, Vimeo, etc. using yt-dlp and ffmpeg. Use this skill when users request downloading videos, extracting audio, handling protected streams with authentication headers, or troubleshooting download issues like nsig extraction failures, 403 errors, or cookie extraction problems.

IGTA-Tech/claude-skills-research

Write high-converting landing page copy using proven frameworks like PAS (Problem-Agitate-Solution), AIDA, and StoryBrand. Creates headlines, value propositions, CTAs, and full page sections optimized for conversion. Use when users need landing page copy, sales page content, or marketing website text.

IGTA-Tech/claude-skills-research

Downloads videos from YouTube and other platforms for offline viewing, editing, or archival. Handles various formats and quality options.

相關技能