Bash – Download YouTube Music

| | | | | |
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl
sudo apt update
sudo apt upgrade -y
sudo apt install youtube-dl python ffmpeg net-tools -y

# Install YouTube Downloader CLI
brew install youtube-dl ffmpeg

# https://github.com/rg3/youtube-dl
# Download YouTube video, convert the video to MP3 and delete the original video
youtube-dl -i -f mp4 -x --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?v=A9XtDyDUjIU

# Download YouTube video, convert the video to MP3 and keep the original video as MP4 format
youtube-dl -k -i -f mp4 -x --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?v=A9XtDyDUjIU

# Download YouTube video as MP4 format
youtube-dl -i -f mp4 https://www.youtube.com/watch?v=A9XtDyDUjIU

Alternate option, read a text file.

#!/bin/bash

while IFS='' read -r line || [ -n "$line" ]; do
    echo "Downloading $line"
    youtube-dl -i -f mp4 -k -x --audio-format mp3 --audio-quality 0 $line
done < "$1"

#youtube-dl -i -f mp4 --yes-playlist $1

# -i, --ignore-errors
# -f, --format
# -x, --extract-audio
# --audio-format
# --yes-playlist
# -k to keep the video

# mkdir music
# mkdir video

count=`ls -1 *.mp3 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
    mv *.mp3 music
fi 

count=`ls -1 *.mp4 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
    mv *.mp4 video
fi

Create a text file with the YouTube links with a line break between each. The links can be a single video or a playlist URL, the downloader will parse the playlist and download each of the videos in the playlist and convert to MP3

sh youtube-dl.sh music.txt
Originally Posted on August 7, 2018
Last Updated on June 30, 2019
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.