Skip to content

Instantly share code, notes, and snippets.

@kljensen
Created November 5, 2024 15:12
Show Gist options
  • Save kljensen/92503eef398ba5651728518ec52bbe77 to your computer and use it in GitHub Desktop.
Save kljensen/92503eef398ba5651728518ec52bbe77 to your computer and use it in GitHub Desktop.
download-youtube-transcript
#!/bin/sh
# We expect a single parameter, the URL of the YouTube video
if [ $# -ne 1 ]; then
echo "Usage: $0 <YouTube URL>"
exit 1
fi
# Get a temp file name
tempfile=$(mktemp)
LANG="en"
OUTPUT_FILE="$tempfile.$LANG.vtt"
# Remove the temp file on exit
trap "rm -f $OUTPUT_FILE" EXIT
youtube-dl -o "$tempfile" --write-sub --sub-lang $LANG --skip-download "$1" 2>/dev/null >/dev/null
# Extract the transcript from the subtitle file.
# We remove lines with "-->" and the first 5 lines.
TEXT=$(
cat "$OUTPUT_FILE" | \
tail -n +5 | \
grep -v -F ' --> ' | \
grep -v '^$'
)
printf "$TEXT\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment