-
-
Save cxbig/2a0309fd9c13f0f0d0732a77ef9163fc to your computer and use it in GitHub Desktop.
Alfred | Run Script | Download YouTube video(s) by youtube-dl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Alfred : Run Script | |
## Options : /bin/zsh | with input as argv | Sequentially | |
alias youtube-dl='/usr/local/bin/youtube-dl' | |
if [[ -z "$1" ]]; then echo 'Please provide a URL'; return 1; fi | |
youtube_root="$HOME/YouTube" | |
[[ ! -d $youtube_root ]] || mkdir -p $youtube_root | |
if [[ $? -gt 0 ]]; then echo "Failed to create YouTube root directory : $youtube_root"; return 1; fi | |
if [[ "$1" =~ ^https:\/\/www\.youtube\.com\/ ]]; | |
then | |
params=${1#*\?} | |
list_id=`echo $params | sed -nE 's/.*[&]?list=([^&]+).*/\1/p'` | |
if [[ ${#list_id} -gt 0 ]]; | |
then | |
dir="$youtube_root/_list/$list_id" | |
url="https://www.youtube.com/playlist?list=${list_id}" | |
[[ -d $dir ]] || mkdir -p $dir | |
if [[ $? -gt 0 ]]; then echo "Failed to create video directory : $dir"; return 1; fi | |
( cd $dir && youtube-dl -q --no-warnings $url 2>&1 && echo "List $list_id downloaded" || return 1 ) | |
return 0 | |
fi | |
video_id=`echo $params | sed -nE 's/.*[&]?v=([^&]+).*/\1/p'` | |
if [[ ${#video_id} -gt 0 ]]; | |
then | |
dir="$youtube_root/_video" | |
url="https://www.youtube.com/watch?v=${video_id}" | |
[[ -d $dir ]] || mkdir -p $dir | |
if [[ $? -gt 0 ]]; then echo "Failed to create video directory : $dir"; return 1; fi | |
( cd $dir && youtube-dl -q --no-warnings $url 2>&1 && echo "Video $video_id downloaded" || return 1 ) | |
return 0 | |
fi | |
else | |
dir="$youtube_root/_list/$(date +'%Y%m%d_%H%M%S')" | |
[[ -d $dir ]] || mkdir -p $dir | |
if [[ $? -gt 0 ]]; then echo "Failed to create video directory : $dir"; return 1; fi | |
( cd $dir && youtube-dl -q --no-warnings $1 2>&1 && echo "Video downloaded" || return 1 ) | |
return 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment