kkdai / youtube

Download Youtube Video in Golang
MIT License
3.37k stars 433 forks source link

How to set highest quality when downloading? #287

Closed humanfriend22 closed 10 months ago

humanfriend22 commented 1 year ago

Videos have differing qualities, so I would like to download them at the highest quality available. How is this possible?

tosiek88 commented 1 year ago
#!/usr/bin/env bash

set -Eeuo pipefail

parse_params() {
  args=("$@")

  # check required params and arguments
  [[ ${#args[@]} -eq 0 ]] && die "Missing script arguments"

  echo "[]" > /tmp/youtube.json 

  youtubedr list https://www.youjsontube.com/watch\?list\=${args} -f json | \
  jq '.Videos[] | .ID' | xargs -I {} \
  sh -c "\
    json=\$(cat /tmp/youtube.json);
    (echo \"\$json\" | jq '.+=[{\"Video\":\"{}\"}]') > /tmp/youtube.json
  " 

  json=$(cat /tmp/youtube.json);

  for row in $(echo $json | jq -r '.[] | .Video'); do

    info=$(youtubedr info -f json https://www.youtube.com/watch\?v\=$row)
    itag=$(echo $info | jq '.Formats[0] | .Itag')
    video_quality=$(echo $info | jq -r '.Formats[0]|.VideoQuality')
    jq --arg video "$row" --arg itag "$itag" --arg video_quality "$video_quality" '(.[] | select(.Video==$video)) | {Video, Itag:$itag, VideoQuality:$video_quality}' /tmp/youtube.json

    youtubedr download https://www.youtube.com/watch\?v\=$row \
    -q ${itag}
  done

  return 0
}

parse_params "$@"
sudo chmod +x ~/youtube.sh 

Parsing whole playlist and selecting highest video quality.

~/develop/youtube/youtube.sh PL9IEJIKnBJjEPxenuhKU7J5smY4XjFnyg                          ─╯
{
  "Video": "a8mPKBxQ9No",
  "Itag": "137",
  "VideoQuality": "1080p"
}
4.64 MiB / 4.64 MiB 100 % [===================================================] 0s ] 1.86 GiB/s
{
  "Video": "G5dNorAoeCM",
  "Itag": "137",
  "VideoQuality": "1080p"
}
yangxuhui commented 1 year ago

Setting quality to empty string allows you to download the highest quality available. youtubedr download -q "" G208iCLwNN8