Skip to content

Instantly share code, notes, and snippets.

@pedramamini
Created August 20, 2025 17:21
Show Gist options
  • Save pedramamini/0f467306c924003268e140018b7406a4 to your computer and use it in GitHub Desktop.
Save pedramamini/0f467306c924003268e140018b7406a4 to your computer and use it in GitHub Desktop.
Pull a transcript from YouTube or fall back to local transcription.
#!/bin/bash
# YouTube Transcript Tool (ytt)
# Extracts transcripts from YouTube videos using built-in transcripts or local transcription
url="$1"
if [[ -z "$url" ]]; then
echo "Usage: ytt <youtube-url>"
echo "Extract transcript from YouTube video using built-in transcripts or whisperkit-cli fallback"
exit 1
fi
# echo "$url"
# Try using built-in transcript first
transcript_output=$(yt --transcript "$url" 2>&1)
if echo "$transcript_output" | grep -q "Transcript not available"; then
echo "[ytt] Transcript not available from YouTube. Falling back to whisperkit-cli transcription..." >&2
# Temporary directory
tmp_dir=$(mktemp -d)
video_file="$tmp_dir/video.mp4"
# Download video with yt-dlp
echo "[ytt] Downloading video..." >&2
if ! yt-dlp -f 'mp4' -o "$video_file" "$url" >/dev/null 2>/dev/null; then
echo "[ytt] Failed to download video." >&2
rm -rf "$tmp_dir"
exit 1
fi
# Extract audio from video using ffmpeg
echo "[ytt] Extracting audio..." >&2
audio_file="$tmp_dir/audio.wav"
if ! ffmpeg -i "$video_file" -vn -acodec pcm_s16le -ar 16000 -ac 1 "$audio_file" -y 2>/dev/null; then
echo "[ytt] Failed to extract audio from video." >&2
rm -rf "$tmp_dir"
exit 1
fi
# Transcribe with whisperkit-cli (optimized for Apple Silicon)
if command -v whisperkit-cli &> /dev/null; then
echo "[ytt] Transcribing with whisperkit-cli..." >&2
transcript_output=$(whisperkit-cli transcribe --audio-path "$audio_file" --model small --language en --task transcribe 2>/dev/null)
if [ -n "$transcript_output" ]; then
echo "$transcript_output"
else
echo "[ytt] Failed to generate transcript with whisperkit-cli."
fi
elif command -v whisper &> /dev/null; then
echo "[ytt] Falling back to whisper..." >&2
cd "$tmp_dir" && whisper "$audio_file" --language en --task transcribe --model small --output_dir . --output_format txt 2>/dev/null
transcript_txt="$tmp_dir/audio.txt"
if [ -f "$transcript_txt" ]; then
cat "$transcript_txt"
else
echo "[ytt] Failed to generate transcript with whisper."
fi
else
echo "[ytt] No transcription tool found (whisperkit-cli or whisper)."
rm -rf "$tmp_dir"
exit 1
fi
# Cleanup
rm -rf "$tmp_dir"
else
echo "$transcript_output"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment