scripts/youtube-dl-install-ubuntu20...

97 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/bash
#############################################################
# Author: Taryel Hlontsi, 2022
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
#############################################################
DIR=$HOME
not_set() {
count=$(grep -Pcx "$1" "$2")
code=$?
if (( $code > 0 )) || (( $count == 0 )); then
true
else
false
fi
}
which youtube-dl
if (($? > 0)); then
sudo apt update
sudo apt install -y python3-pip
sudo apt install -y ffmpeg
fi
pip install youtube-dl --upgrade --user
SCRIPT="${DIR}/.local/bin/get-mp3.sh"
ALIAS="alias get-mp3='${SCRIPT}'"
if test -f ~/.bash_aliases; then
if not_set "${ALIAS}" "${DIR}/.bash_aliases"; then
echo $ALIAS >> "${DIR}/.bash_aliases"
fi
else
echo $ALIAS > "${DIR}/.bash_aliases"
fi
source "${DIR}/.bashrc"
cat <<'EOF' > ${SCRIPT}
#!/bin/bash
if [[ ! ${1+x} ]]; then
echo 'Provide a web link to the media file as an argument!'
exit -1
else
URL=$(echo $1 | sed "s/\?list=.*//i")
echo "Link: ${URL}"
fi
youtube-dl --list-formats $URL
read -t 10 -p 'Select the format number from above [251]: ' FORMAT
echo $FORMAT
if test -z "$FORMAT"; then
FORMAT=251
else
if [[ ! $FORMAT =~ ^[0-9]+$ ]]; then
echo 'Specified format must be a number!'
exit -1
fi
fi
echo "Format to be used: ${FORMAT}"
youtube-dl --format $FORMAT \
--no-call-home \
--console-title \
--ignore-config \
--buffer-size 16K \
--no-resize-buffer \
--extract-audio \
--audio-quality 0 \
--audio-format mp3 \
-o "~/Music/%(title)s.%(ext)s" \
"${URL}"
EOF
chmod u+x $SCRIPT
echo "to use it run 'get-mp3' command followed by a URL link (in a new terminal session)"