July 28, 2021
Things that are online may spontaneously disappear, or may be not-so-spontaneously removed by different companies.
What if you want to archive your favorite Youtube channel so that, in case it gets deleted, you still have the videos?
You can do this easily with youtube-dl
.
Before I continue any further, a disclaimer: I don’t condone or encourage downloading copyrighted material. If you don’t have the rights to something on Youtube, I have to tell you not to download it.
With that out of the way, let’s get to the actual guide.
First, make sure you have youtube-dl installed.
You can get the official version, but personally I use yt-dlp.
yt-dlp
is a maintained fork of youtube-dlc
(which is in turn a fork of youtube-dl
), and includes numerous community improvements; consult its Github repo (linked in the previous sentence and in the sidebar) for more details.
As an example for this guide, let’s say I want to download Doc Schuster’s playlists (which is a fantastic physics channel, I owe a large part of my IB HL Physics grade to his videos).
Let’s also say I created a directory for this channel at /mnt/video-storage/doc-schuster
.
Here’s the command I’d use to download all playlists on the channel:
youtube-dl -ciw -f bestaudio+bestvideo -o '/mnt/video-storage/doc-schuster/%(playlist)s/%(playlist_index)s_%(title)s_%(id)s.%(ext)s' --yes-playlist -r 2.5M https://www.youtube.com/channel/UCQnKBRdXfNgxhn_XDrFdHjA/playlists
This is what the command does:
youtube-dl
: run the youtube-dl
executable, I have it symlinked to yt-dlp
-c
: resume any partially downloaded files (good if your connection gets interrupted, or if you stop the download manually)-i
: ignore errors, so if some video is unavailable the whole download doesn’t stop-w
: don’t overwrite anything-f bestaudio+bestvideo
: download best audio and video available. These may be in separate formats, in which case youtube-dl will download them separately and merge them.-o /mnt/video-storage/doc-schuster/%(playlist)s/%(playlist_index)s_%(title)s_%(id)s.%(ext)s
: download files to /mnt/video-storage/doc-schuster
, into directories named after playlists, with filenames including the playlist position, video title, and extension--yes-playlist
: download the playlist-r 2.5M
: limit the download rate to 2.5 Mbps so as to not hog bandwidth (you can adjust this as needed)https://www.youtube.com/channel/UCQnKBRdXfNgxhn_XDrFdHjA/playlists
: the Youtube link to the channel’s playlistsI recommend running this in a tmux
session, so you can detach/reattach terminals as needed.
If you want to download the whole channel, use the link to the channel instead (i.e. omit the /playlists
and the end of the URL).
You might also want to see which playlists a channel has. If you have jq (a JSON parser/processor) installed, you can use this to get a tab-separated list of playlist names and URLs:
youtube-dl --dump-json --flat-playlist https://youtube.com/channel/ID/playlists | jq -s 'map([.title, .url] | @tsv) | .[]'
Here’s the breakdown:
youtube-dl
: run youtube-dl--dump-json
: don’t download anything, print JSON info--flat-playlist
: don’t extract videos of playlist, just list themhttps://youtube.com/channel/ID/playlists
: the link to the channel’s playlists, replace with the actual link from Youtube| jq -s
: pipe to jq
, reading the input (JSON data from youtube-dl) into a large array'map([.title, .url] | @tsv) | .[]'
: a jq
expression that takes only the title and URL of each playlist and puts them on a single line, tab-separatedThen you can select the playlists that you want, and pass them to youtube-dl
.