I want to download all my favorite music to my local machine for completely offline listening. My goal is to have the same music library on both my iPhone (iOS) and MacBook (macOS).

<aside> ☝

This is for personal use only. I don't encourage illegal activities.

</aside>

Download music

<aside> ⚠️

Be careful: If you use --cookies-from-browser chrome to bypass the 403 error, you'll need to grant yt-dlp your OS user password. This gives the program access to your Chrome cookies and potentially stored passwords. Use at your own risk and make sure to download the official version from GitHub.

</aside>

Download a single file

yt-dlp --cookies-from-browser chrome \\
	-x --audio-format mp3 \\
	--embed-metadata --embed-thumbnail	\\
	--parse-metadata ".*:(?P<album>tinh_tao)"	\\
	--parse-metadata "uploader:(?P<artist>.*)" \\
	--replace-in-metadata "title" " \\[.*?\\]" "" \\
	-o "%(title)s.%(ext)s" \\
	"PLAYLIST_URL"

Download a playlist

Download all songs in .mp3 format from a YouTube playlist using yt-dlp

yt-dlp --cookies-from-browser chrome \\
	-x --audio-format mp3 \\
	--embed-metadata --embed-thumbnail \\
	--parse-metadata ".*:(?P<album>tinh_tao)" \\
	--parse-metadata "uploader:(?P<artist>.*)" \\
	--replace-in-metadata "title" " \\[.*?\\]" "" -o "%(title)s.%(ext)s" \\
	--download-archive downloaded.txt \\
	--no-overwrite \\
	"PLAYLIST_URL"

This command downloads and converts all songs from a YouTube playlist to .mp3 format. It saves the downloaded video IDs in downloaded.txt to prevent downloading the same song twice (using --no-overwrite). The command also captures all metadata (title, artist, album, cover) and removes [youtube_id] from the title.

The --cookies-from-browser option helps bypass the 403 (Forbidden) error.

Other options are available in the yt-dlp repository. Here are some useful ones:

If you only want to generate the downloaded.txt file without downloading anything:

yt-dlp --flat-playlist --no-overwrite \\
	--get-id "PLAYLIST_URL" | sed 's/^/youtube /' > downloaded.txt

To play a notification sound when your download completes:

yt-dlp .... && afplay /System/Library/Sounds/Glass.aiff

Using scripts to quickly update your music library

You can create aliases (in zsh or bash) to efficiently download newly added songs from your playlists. For example, I have three playlists—"choices," "mood," and "rock"—with corresponding folders of the same names in ~/Download/musics/