Downloading things from YouTube

Published: October 4, 2023, updated: January 6, 2025

Here are some notes on how to download things from YouTube using yt-dlp (PyPI link).

Installation

I use venv and Python 3.11 here.

Whoa: venv works well across Python versions and operating systems independent of any package manager. I wasn’t aware for a long time that there is no need to run pip3 install --user and pollute ~/.local. Or, worse, put things into your Homebrew python3.* path by accident and run random commands until an entirely unrelated Python project works again. That’s great.

You can achieve maximal isolation with minimal hassle. It’s also unlike npm install -g, since in that case you would be doing a system-wide install again. That’s annoying when your OS provides npm and installing packages would put them in a non-user-specific path.

# Create virtual environment
python3.11 -m venv ~/.local/share/yt-dlp
# Update setuptools/pip and install yt-dlp
~/.local/share/yt-dlp/bin/pip install -U setuptools pip yt-dlp
# Just need to link this binary, and we're done
ln -s ~/.local/share/yt-dlp/bin/yt-dlp ~/.local/bin

An edge case might be python3.11 itself disappearing, for example when a package manager removes it after it EOL’s. In that case, run the following snippet, and then run the preceding installation one more time.

rm -r ~/.local/share/yt-dlp

You can update an existing installation of yt-dlp like so:

# Same command as above
~/.local/share/yt-dlp/bin/pip install -U setuptools pip yt-dlp

Downloading DJ sets

I download a lot of DJ sets. I don’t like leaving my browser open just to listen to music. I use MusicBrainz Picard and Apple Music to manage my music and sync it over to other devices. I find a DJ set that I like on YouTube and note the URL.

For example, if I feel like doing a Detroit Jit inside my head on a 2 h git bisect session, I would like to download and listen to this mix. I want to convert downloads to m4a (AAC) with the YouTube metadata intact. Apple Music only supports AAC, but not WebM/Opus. (sad emoji)

Further, I want to store this and all my downloads into separate directories. Each should directory contain the downloads for a given date within ~/Music/ytdl

To achieve this, run the following in the fish shell:

yt-dlp --extract-audio \
  --audio-format m4a \
  --output "~/Music/ytdl/"(date '+%Y-%m-%d')"/%(title)s-%(id)s.%(ext)s" \
  --embed-metadata \
  (read)

in the preceding snippet, (read) instructs the shell to insert whatever you input there as an argument to yt-dlp. Paste the URL of the video you want to download from YouTube and press enter.

A minute later or so, the download finishes and yt-dlp encodes the result to m4a. You can even use something like (pbpaste) (on macOS) or any other system clipboard access and skip the step of having to paste things into your terminal every time.

Even if your favorite mix ever disappears from the Interwebz, you now have it on your local machine. No one can ever steal your favorite mix again. >:)

Tags

I would be thrilled to hear from you! Please share your thoughts and ideas with me via email.

Back to Index