Download songs from Youtube



Get started

Download the yt-dlp .exe file (Updated version of Youtube-dl)

https://github.com/yt-dlp/yt-dlp#installation

Download ffmpeg .7z file

Customize your output folder

- Create new file `yt-dlp.conf` in the same folder
- Enter and save: 
-o ./Songs/%(title)s.%(ext)s

Test

The folder should now look like this:

ffmpeg.exe
yt-dlp.exe
yt-dlp.conf

To test that it all works, run this in cmd in the same folder:

1
./yt-dlp.exe

You should see this output:

1
2
3
4
Usage: yt-dlp.exe [OPTIONS] URL [URL...]

yt-dlp.exe: error: You must provide at least one URL.
Type yt-dlp --help to see a list of all options.

Download by search term

How does this work?

  • You enter the name of the song to be downloaded
  • It searches Youtube for that name
  • Downloads the first result

The name can also be the Youtube URL of the song itself, since searching Youtube for a URL shows that video as the first result.

Download a song

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#Test: 
#This searches for the input name and displays the title of the first result
#For example, "Wolfmother - Joker and the Thief" is the first result for the search term "joker and the thief"
#More options: https://github.com/ytdl-org/youtube-dl/blob/master/README.md#verbosity--simulation-options
./yt-dlp.exe "joker and the thief" --default-search "ytsearch" --get-title --no-playlist
Wolfmother - Joker and the Thief

#Download audio with thumbnail and metadata.
#You should see the downloaded song in the /Songs folder after running this
./yt-dlp.exe "joker and the thief" --default-search "ytsearch" --no-playlist --extract-audio --audio-format "mp3" --audio-quality 0 --embed-thumbnail --add-metadata

Download multiple songs

  • Create a file song-list.txt with either song names or youtube URLs, 1 value in each line.
    Example:
Joker and the thief
https://www.youtube.com/watch?v=eBGIQ7ZuuiU:258cg0vt
Country roads john denver
  • Test run: Get the names of songs that will be downloaded
1
./yt-dlp.exe --default-search "ytsearch" --no-playlist --extract-audio --audio-format "mp3" --audio-quality 0 --embed-thumbnail --add-metadata --batch-file "./song-list.txt" --get-title
  • Download the songs
1
./yt-dlp.exe --default-search "ytsearch" --no-playlist --extract-audio --audio-format "mp3" --audio-quality 0 --embed-thumbnail --add-metadata --batch-file "./song-list.txt"

Download a Youtube Music playlist

Steps

  • Get all song names in the playlist using the commands below
  • Put it in the song-list.txt file
  • Download it using the commands above

Get the list of songs from Youtube Music playlist

  • Navigate to the playlist page with songs listed. Scroll down till you reach the bottom.
  • Open the devTools console on the Playlist page.

Preventing duplicate downloads

Have you already downloaded songs before from this list? In that case, we want to download only the “new” songs now.
Run this code in the console:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//Copy-paste all older song names here so that we get only new songs which aren't already present
let oldSongs;
oldSongs = `<copy-paste all older songs here!>`;
//Example:
oldSongs = `All That Glitters - Earl, Tongue Tied
Don't Stop Believin' - Journey, Time 3
Jailhouse Rock - Elvis Presley, Elv1s: 30 #1 Hits
I'm Your Man - Leonard Cohen, I'm Your Man
Ride - twenty one pilots, Blurryface
Love Me Again - John Newman, Tribute`;

Getting the songs list

Run this code in the console.
You should see all new songs listed; copy-paste it into the song-list.txt file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Get a list of songs
let songs = [];
let duplicates = [];
if (!oldSongs) {
    oldSongs = '';
}
let allElements = document.getElementsByClassName('flex-columns style-scope ytmusic-responsive-list-item-renderer');
for (let i = 0; i < allElements.length; i++) {
    let song = '';
    let currentElem = allElements[i];
    let subElements = currentElem.querySelectorAll('yt-formatted-string');
    if (subElements && subElements.length > 0) {
        if (oldSongs.indexOf(subElements[0].title.toLowerCase()) < 0) {
            song += subElements[0].title + ' - ' + subElements[1].title + ', ' + subElements[2].title;
        } else {
            duplicates.push(subElements[0].title);
        }
    }
    if (song != '') {
        songs.push(song);
    }
}
console.log(songs.join('\n'));
if (duplicates.length > 0) {
	console.log('Duplicates: \n' + duplicates.join('\n'));
}

//Copy paste the song names into a text file

Download Spotify liked songs

Duplicates aren’t handled here yet. I don’t use Spotify. If you do, I’ll add it in!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
let songs = new Set();
let songList, artistList = [];

//Get classnames from console
function populateData() {
	songList = document.getElementsByClassName('Type__TypeElement-goli3j-0 fCtMzo t_yrXoUO3qGsJS4Y6iXX standalone-ellipsis-one-line');
	artistList = document.getElementsByClassName('Type__TypeElement-goli3j-0 hHrtFe rq2VQ5mb9SDAFWbBIUIn standalone-ellipsis-one-line');
}
populateData();

function addSongs() {
	if (songList.length != artistList.length) {
		console.log(songList.length + ', ' + artistList.length + ' == exiting.')
		return;
	}
	for(let i = 0; i < songList.length; i++) {
		let temp = '';
		temp += songList[i].innerText + ' - ';
		temp += artistList[i].childNodes[0].innerText;
		//console.log(temp);
		songs.add(temp);
	}
	console.log('Total songs: ' + songList.length);
}
addSongs();

//songs Set now contains all songs
let allSongsAsText = '';
songs.forEach((element) => {
	allSongsAsText += element + '\n'
})
//This prints all songs as a single message
console.log(allSongsAsText);