Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Created August 8, 2025 10:02
Show Gist options
  • Save alfonsodev/8a5fd7bb4d6f95fcb078802ca39b3192 to your computer and use it in GitHub Desktop.
Save alfonsodev/8a5fd7bb4d6f95fcb078802ca39b3192 to your computer and use it in GitHub Desktop.
cli script to sumarize a yt video
#!/usr/bin/env bash
set -euo pipefail
# summarize-youtube.sh
# Usage: ./summarize-youtube.sh "https://www.youtube.com/watch?v=A9BiNPf34Z4" [lang]
# Requires: yt-dlp, jq, llm
# Optional: set COOKIES_FROM_BROWSER=chrome (or your browser) if the video needs auth/age check.
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <youtube-url> [lang]" >&2
exit 1
fi
for cmd in yt-dlp jq llm; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Missing dependency: $cmd" >&2
exit 1
fi
done
url="$1"
lang="${2:-en}"
# Optional cookies-from-browser support
extra_args=()
if [[ -n "${COOKIES_FROM_BROWSER:-}" ]]; then
extra_args+=(--cookies-from-browser "$COOKIES_FROM_BROWSER")
fi
# Get video ID
id="$(yt-dlp --print id --skip-download "${extra_args[@]}" "$url")"
# Fetch captions (prefer human, fall back to auto), JSON3 format
yt-dlp -q --no-warnings --skip-download \
--write-subs --write-auto-subs \
--sub-langs "${lang}.*" \
--sub-format json3 \
-o '%(id)s' \
"${extra_args[@]}" \
"$url" >/dev/null 2>&1 || true
# Find the best matching captions file
capfile=""
if compgen -G "$id.${lang}"'*.json3' > /dev/null; then
capfile="$(ls -1 "$id.${lang}"*.json3 | head -n1)"
elif compgen -G "$id."*'.json3' > /dev/null; then
capfile="$(ls -1 "$id."*.json3 | head -n1)"
fi
if [[ -z "$capfile" ]]; then
echo "No captions found (language: $lang). Try a different lang code or check --list-subs." >&2
exit 2
fi
# Convert JSON3 captions -> plain text
out="/tmp/$id.txt"
jq -r '.events[]? | (.segs // []) | map(.utf8) | join("")' "$capfile" > "$out"
# Prompt + subtitles -> llm
{
echo "<prompt>sumarize this video subtitle file and extract the gist of advice and learnings </prompt>"
echo "<subtitles>"
cat "$out"
echo "</subtitles>"
} | llm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment