ClavinJune

yt-dl.sh

Mar 28th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.73 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage () {
  4.   echo "./$0 <youtube-video-id> <resolution>"
  5.   echo "example: ./$0 cdwal5Kw3Fc 360p"
  6. }
  7.  
  8. main () {
  9.   url="https://youtube.com/get_video_info?video_id=$1"
  10.  
  11.   videoInfoFile=$(wget -qO- $url)
  12.  
  13.   declare -A videoInfo
  14.  
  15.   while read key value
  16.   do
  17.     videoInfo["$key"]="$value"
  18.   done < <(awk -F'&' '{for(i=1;i<=NF;i++) {print $i}}' <<< $videoInfoFile | awk -F'=' '{print $1" "$2}')
  19.  
  20.   playerResponse=${videoInfo["player_response"]}
  21.   decodedPlayerResponse=$(printf '%b' "${playerResponse//%/\\x}")
  22.  
  23.   result=($(echo $decodedPlayerResponse | jq .'streamingData' | jq '.formats' | jq '.[] | .url, .qualityLabel'))
  24.  
  25.   title=$(echo $decodedPlayerResponse | jq .'videoDetails' | jq .'title' | tr "\"" " " | tr -d '[:space:]' | tr "+" " ")
  26.  
  27.   declare -A links
  28.  
  29.   for ((i = 0 ; i < ${#result[@]}-1 ; i+=2))
  30.   do
  31.     url=$(echo ${result[$i]} | tr "\"" " " | tr -d '[:space:]')
  32.     resolution=$(echo ${result[$i+1]} | tr "\"" " " | tr -d '[:space:]')
  33.     links["$resolution"]="$url"
  34.   done
  35.  
  36.   if [ "${links["$2"]}" = "" ]; then
  37.  
  38.     if [ ${#links[@]} -eq 0 ]; then
  39.       echo "+ Video not available"
  40.     else
  41.       echo "+ Resolution not available"
  42.       echo "+ Downloadable resolution: ${!links[@]}"
  43.     fi
  44.     exit 1
  45.   fi
  46.  
  47.   target="$title-$2.webm"
  48.   wget ${links["$2"]} -qO "$target"
  49. }
  50.  
  51. if [ $# -eq 0 ]; then
  52.   usage
  53.   exit 1
  54. fi
  55.  
  56. main $1 $2
  57. exit 0
  58.  
  59.  
  60. # echo ${links["$2"]}
  61.  
  62. # echo $result
  63. # result=$()
  64. # echo $result
  65.  
  66. # jsonFormat=$(echo $videoInfo | tr '&' '\n' | jq -Rsc 'split("\n")')
  67. # jsonLength=$(echo $jsonFormat | jq length)
  68. # echo $jsonFormat | jq '.[csi_page_type]'
  69. # x=$(curl $url)
  70. # echo $x
  71. # cat ../Downloads/get_video_info | tr '&' '\n' | jq -Rsc 'split("\n")' | jq '.[0]'
Add Comment
Please, Sign In to add comment