Skip to content

Instantly share code, notes, and snippets.

@vlazic
Last active June 18, 2021 07:26
Show Gist options
  • Save vlazic/604f464c1e3ad563b8a8d42fe8eba1e0 to your computer and use it in GitHub Desktop.
Save vlazic/604f464c1e3ad563b8a8d42fe8eba1e0 to your computer and use it in GitHub Desktop.
Download list of YouTube videos in MP3 format
#!/usr/bin/env bash
blue='\033[0;34m'
nocolor='\033[0m'
green='\033[0;32m'
downloadFolder="download"
playlistFile="playlist.txt"
function exit_on_error_and_undefined() {
# Will exit script if we would use an uninitialised variable:
set -o nounset
# Will exit script when a simple command (not a control structure) fails:
set -o errexit
}
function test_dep() {
command -v "$1" >/dev/null 2>&1
}
function install_if_not_installed() {
for i in "$@"; do
test_dep "$i" || sudo apt install -y "$i"
done
}
function info() {
echo -e "${blue}------------------------------------------------------------"
echo -e "$*"
echo -e "${nocolor}"
}
function success() {
echo -e "${green}✅ $*${nocolor}"
}
exit_on_error_and_undefined
if [ -t 0 ]; then
echo "Usage: cat playlist.txt | yt-dl.sh"
exit 1
fi
install_if_not_installed docker detox rename
mkdir -p "${downloadFolder}"
cd "${downloadFolder}" >/dev/null || exit 1
cat /dev/stdin >"$playlistFile"
info "Downloading songs into: $(pwd)"
docker run \
--rm -i \
-e PGID="$(id -g)" \
-e PUID="$(id -u)" \
-v "$(pwd)":/workdir:rw \
mikenye/youtube-dl \
--extract-audio \
--audio-format mp3 \
--ignore-errors \
--no-overwrites \
--output "%(title)s.%(ext)s" \
--continue \
--batch-file "$playlistFile"
success "Done"
info "Cleaning filenames"
detox -r -v --remove-trailing "$(pwd)"
rename --verbose 's/_/ /g' "$(pwd)"
rename --verbose 's/-/ - /g' "$(pwd)"
success "Done"
rm -f "$playlistFile"
cd - >/dev/null || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment