/ howto

automatically download your youtube subscriptions

using a raspberry pi and a simple script, you are able to have your recent youtube subscriptions downloaded on a daily basis. if you want your subscriptions be delivered in a different interval, there's an option for that. and other custom adjustments are possible, too. and here's how:

table of contents


hardware

the raspberry pi foundation just released the new version raspberry pi 3 b+. if you haven't already, you should grab a raspberry pi. don't assume that this guide will work on every model, since there are a multiple of different operating systems and resulting software dependencies. i'm using the raspbian os, running on model 3 the day this is published.

software pre-requisites

please have you raspberry pi set up and running following any of the guides out there in the internet. if you're too lazy to use a search engine, please start here: software guide on raspberrypi.org

please open terminal and login to your raspberry pi via ssh, then install, in order (paste and run commands in terminal):

  1. linuxbrew: see linuxbrew.sh for more information and installation help

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

  1. youtube-dl and ffmpeg: (run in terminal after linuxbrew has been installed properly)

brew install youtube-dl ffmpeg

  1. to create the folder structure, please run:

mkdir -p ~/Scripts/yt-subscriptions/video ~/Scripts/yt-subscriptions/cache

  1. optionally: if you want to transfer files from your raspberry pi to another computer, i'd suggest installing resilio sync: official sync guide: installing sync package on linux

the script

please create a file at path ~/Scripts/yt-subscriptions/ytsubs.config and paste this content (run nano ~/Scripts/yt-subscriptions/ytsubs.config and paste):

# Authentication Options
--username 'YTUSER
--password 'YTPASS'
--mark-watched

# Download Options
--dateafter today-1day
--max-downloads 64
--yes-playlist
--no-progress
--no-warnings
--no-cache-dir

# Video Quality
--format 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'

# Post-processing Options
--add-metadata
--embed-subs

# Filesystem Options
--output '/home/pi/Scripts/yt-subscriptions/cache/%(title)s.%(ext)s'
--exec 'mv -f {} /home/pi/Scripts/yt-subscriptions/video/'

please enter your own username (YTUSER) and password (YTPASS), then save the file (ctrl-o, enter) and quit nano (ctrl-x). next, create a file at path ~/Scripts/yt-subscriptions/ytsubs-cron.sh and paste this content:

#!/bin/sh

# Define vars
BASEDIR=$(dirname "$0")
LOGFILE="$BASEDIR/ytsubs.log"
PATTERN="upload date is not in range"

# Reset Logfile
rm -f ${LOGFILE}
touch ${LOGFILE}

# Download Subscriptions using youtube-dl AND inspect logfile for occurrence of PATTERN to interrupt execution of youtube-dl
echo "Downloading YouTube Subscriptions"

# Download Video and Wait until PATTERN occurs
python /home/linuxbrew/.linuxbrew/bin/youtube-dl --config-location '/home/pi/Scripts/yt-subscriptions/ytsubs.config' :ytsubs > "${LOGFILE}" & ( tail -f -n0 "${LOGFILE}" & ) | grep -q "${PATTERN}"

echo "Done downloading most recent subscriptions"
echo "Stopping processes"

# Killing Process IDs
ps aux | grep 'youtube-dl' | grep 'python' | kill $(awk '{print $2}')

echo "Done."
exit

save the file and quit nano (ctrl-o, enter, ctrl-x) and make the file executable by running: chmod +x ~/Scripts/yt-subscriptions/ytsubs-cron.sh.
if you are using a different folder structure, please adjust the paths at the corresponding lines inside both files.

setting up the cronjob

next and last thing to do is setting up the cronjob:

crontab -e and add this line:

0 1 * * * /bin/sh ~/Scripts/yt-subscriptions/ytsubs-cron.sh

save and exit the editor. this cronjob is now scheduled for execution, daily at 1 am. modify it to adapt your needs. if you plan to run it weekly, please also change the config file: --dateafter today-7days