Skip to content

Instantly share code, notes, and snippets.

@z-zawhtet-a
Forked from bluenex/ytdl_bash_addon.md
Last active September 10, 2015 14:36
Show Gist options
  • Save z-zawhtet-a/4cbf9f13ba20ea8da64c to your computer and use it in GitHub Desktop.
Save z-zawhtet-a/4cbf9f13ba20ea8da64c to your computer and use it in GitHub Desktop.

youtube-dl add-on

This add-on is to:

  • bypass certification checking for youtude-dl (2015.04.26) on OSX (10.10.3)
  • pass through proxy (MU proxy)
  • download the best quality of audio or video
  • save to ~/Music/youtube-dl

note Since the latest update of youtube-dl (2015.08.23) and OSX (10.10.4), there is no need to bypass certificate anymore. The proxy also seems to be fixed (by importing system proxy.. maybe). However, the options will remain until I'm sure to take them out later.

requirements

Only youtube-dl is needed now.

pip install --upgrade youtube-dl

where to place snippet

sudo nano ~/.bash_profile

function

# youtube-dl
# due to certification error, this alias is to bypass

chk-ytdl-dir() {
if [ ! -d ~/Music/youtube-dl ]; then
  mkdir ~/Music/youtube-dl
  echo "created ~/Music/youtube-dl"
fi
}

ytdl() {
  ## checking
  testlen=$1
  if [[ $# -gt 2 ]]; then
    echo "Only 2 arguments are allowed!"
    return 1
  fi
  if [[ ${#testlen} -gt 4 ]]; then
    echo "Maximum length of option is 4.. (-sph or -vph)"
    return 1
  fi
  if [[ $1 == *"s"* && $1 == *"v"* ]]; then
    echo "Could not download both song and video at once!"
    return 1
  fi

  ## downloading
  curdir="$PWD"
  if [[ $1 != *"h"* ]]; then
    chk-ytdl-dir
    cd ~/Music/youtube-dl/
  else
    echo "Download to current path.."
  fi
  if [[ $1 == *"s"* ]]; then
    echo "Downloading song.."
    if [[ $1 == *"p"* ]]; then
      echo "With proxy.."
      youtube-dl -o "%(title)s.%(ext)s" -f "m4a" --proxy "" --no-check-certificate $2
    else
      youtube-dl -o "%(title)s.%(ext)s" -f "m4a" --no-check-certificate $2
    fi
  elif [[ $1 == *"v"* ]]; then
    echo "Downloading video.."
    if [[ $1 == *"p"* ]]; then
      echo "With proxy.."
      youtube-dl -o "%(title)s.%(ext)s" -f "best" --proxy "" --no-check-certificate $2
    else
      youtube-dl -o "%(title)s.%(ext)s" -f "best" --no-check-certificate $2
    fi
  elif [[ $1 == *"4k"* ]]; then
    echo "Downloading 4k video.."
    if [[ $1 == *"p"* ]]; then
      echo "With proxy.."
      youtube-dl -o "%(title)s.%(ext)s" -f "(mp4)[height>1080]+m4a" --proxy "" --no-check-certificate $2
    else
      youtube-dl -o "%(title)s.%(ext)s" -f "(mp4)[height>1080]+m4a" --no-check-certificate $2
    fi
  else
    echo "No satisfied parameters filled.."
    echo "Please use -s for song, -v for video, -p if proxy and -h to save to current path."
  fi
  cd $curdir
}

usage

option:

  • -s download song.
  • -v download video.
  • -p if being behind MU proxy.
  • -h download to current path.

youtube link: https://www.youtube.com/watch?v=Iwc81dpA008

ytdl -s https://www.youtube.com/watch?v=Iwc81dpA008
ytdl -sp https://www.youtube.com/watch?v=Iwc81dpA008
ytdl -vh https://www.youtube.com/watch?v=Iwc81dpA008
ytdl -vph https://www.youtube.com/watch?v=Iwc81dpA008

note

  • Cannot download both song and video at once!
  • .m4a is supposed to be best format, but recently is being substituted by .webm. Anyway, I prefer .m4a because it's more widely supported.
  • Converter to .mp3 and .m4a will be included in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment