It took me a while to figure out how to work with the Raspberry Pi. Especially when doing (and trying to remember) harder terminal commands. Let’s take a look at getting YouTube video’s downloaded for later use, using only a Raspberry Pi to do so. (and watching on a device of your choosing, iOS/mac in my case)
what you need to get started
- Raspberry Pi, I’ve used the 4 for this since it’s a decent machine, 2 GB of memory cuts it for me. (full setup doc here)
- External storage, for testing a SD card will do — for more than that, don’t.
- A file-sharing setup for devices to access the files (e.g., SMB or AFP)
- A decent internet connection (files can get pretty big)
- A device to play your downloaded videos on
- A pinch of patience
Getting your installation going
For now, youtube-dl can be downloaded freely, the easiest way using
curl to get you going:
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
This downloads and adds the youtube-dl binary to your
usr/local/bin folder on your device.
You’ll need to allow execution and reading rights:
sudo chmod a+rx /usr/local/bin/youtube-dl
And you’re ready to go! Try it out to get started:
youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best <youtube videoID>' # this gets the best audio and video format
My setup additions
I have a few use cases to download YouTube playlists:
- Getting videos for working out to, learning more about my body
- Music video’s I love — YouTube is a treasure chest for great 90s music or live-sets
- ‘up to date’ content that I’d like to watch straight away
I’m using cronjobs for doing this (using the crontabs ui, that I’ll write about later) to achieve: //todo: documentation on crontabs
- checking a playlist every 5 minutes for updates
- doing an hourly download of workout videos
- downloading music once every day
they’re all loosely based on the following bash script:
dlpath="/mnt/share/youtube/" #sets the path to download files to dlfolder="watchnow/" #set the folder playlist="playlistID" #set what playlistID to use to download /usr/local/bin/youtube-dl --restrict-filenames --add-metadata --yes-playlist--write-thumbnail \ --download-archive "$dlpath$dlfolder/listing.txt" \ -o\ "$dlpath$dlfolder(%(epoch)s) %(title)s.%(ext)s" \ https://www.youtube.com/playlist?list=$playlist
I save the bash script for each of the playlists I want to save, and run it as a cronjob regularly.
It looks like a lot for a single command, but in short:
add-metatadataadds the metadata to the file afterwards, which helps with additional information when previewing
write-thumbnailwrites a thumbnail of the video. It shows you the preview thumbnail in supported software.
download-archivewill keep an archive of downloaded files in a text file, which prevents downloading over, and over again.
- Output is stored in the download folder, with the filename including an
epochtimestamp to be able to sort files easier, and keeping track of recent downloads
I’ve had the most success with Infuse on AppleTV and iOS, supporting playlists — instant playback without video format problems (VC9 or MP4) and syncing video status.
It’s great with metadata and thumbnails, which makes it look better.
Bonus tip for the Mac users out there, IINA. Unknown, but the best player out there!