|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# ydownl - a simple youtube-dl download script |
| 4 | +# |
| 5 | +# USAGE: |
| 6 | +# ./ydownl.sh URL |
| 7 | +# |
| 8 | +# REFERENCE |
| 9 | +# youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o "%(playlist_index)s %(playlist)s - %(title)s.%(ext)s" https://www.youtube.com/watch?v=Y52M28WQu2s |
| 10 | +# |
| 11 | +# HISTORY: |
| 12 | +# 20210322 - Version 1.0.0 Initial |
| 13 | +# core implementation |
| 14 | +# including support for notification (zenity) |
| 15 | +# basic support for user configs |
| 16 | + |
| 17 | +# ------------------------------------------------------------------------------ |
| 18 | +# TODO: |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | +# * if no url provided - use notification/dialog to ask for url |
| 21 | + |
| 22 | +# ------------------------------------------------------------------------------ |
| 23 | +# DEBUG |
| 24 | +# ------------------------------------------------------------------------------ |
| 25 | +# Debugging: This will report the usage of uninitialized variables |
| 26 | +#set -u |
| 27 | + |
| 28 | +# ------------------------------------------------------------------------------ |
| 29 | +# DEFINE CONSTANTS - DON'T TOUCH |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +SCRIPTNAME="ydownl.sh" |
| 32 | +SCRIPTVERSION="1.0.0" |
| 33 | +SCRIPTDEMOURL="https://www.youtube.com/watch?v=Y52M28WQu2s" |
| 34 | + |
| 35 | +# ------------------------------------------------------------------------------ |
| 36 | +# USER CONFIG |
| 37 | +# ------------------------------------------------------------------------------ |
| 38 | +# |
| 39 | +# CONFIG_YTDL_AUDIOFORMAT: Specify audio format: |
| 40 | +# "best", |
| 41 | +# "aac", |
| 42 | +# "flac", |
| 43 | +# "mp3", |
| 44 | +# "m4a", |
| 45 | +# "opus", |
| 46 | +# "vorbis", or |
| 47 | +# "wav"; |
| 48 | +# "best" by default; No effect |
| 49 | +CONFIG_YTDL_AUDIOFORMAT="mp3" # default: "mp3" |
| 50 | + |
| 51 | +# CONFIG_YTDL_AUDIOQUALITY: Specify ffmpeg/avconv audio quality,insert a value between 0 (better) and 9 |
| 52 | +# (worse) for VBR or a specific bitrate like 128K (default 5) |
| 53 | +CONFIG_YTDL_AUDIOQUALITY=0 |
| 54 | + |
| 55 | +# CONFIG_ZENITY_TIMEOUT: Defines the timeout for the zenity notification dialog after download finished |
| 56 | +CONFIG_ZENITY_TIMEOUT=15 # default 5 |
| 57 | + |
| 58 | +# ------------------------------------------------------------------------------ |
| 59 | +# FUNCTIONS |
| 60 | +# ------------------------------------------------------------------------------ |
| 61 | +function initColors() { |
| 62 | + # format |
| 63 | + bold=$(tput bold) |
| 64 | + normal=$(tput sgr0) |
| 65 | + blink=$(tput blink) |
| 66 | + reverse=$(tput smso) |
| 67 | + underline=$(tput smul) |
| 68 | + |
| 69 | + # colors |
| 70 | + black=$(tput setaf 0) |
| 71 | + red=$(tput setaf 1) |
| 72 | + green=$(tput setaf 2) |
| 73 | + yellow=$(tput setaf 3) |
| 74 | + lime_yellow=$(tput setaf 190) |
| 75 | + powder_blue=$(tput setaf 153) |
| 76 | + blue=$(tput setaf 4) |
| 77 | + magenta=$(tput setaf 5) |
| 78 | + cyan=$(tput setaf 6) |
| 79 | + white=$(tput setaf 7) |
| 80 | +} |
| 81 | + |
| 82 | +function reset() { |
| 83 | + tput reset |
| 84 | +} |
| 85 | + |
| 86 | +function showHeader() { |
| 87 | + printf " ${bold}${lime_yellow}%s${normal} - ${bold}%s ${normal}\n" "$SCRIPTNAME" "$SCRIPTVERSION" |
| 88 | + printf " ${bold}----------------------------------------------------------${normal}\n" |
| 89 | +} |
| 90 | + |
| 91 | +function showNotification() { |
| 92 | + if ! hash notify-send 2>/dev/null |
| 93 | + then |
| 94 | + printf "Notifications using notify-send is not supported - skipping ...\n" |
| 95 | + else |
| 96 | + #notify-send -u low -t 0 "$SCRIPTNAME" "$1" |
| 97 | + zenity --info --text="$1" --title="$SCRIPTNAME" --width="500" --height="150" --timeout="$CONFIG_ZENITY_TIMEOUT" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +function checkIfExists() { |
| 102 | + if ! hash "$1" 2>/dev/null |
| 103 | + then |
| 104 | + # does not exist |
| 105 | + printf "${red}[ Error ]${normal} $1 not found\n" |
| 106 | + exit 1 |
| 107 | + else |
| 108 | + # exists |
| 109 | + printf "${green}[ OK ]${normal} $1 detected\n" |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +# ------------------------------------------------------------------------------ |
| 114 | +# SCRIPT |
| 115 | +# ------------------------------------------------------------------------------ |
| 116 | +reset # clear the screen |
| 117 | +initColors # initialize the color and font formating variables |
| 118 | +showHeader # show the script header |
| 119 | +checkIfExists "youtube-dl" |
| 120 | +checkIfExists "zenity" |
| 121 | + |
| 122 | +# Check if a parameter was supplied - if not stop execution |
| 123 | +if [ -z "$1" ] |
| 124 | +then |
| 125 | + printf "${red}[ Error ]${normal} no URL detected. Usage: ./%s %s\n\n" "$SCRIPTNAME" "$SCRIPTDEMOURL" |
| 126 | + exit 1 |
| 127 | +else |
| 128 | + printf "${green}[ OK ]${normal} URL detected\n" |
| 129 | + URL=$1 # save url in variable |
| 130 | +fi |
| 131 | + |
| 132 | +# check if the url is valid |
| 133 | +if curl --output /dev/null --silent --head --fail "$URL"; then |
| 134 | + printf "${green}[ OK ]${normal} URL is valid\n" |
| 135 | + printf "\nStart processing the following url:\n\t${bold}%s${normal}\n\n" "$URL" |
| 136 | + |
| 137 | + # start downloading (alt: youtube-dlc) |
| 138 | + youtube-dl -f bestaudio --extract-audio --audio-format "$CONFIG_YTDL_AUDIOFORMAT" --audio-quality $CONFIG_YTDL_AUDIOQUALITY -o "%(playlist_index)s %(playlist)s - %(title)s.%(ext)s" $URL |
| 139 | + showNotification "Finished downloading\n\t<a href='$URL'>$URL</a>" |
| 140 | +else |
| 141 | + printf "${red}[ Error ]${normal} URL is not reachable. Aborting..\n\n" |
| 142 | + exit 1 |
| 143 | +fi |
0 commit comments