Guest User

Video downloader using ffmpeg

a guest
Oct 7th, 2024
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.77 KB | Source Code | 0 0
  1. #!/usr/bin/zsh
  2.  
  3. # User will provide URL to download the video
  4.  
  5. setopt BASH_REMATCH
  6.  
  7. YOUTUBE_REGEX="https\:\/\/www\.youtube\.com\/watch\?v=[0-9A-Za-z_-]{11}"
  8. YTSHORT_REGEX="https\:\/\/youtu\.be/([0-9A-Za-z_-]{11})"
  9. TSHORTS_REGEX="https\:\/\/www\.youtube\.com\/shorts/([0-9A-Za-z_-]{11})"
  10. TWITTER_REGEX="https\:\/\/(www.)?twitter\.com"
  11. REDDIT_REGEX="https\:\/\/(old\.|m\.|www\.)?reddit.com"
  12.  
  13. YOUTUBE_MEDIA_LIST="you.out"
  14.  
  15. _URL=""
  16. _OUTPUT_FILE="%(title)s-%(uploader)s.mp4"
  17.  
  18. function print_help(){
  19.     usage="This program downloads YouTube video. You can select quality of your liking
  20.         -h --help           print this message
  21.         -u --url            YouTube url of the video
  22.         -o --output-file    the name of output file
  23.         "
  24.     echo $usage
  25. }
  26.  
  27. opts=$(getopt -o hu:o: --long help --long url: --long output-file: -- "$@")
  28.  
  29. if [ $? -ne 0 ]; then
  30.     echo "Incorrect options provided"
  31.     exit 1
  32. fi
  33.  
  34. eval set __ "$opts"
  35.  
  36. while true; do
  37.     case "$1" in
  38.         -h|--help)
  39.             print_help
  40.             exit 0
  41.             ;;
  42.         -u|--url)
  43.             shift
  44.             _URL="$1"
  45.             ;;
  46.         -o|--output-file)
  47.             shift;
  48.             _OUTPUT_FILE="$1"
  49.             ;;
  50.         --)
  51.             shift;
  52.             break
  53.             ;;
  54.     esac
  55.     shift
  56. done
  57.  
  58. if [[ $_URL =~ $TSHORTS_REGEX ]]; then
  59.     vid_code="${BASH_REMATCH[2]}"
  60.     _URL="https://www.youtube.com/watch?v=${vid_code}"
  61. elif [[ $_URL =~ $YTSHORT_REGEX ]]; then
  62.     vid_code="${BASH_REMATCH[2]}"
  63.     _URL="https://www.youtube.com/watch?v=${vid_code}"
  64. fi
  65.  
  66. yt-dlp  -F "$_URL">$YOUTUBE_MEDIA_LIST
  67.  
  68. more $YOUTUBE_MEDIA_LIST|awk '!/^(\[youtube\]|\[info\]|sb[0-9])/'
  69.  
  70. declare -A fmt_list
  71. more $YOUTUBE_MEDIA_LIST|awk '!/^(\[youtube\]|\[info\]|sb[0-9])/'| awk '/^([0-9]+|hls|dash|fall)/{print $1,$2}'|while read cd fmt;do fmt_list[$cd]=$fmt ; done
  72. keys=( ${(k)fmt_list} )
  73. c=""
  74. f=""
  75. FORMATS=""
  76. iput="no"
  77. while [ $iput = "no" ] ;
  78.  do
  79.     vared -p 'Select your video+audio (134+139) stream. Press ENTER for default values : ' -c FORMATS
  80.  
  81.     if [[ -z "$FORMATS" ]]; then
  82.         echo "Default values (134+139) selected"
  83.         FORMATS="134+139"
  84.     fi
  85.  
  86.     code_fmt=( ${(@s/+/)FORMATS} )
  87.     c=$code_fmt[1]
  88.     f=$code_fmt[2]
  89.  
  90.     nc=$keys[(Ie)$c]
  91.     nf=$keys[(Ie)$f]
  92.  
  93.     # echo "FORMATS : ${FORMATS}"
  94.     # echo "code_fmt: ${code_fmt}"
  95.     # echo "c = $c, nc = $nc"
  96.     # echo "f = $f, nf = $nf"
  97.    
  98.  
  99.     if [[ -n "$c" && -z "$f"  && $nc -gt 0 ]]; then
  100.         iput="yes"
  101.     elif [[ -n "$c" && -n "$f" && $nc -gt 0 && $nf -gt 0 ]]; then
  102.         iput="yes"
  103.     else
  104.         echo "ERROR: Please provide correct codes"
  105.     fi
  106.  
  107. done
  108.  
  109. echo "FORMATS : $FORMATS"
  110.  
  111.  
  112. #yt-dlp -f "${FORMATS}" --add-metadata --embed-thumbnail --recode-video mp4 --prefer-ffmpeg --audio-format aac "${_URL}" -o "${_OUTPUT_FILE}"
  113. yt-dlp -f "${FORMATS}" --add-metadata --embed-thumbnail --recode-video mp4 --prefer-ffmpeg --audio-format aac "${_URL}" -o "${_OUTPUT_FILE}"
  114. rm $YOUTUBE_MEDIA_LIST
Advertisement
Add Comment
Please, Sign In to add comment