Downloading Music from YouTube is easy with yt-dlp, but there are many ways to do it and it depends on what kind of playlist you want.
There are three types of playlists on YouTube:
- Regular playlists
- A playlist with consisting of multiple video files. The URL always begins with
https://www.youtube.com/playlist?list=
. Here is an example.
- A playlist with consisting of multiple video files. The URL always begins with
- Single-video with multiple tracks
- One long video containing multiple tracks. Often separated by chapters. Here is an example.
- YouTube Music albums
- YouTube Music is a paid premium service, here is an example of an album.
Regular playlist
Her is a method you can use to download regular playlists. In other words playlists contain multiple videos as opposed to long continuous videos with multiple audio tracks in one video. The IRL always begins with https://www.youtube.com/playlist?list=
.
In other words, we are going to download a playlist containing multiple videos, each containing an audio track.
Here is the yt-dlp command we are using. The command is very long so it is written line, by line separated with the ^
. On Linux and Mac, this may be different. In that case, you can just remove the caret symbols.
yt-dlp ^
--update ^
--ignore-config ^
--yes-playlist ^
--format bestaudio ^
--extract-audio ^
--embed-thumbnail ^
--embed-metadata ^
--convert-thumbnails jpg ^
--parse-metadata "%(artist,webpage_url_domain)s playlists:%(artist)s" --add-metadata ^
--parse-metadata "artist:(?s)(?P<meta_album_artist>.+)" --add-metadata ^
--parse-metadata ":(?P<meta_date>)" ^
--parse-metadata "%(album,playlist_title)s:%(album)s" --add-metadata ^
--parse-metadata "%(track_number,playlist_index|0)s:%(track_number)s" --add-metadata ^
--cookies-from-browser "firefox:C:/Users/admin/AppData/Roaming/librewolf/Profiles"::Google ^
--write-info-json ^
--output "~/Desktop/%(webpage_url_domain|unknown_website)s playlists/%(playlist_title)s/%(track_number,playlist_index)02d %(title)s [%(webpage_url_domain)s WEB-DL].%(ext)s" ^
"https://www.youtube.com/playlist?list=PLJ6iE9ACR2noPiDHx_X1SGZ2nrcRvuk_z"
Here is an example of how it will look in a media player called Foobar2000:
Code explanation:
yt-dlp
All yt-dlp command should start with yt-dlp.
--update
This updates the application before proceeding to download.
--ignore-config
If you have any configuration files for yt-dlp, this will ignore them. If you don’t know what a config file is, ignore this.
--yes-playlist
We tell yd-dlp explicitly to download the whole playlist.
--format bestaudio
Tells yt-dlp to select the available format that has the best audio quality. For YouTube, this is usually the Opus format. The point here is to disregard video quality and prioritize the audio quality.
--extract-audio
Extract only the audio from the video provided.
--embed-thumbnail
Embedds the video thumbnail as the music artwork in the audio file.
--embed-metadata
Adds metadata from the video, such as title to the audio file.
--convert-thumbnails jpg
YouTube stores thumbnail images as WebP files. We need them in JPEG to be able to embed them in the audio files.
--parse-metadata "%(artist,webpage_url_domain)s playlists:%(artist)s" --add-metadata
By default, yt-dlp will try to find the artist from the artist, creator, uploader, or uploader_id fields, and set it as the title for the artist field. We don’t want that. Instead, we want the artist
metadata of the audio file to be called youtube.com playlists
.
--parse-metadata "artist:(?s)(?P<meta_album_artist>.+)" --add-metadata
In this part we are telling yt-dlp to use the artist
field as the basis for the audio file metadata. In other words we are letting yt-dlp know that we would like to use youtube.com playlists
as the artist
field in the Opus file.
--parse-metadata ":(?P<meta_date>)"
Since different videos in a playlist can be uploaded on completely different dates, we are telling yt-dlp to exclude the date field. If we don’t do this media players will display the playlists in a weird way, separating the tracks by year.
--parse-metadata "%(album,playlist_title)s:%(album)s" --add-metadata
Here we are using the playlist title as the album name.
--parse-metadata "%(track_number,playlist_index|0)s:%(track_number)s" --add-metadata
Here we are using the sorting order of each video in a playlist as a basis for the track numbers in the files.
--cookies-from-browser "firefox:C:/Users/admin/AppData/Roaming/librewolf/Profiles"::Google
Since I am using LibreWolf as my browser together with the Firefox Multi-Account Containers extension, I am loading cookies with this command. Google
is the name of my container. Strictly speaking, you don’t have to load cookies unless you are trying to download a private playlist.
--write-info-json
Since videos can be removed from YouTube any time, you should always include this line. This will save a .info.json file with a lot of information about the video like uploader id, duration, track nuber, original links, thumbnail links and so on. If the video is deleted you will still know where the video was downloaded from.
–output “~/Desktop/%(webpage_url_domain|unknown_website)s playlists/%(playlist_title)s/%(track_number,playlist_index)02d %(title)s [%(webpage_url_domain)s WEB-DL].%(ext)s"
This will save the file as for example: C:\Users\admin\Desktop\youtube.com playlists\Super Rub 'a' Dub Music\07 Theme1 tub - Super Rub'a'Dub [youtube.com WEB-DL].opus
"https://www.youtube.com/playlist?list=PLJ6iE9ACR2noPiDHx_X1SGZ2nrcRvuk_z"
The URL for the YouTube playlist.
Single-video with multiple tracks
If you want to download a long video containing several music tracks merged together in one long video, you can use this method.
yt-dlp ^
--update ^
--ignore-config ^
--yes-playlist ^
--write-playlist-metafiles ^
--format bestaudio ^
--extract-audio ^
--embed-thumbnail ^
--embed-metadata ^
--add-metadata ^
--embed-chapters ^
--convert-thumbnails jpg ^
--parse-metadata "%(artist,webpage_url_domain)s playlists:%(artist)s" --add-metadata ^
--parse-metadata "artist:(?s)(?P<meta_album_artist>.+)" --add-metadata ^
--parse-metadata ":(?P<meta_date>)" ^
--parse-metadata "%(title)s:%(album)s" --add-metadata ^
--cookies-from-browser "firefox:C:/Users/admin/AppData/Roaming/librewolf/Profiles"::Google ^
--write-info-json ^
--output "~/Desktop/%(webpage_url_domain|unknown_website)s playlists/%(title)s/%(title)s [%(webpage_url_domain)s WEB-DL].%(ext)s" ^
"https://youtu.be/Pa_s7ogtokM"
In this case, we don’t need a track number, because we are just downloading one long video.
Instead of using the playlist title as the album name we are using the title of the video as the album name.
Note that it is not possible to split a video by chapters with yt-dlp when extracting audio at the moment. Perhaps this will be enabled in the future.
Albums from YouTube Music
Note that YouTube Music is a premium streaming service for YouTube Premium members.
If you want to download an album from YouTube Music you can use this method:
yt-dlp ^
--update ^
--ignore-config ^
--yes-playlist ^
--write-playlist-metafiles ^
--format bestaudio ^
--extract-audio ^
--embed-thumbnail ^
--embed-metadata ^
--add-metadata ^
--embed-chapters ^
--convert-thumbnails jpg ^
--parse-metadata "title:%(artist)s - %(title)s" ^
--parse-metadata ":(?P<meta_synopsis>)" ^
--parse-metadata "%(album,webpage_url_domain)s:%(album)s" --add-metadata ^
--parse-metadata "%(track_number,playlist_index|0)s:%(track_number)s" --add-metadata ^
--parse-metadata "channel:(?s)(?P<meta_album_artist>.+)" --add-metadata ^
--cookies-from-browser "firefox:C:/Users/admin/AppData/Roaming/librewolf/Profiles"::Google ^
--write-info-json ^
--output "~/Desktop/%(channel,uploader|NA)s/%(upload_date>%Y)s - %(album,webpage_url_domain)s"/"%(track_number,playlist_index)02d %(title|)s [%(webpage_url_domain)s WEB-DL].%(ext)s" ^
"https://music.youtube.com/playlist?list=OLAK5uy_nCzBucUbL-WJ-nxBeZBoqrl2u9h1icFK8"
Here is how it will look in Foobar2000: