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 very 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,
accidentally put things into your Homebrew python3.* path and randomly run
commands until a completely unrelated Python project works again. That is
great.
We get 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 is annoying when npm is provided by your OS 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 does not have it available anymore after it EOL’s. In that case the above procedure would have to be rerun after performing the following:
rm -r ~/.local/share/yt-dlp
A simple update for an existing installation of yt-dlp can be run 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 mental Detroit Jit on a 2h git bisect
session, I would like to download and listen to
this mix. I want all my
downloads to be converted to m4a (AAC), as unlike WebM/Opus, Apple Music
supports AAC quite well. (sad emoji) Further, I want my downloads to be put
into separate folders by date within ~/Music/ytdl
, and that the metadata on
YouTube is preserved.
So we cobble together this command, to be run in fish:
yt-dlp --extract-audio \
--audio-format m4a \
--output "~/Music/ytdl/"(date '+%Y-%m-%d')"/%(title)s-%(id)s.%(ext)s" \
--embed-metadata \
(read)
When we run this, (read)
instructs our shell to insert whatever we put there
as an argument to our command. We can paste the URL we have noted in our
browser and press enter. A minute later or so, the download is finished and
encoded to m4a. You can even use something like (pbpaste)
or any other system
clipboard access to skip the step of having to paste things into your terminal
every time.
If this mix ever disappears from the Interwebz, I will still have it on my local machine. The enjoyment can never be taken from me again >:)