Created
February 28, 2026 19:56
-
-
Save Kishibe3/0e87f02248df43ab06a14485a1a43e5c to your computer and use it in GitHub Desktop.
get Youtube subtitles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #sudo apt update | |
| #sudo apt install ffmpeg | |
| #pip install -U openai-whisper yt-dlp | |
| #sudo chmod +x yt-sbt.sh | |
| if [ $# -eq 0 ]; then | |
| echo "請提供 YouTube 影片 ID 作為參數。" | |
| exit 1 | |
| fi | |
| video_id=$1 | |
| url="https://www.youtube.com/watch?v=$video_id" | |
| output_file="${video_id}.txt" | |
| langs=("en" "zh-TW" "zh") | |
| subs_found=false | |
| subs_file="/tmp/subs_$(date +%s%N).vtt" | |
| audio_file="/tmp/audio_$(date +%s%N).mp3" | |
| downloaded_subs_file="/tmp/downloaded_subs_$(date +%s%N).vtt" | |
| for lang in "${langs[@]}"; do | |
| echo "正在嘗試下載 $lang 字幕..." | |
| if yt-dlp --write-subs --skip-download --sub-lang "$lang" --output "$subs_file" "$url"; then | |
| if [ -f "${subs_file}.${lang}.vtt" ]; then | |
| mv "${subs_file}.${lang}.vtt" "$downloaded_subs_file" | |
| subs_found=true | |
| break | |
| fi | |
| fi | |
| done | |
| if [ "$subs_found" = true ]; then | |
| echo "找到字幕,正在提取字幕文字..." | |
| sed '/^[0-9]/d' "$downloaded_subs_file" | sed '/^$/d' > "$output_file" | |
| echo "字幕已保存到 $output_file" | |
| rm "$downloaded_subs_file" | |
| else | |
| echo "找不到字幕,正在下載音檔..." | |
| yt-dlp -f bestaudio --extract-audio --audio-format mp3 --output "$audio_file" "$url" | |
| echo "音檔下載完成,正在使用 Whisper 進行轉錄..." | |
| whisper --model small --output_dir /tmp --output_format txt "$audio_file" | |
| # 獲取轉錄結果的檔案路徑 | |
| transcription_file="${audio_file%.*}.txt" | |
| if [ -f "$transcription_file" ]; then | |
| mv "$transcription_file" "$output_file" | |
| echo "轉錄已保存到 $output_file" | |
| else | |
| # 如果找不到轉錄結果檔案,顯示錯誤訊息並退出腳本 | |
| echo "無法找到轉錄檔案。" | |
| exit 1 | |
| fi | |
| rm "$audio_file" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment