import os
import subprocess
import whisper
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
from yt_dlp import YoutubeDL

Configuration

CHANNEL_URL = "https://www.youtube.com/c/YOUR_CHANNEL_NAME"
OUTPUT_DIR = "transcripts"
USE_WHISPER_IF_NO_CAPTION = True
WHISPER_MODEL = "base" # Options: tiny, base, small, medium, large

Make output directory

os.makedirs(OUTPUT_DIR, exist_ok=True)

Step 1: Get video IDs from channel

print("Fetching video list...")
ydl_opts = {
'quiet': True,
'extract_flat': True,
'dump_single_json': True,
}
with YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(CHANNEL_URL, download=False)
video_entries = result.get('entries', [])
video_ids = [entry['id'] for entry in video_entries]

print(f"Found {len(video_ids)} videos.")

Step 2: Load Whisper model

if USE_WHISPER_IF_NO_CAPTION:
model = whisper.load_model(WHISPER_MODEL)

Step 3: Process each video

for idx, video_id in enumerate(video_ids, 1):
video_url = f"https://www.youtube.com/watch?v={video_id}"
transcript_path = os.path.join(OUTPUT_DIR, f"{video_id}.txt")

if os.path.exists(transcript_path):
    print(f"[{idx}/{len(video_ids)}] Skipping {video_id}, already done.")
    continue

try:
    print(f"[{idx}/{len(video_ids)}] Trying YouTube transcript for {video_id}...")
    transcript = YouTubeTranscriptApi.get_transcript(video_id)
    text = "\n".join([entry['text'] for entry in transcript])
    with open(transcript_path, "w", encoding="utf-8") as f:
        f.write(text)
    print("✓ Captions found.")
except (TranscriptsDisabled, NoTranscriptFound):
    if USE_WHISPER_IF_NO_CAPTION:
        print("✗ No captions. Downloading audio for Whisper...")
        audio_filename = f"{video_id}.mp3"
        subprocess.run([
            "yt-dlp", "-x", "--audio-format", "mp3", "-o", audio_filename, video_url
        ], check=True)
        result = model.transcribe(audio_filename)
        with open(transcript_path, "w", encoding="utf-8") as f:
            f.write(result["text"])
        os.remove(audio_filename)
        print("✓ Transcribed with Whisper.")
    else:
        print("✗ No captions and Whisper disabled.")
Edit

Pub: 31 May 2025 07:34 UTC

Views: 12