#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'TXT'
Usage:
ytsubs FORMAT URL [LANG]
ytsubs --list URL
Examples:
ytsubs srt 'https://youtu.be/...'
ytsubs vtt 'https://youtu.be/...' 'en.*'
ytsubs --list 'https://youtu.be/...'
Formats: srt, vtt, ass, lrc
LANG defaults to en.*
TXT
}
[[ $# -gt 0 ]] || { usage; exit 2; }
if [[ $1 == --help || $1 == -h ]]; then
usage
exit 0
fi
if [[ $1 == --list || $1 == -l ]]; then
[[ $# -eq 2 ]] || { usage >&2; exit 2; }
exec yt-dlp --list-subs "$2"
fi
[[ $# -ge 2 && $# -le 3 ]] || { usage >&2; exit 2; }
format=${1,,}
url=$2
lang=${3:-en.*}
case $format in
srt|vtt|ass|lrc) ;;
*)
printf 'error: unsupported subtitle format: %s\n' "$format" >&2
usage >&2
exit 2
;;
esac
args=(
--skip-download
--write-subs
--write-auto-subs
--sub-langs "$lang"
--sub-format 'vtt/best'
-o '%(title)s [%(id)s].%(ext)s'
)
[[ $format == vtt ]] || args+=(--convert-subs "$format")
exec yt-dlp "${args[@]}" "$url"Install it:
install -Dm755 ytsubs ~/.local/bin/ytsubsUsage:
# Show manual and automatic subtitles
ytsubs --list 'https://youtu.be/MFR6SMV64h4'
# Download English subtitles as SRT
ytsubs srt 'https://youtu.be/MFR6SMV64h4'
# Download as VTT
ytsubs vtt 'https://youtu.be/MFR6SMV64h4'
# Select another language or language pattern
ytsubs srt 'https://youtu.be/MFR6SMV64h4' 'pl.*'
# Download every available language
ytsubs srt 'https://youtu.be/MFR6SMV64h4' 'all'It requests both manually uploaded subtitles and automatic captions, but downloads no video or audio.