yt-dl.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # look into bracketed-paste-magic and url-quote-magic zsh features
  4. output="$(date +%F)_%(upload_date)s_%(title)s_%(resolution)s.%(ext)s"
  5. DIR="${HOME}/Videos/yt-dlp"
  6. flags=(-ciw --add-metadata --embed-subs --sub-langs all --embed-thumbnail --sponsorblock-remove all)
  7. display_help(){
  8. echo "Usage: ${0##*/} [flags] [url/s]"
  9. echo "-q, --Specify video quality to download, 144|240|360|480|..."
  10. echo "-a, --Download audio ONLY (default best quality possible)"
  11. echo "-c, --Adds channel name to filenames"
  12. echo "-r, --Regex pattern to search in channel"
  13. echo "-R, --Regex pattern to search in channel (case sensitive)"
  14. echo "-d, --Downloads to custom specified directory, def: ${DIR}"
  15. echo "-h, --Show this help message"
  16. echo "Examples:"
  17. echo "${0##*/} -q [144,240,360,720,...] youtube.com/..."
  18. echo "${0##*/} -a [best,worst] youtube.com/..."
  19. echo "${0##*/} -r 'Linux Crash Course' youtube.com/channel..."
  20. exit 2
  21. }
  22. setup_output_for(){
  23. vid_link="$1"
  24. prefix="all/"
  25. if grep -E --color 'playlist' <<< "$vid_link"; then
  26. prefix="%(playlist_title)s/%(playlist_index)s_"
  27. fi
  28. output="${DIR}/%(channel)s/${prefix}${output}"
  29. }
  30. set_cookies(){
  31. cookies="${DOTFILES}/.misc/yt-cookies.txt"
  32. if [[ -f $cookies ]]; then
  33. flags+=(--cookies "$cookies")
  34. else
  35. echo "No cookie file at ${cookies}!"
  36. fi
  37. }
  38. download(){
  39. clear_output="$output"
  40. for vid_link in "$@"; do
  41. set_cookies
  42. setup_output_for "$vid_link"
  43. yt-dlp "${flags[@]}" -o "$output" -- "$vid_link"
  44. output="$clear_output"
  45. done
  46. }
  47. rerun_if_not_downloaded(){
  48. if [[ -n $(find "$DIR" -type f -mtime -1 -iname "*.part") ]]; then
  49. "$0" "$@"
  50. fi
  51. }
  52. while getopts 'q:a:r:R:d:h' opt; do
  53. case $opt in
  54. q) flags+=(-f "bestvideo[height<=${OPTARG:-480}]+bestaudio/best[height<=${OPTARG:-480}]") ;;
  55. a) flags+=(-x -f "${OPTARG:-best}audio/${OPTARG:-best}") ;;
  56. r) flags+=(--match-title="(?i)${OPTARG}") ;;
  57. R) flags+=(--match-title="(?i)${OPTARG}") ;;
  58. d) DIR="$OPTARG" ;;
  59. h|\?|:|*) display_help ;;
  60. esac
  61. done
  62. all_params="$@"
  63. shift $((OPTIND-1))
  64. links="$@"
  65. [[ $# -lt 1 ]] && display_help
  66. download "$links"
  67. rerun_if_not_downloaded "$all_params"
  68. # >>93364968
  69. # I don't care. If you wanted to do that with -S all you'd need is -S acodec:opus,br, since it should always have the highest bitrate for video. But normal people care about what actually looks the best.
  70. # >>93364942
  71. # This is about ripping the enhanced bitrate stream which is only available on the VP9 codec, just stfu retard.
  72. # >>93364898
  73. # That's because av1 streams consistently have higher vmaf scores than the vp9 premium bitrate streams.
  74. # >>93364858
  75. # The last time you posted the command you selected av1 in the sorting formats flags like a retard so stfu.
  76. # >>93364629
  77. # Like I said, it's gimped. It requires you to manually select the ID. I've posted the correct command multiple times but you keep calling it bloated despite being fewer total characters.
  78. # >>93364598
  79. # Why is it gimped? point one thing wrong with the command, go ahead.
  80. # >>93364439
  81. # Stop posting this godawful gimped manual command and use the new -S flag like a normal person.
  82. # Command for ripping YT Premium Enhanced Bitrate videos:
  83. # First, check if the video you want has enhanced bitrate available, the format ID you want to see is the "616":
  84. # yt-dlp --list-formats --extractor-args "youtube:player_client=default,ios" yt-link
  85. # Now to rip the video with OPUS audio:
  86. # yt-dlp -f 616+251 --extractor-args "youtube:player_client=default,ios" yt-link