Skip to content

Instantly share code, notes, and snippets.

@codingEzio
Created December 24, 2022 03:16
Show Gist options
  • Select an option

  • Save codingEzio/9ab763d954d303973c5b935cc3f0a039 to your computer and use it in GitHub Desktop.

Select an option

Save codingEzio/9ab763d954d303973c5b935cc3f0a039 to your computer and use it in GitHub Desktop.
# E: excution
# P: process
# M: meta
# https://github.com/rothgar/mastering-zsh/blob/master/docs/helpers/aliases.md#global-aliases
alias -g YT_E_UA=''
alias -g YT_E_TR='--no-check-certificate --retries 2' # Exec (Re)Try
alias -g YT_E_NM='--restrict-filenames' # Exec Sanitize Name
alias -g YT_P_M_MP4='--merge-output-format mp4' # Process Merge
alias -g YT_P_M_MKV='--merge-output-format mkv' # Process Merge
alias -g YT_M_V='--verbose' # Meta Verbose
alias -g YT_M_EM='--add-metadata --embed-thumbnail' # Meta Embed
ytb() {
# e.g.
# $ ytb URL default best result fmt: mp4
# $ ytb URL format list all the formats
# $ ytb URL subtitle download subtitles only
# $ ytb URL bestwebm videos that have 4k vers result fmt: mp4
# $ ytb URL bestmp4 2k/4k aren't required result fmt: mp4
# $ ytb URL 360p 2k/4k aren't required result fmt: mp4
yt_link=$1
yt_arg=$2
# the native way to avoid download the same vids twice
# [file format]
# > youtube _G95vcrzAGk (platform VID_ID)
#
# [1st time: record -- the rest: yt-dl reads that, then skip dups]
# > youtube-dl OTHER_OPTIONS --download-archive _archive_urls.txt URL
# > youtube-dl OTHER_OPTIONS --download-archive _archive_urls.txt URL
#
# [thoughts]
# > Q: Is it ok to add this flag even if it's just a single video?
# > A: I think it's fine
# - it's just ONE file
# - if you're downloading new vids, it does HELP to avoid dups
yt="/usr/local/Cellar/youtube-dl/2021.12.17/bin/youtube-dl"
arg_listformat="--list-formats"
# only for displaying purposes
# bestmusic="'bestaudio --extract-audio --audio-format mp3 --audio-quality 0'"
# bestvid_webm="'bestvideo[ext=webm]+bestaudio[ext=webm]/bestvideo+bestaudio'"
# bestvid_mp4="'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio'"
if [ "${yt_arg}" = "subtitle" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --write-sub --skip-download \
--write-auto-sub \
--sub-format vtt --sub-lang en --embed-subs ${yt_link}
elif [ "${yt_arg}" = "subtitleall" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --write-sub --skip-download \
--all-subs --write-auto-sub \
--sub-format vtt --embed-subs ${yt_link}
elif [ "${yt_arg}" = "format" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} ${arg_listformat} ${yt_link}
# the fallback of these two are the same (no format being specified)
elif [ "${yt_arg}" = "bestwebm" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --format 'bestvideo[ext=webm]+bestaudio[ext=webm]/bestvideo+bestaudio' \
YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link} ||
${yt} YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link}
# the fallback of these two are the same (no format being specified)
elif [ "${yt_arg}" = "bestmp4" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --format 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' \
YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link} ||
${yt} YT_P_M_MP4 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
# 360p
elif [ "${yt_arg}" = "360p" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --format '134+140' \
YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link} ||
${yt} --format '134+140' \
YT_P_M_MP4 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
# 720p
elif [ "${yt_arg}" = "720p" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --format '136+251' \
YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link} ||
${yt} --format '136+251' \
YT_P_M_MP4 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
# 720p 399+251 (smallest vid, biggest audio)
elif [ "${yt_arg}" = "720p399251" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --format '399+251' \
YT_P_M_MP4 YT_M_V YT_E_NM YT_M_EM YT_E_TR ${yt_link} ||
${yt} --format '399+251' \
YT_P_M_MP4 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
elif [ "${yt_arg}" = "bestmusic" ]; then
echo "You're in the ${yt_arg} branch.\n"
${yt} --extract-audio --audio-format mp3 --audio-quality 0 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
else
echo "You're in the generic branch.\n"
${yt} YT_P_M_MP4 YT_E_NM YT_M_EM YT_E_TR ${yt_link}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment