Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/zsh
- # User will provide URL to download the video
- setopt BASH_REMATCH
- YOUTUBE_REGEX="https\:\/\/www\.youtube\.com\/watch\?v=[0-9A-Za-z_-]{11}"
- YTSHORT_REGEX="https\:\/\/youtu\.be/([0-9A-Za-z_-]{11})"
- TSHORTS_REGEX="https\:\/\/www\.youtube\.com\/shorts/([0-9A-Za-z_-]{11})"
- TWITTER_REGEX="https\:\/\/(www.)?twitter\.com"
- REDDIT_REGEX="https\:\/\/(old\.|m\.|www\.)?reddit.com"
- YOUTUBE_MEDIA_LIST="you.out"
- _URL=""
- _OUTPUT_FILE="%(title)s-%(uploader)s.mp4"
- function print_help(){
- usage="This program downloads YouTube video. You can select quality of your liking
- -h --help print this message
- -u --url YouTube url of the video
- -o --output-file the name of output file
- "
- echo $usage
- }
- opts=$(getopt -o hu:o: --long help --long url: --long output-file: -- "$@")
- if [ $? -ne 0 ]; then
- echo "Incorrect options provided"
- exit 1
- fi
- eval set __ "$opts"
- while true; do
- case "$1" in
- -h|--help)
- print_help
- exit 0
- ;;
- -u|--url)
- shift;
- _URL="$1"
- ;;
- -o|--output-file)
- shift;
- _OUTPUT_FILE="$1"
- ;;
- --)
- shift;
- break
- ;;
- esac
- shift
- done
- if [[ $_URL =~ $TSHORTS_REGEX ]]; then
- vid_code="${BASH_REMATCH[2]}"
- _URL="https://www.youtube.com/watch?v=${vid_code}"
- elif [[ $_URL =~ $YTSHORT_REGEX ]]; then
- vid_code="${BASH_REMATCH[2]}"
- _URL="https://www.youtube.com/watch?v=${vid_code}"
- fi
- yt-dlp -F "$_URL">$YOUTUBE_MEDIA_LIST
- more $YOUTUBE_MEDIA_LIST|awk '!/^(\[youtube\]|\[info\]|sb[0-9])/'
- declare -A fmt_list
- 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
- keys=( ${(k)fmt_list} )
- c=""
- f=""
- FORMATS=""
- iput="no"
- while [ $iput = "no" ] ;
- do
- vared -p 'Select your video+audio (134+139) stream. Press ENTER for default values : ' -c FORMATS
- if [[ -z "$FORMATS" ]]; then
- echo "Default values (134+139) selected"
- FORMATS="134+139"
- fi
- code_fmt=( ${(@s/+/)FORMATS} )
- c=$code_fmt[1]
- f=$code_fmt[2]
- nc=$keys[(Ie)$c]
- nf=$keys[(Ie)$f]
- # echo "FORMATS : ${FORMATS}"
- # echo "code_fmt: ${code_fmt}"
- # echo "c = $c, nc = $nc"
- # echo "f = $f, nf = $nf"
- if [[ -n "$c" && -z "$f" && $nc -gt 0 ]]; then
- iput="yes"
- elif [[ -n "$c" && -n "$f" && $nc -gt 0 && $nf -gt 0 ]]; then
- iput="yes"
- else
- echo "ERROR: Please provide correct codes"
- fi
- done
- echo "FORMATS : $FORMATS"
- #yt-dlp -f "${FORMATS}" --add-metadata --embed-thumbnail --recode-video mp4 --prefer-ffmpeg --audio-format aac "${_URL}" -o "${_OUTPUT_FILE}"
- yt-dlp -f "${FORMATS}" --add-metadata --embed-thumbnail --recode-video mp4 --prefer-ffmpeg --audio-format aac "${_URL}" -o "${_OUTPUT_FILE}"
- rm $YOUTUBE_MEDIA_LIST
Advertisement
Add Comment
Please, Sign In to add comment