Downloading videos from YT Subs and deleting them after having been watched

Please see the end of the post for the links to the raw files on PasteBin. Also, yes, this is going to be posted on reddit.

EDIT: Had to change portion of find. switched atime which counts in days, to amin which counts in minutes.

Here is a script I (with the help of /u/ebb) wrote that downloads videos off of your specified YouTube channels and deletes the videos once they've been watched. If you have any suggestions or improvements, feel free to post them in the comments below.


#!/bin/bash

float=$(cat /mnt/mnemosyne/Scripts/YT-Subs/te.txt) && float=${float%.*} && var=$(expr 59 - $float / 60);

find ~/Videos/YT-Subs -amin -$var -exec rm {} \;;

/usr/bin/time -f "%e" youtube-dl -f "bestvideo[height<=?1080]+bestaudio/best" -i \
--playlist-items 1-4 --download-archive /mnt/mnemosyne/Scripts/YT-Subs/archive.txt \
--dateafter now-2days --rate-limit 1M -a /home/brandon/Documents/channellist.txt -o \
'/home/brandon/Videos/YT-Subs/%(uploader)s/%(upload_date)s %(uploader)s %(title)s' 2> \
/mnt/mnemosyne/Scripts/YT-Subs/te.txt


I set this script up to run once every hour on the hour using cron.

Quick overview of exactly is happening here:

  1. We are using the float placeholder as an intermediate variable. float first gets equated to the output of the string found in te.txt.

  2. Because the output of time -f "%e" {command} is a floating point integer, we must remove everything after the decimal. So we equate float to itself, excluding the decimal point and anything after it.

  3. var is then defined and equated to 59 minus one sixtieth of float**

  4. We then find and delete (rm) any files whose last time being accessed is less than now minus var. Essentially, just deleting all files in that directory has been accessed since the last time the script was run.

  5. We then run the youtube-dl command. We run it inside time , simply to be able to output the time the youtube-dl command takes to run from start to end.


*te.txt is the output of the time command, stored in a txt file. In te.txt is the elapsed time of the youtube-dl execution in seconds, including milliseconds after the decimal.

** We want var to be equal to the time elapsed, in minutes, since the the youtube-dl command has finished and this execution.


Specifics on each part of the youtube-dl options:

  • -f "bestvideo[height<=?1080]+bestaudio/best" and whatever directly follows will be specifying the format wanted. First preference is anything with the best video and audio, but whose video height is 1080 pixels or less (1080p). Second preference is simply the best available should nothing fit into the first preference.

  • -i is to simply ignore any errors, and continue onto the next piece of material.

  • --download-archive /mnt/mnemosyne/Scripts/YT-Subs/archive.txt records all the IDs of videos downloaded and checks against that file to see if a video found by youtube-dl has previously been downloaded. If it has, it doesn't re-download it.

  • --dateafter 2days downloads videos that we posted any time within the last 2 days, still adhering to --download-archive as well,

  • --rate-limit 1M simply limits the video download rate to 1MBps.

  • -a /home/brandon/Documents/channellist.txt tells youtube-dl to look in channellist.txt to find the links of where to search for videos. In my case, I have the videos pages of many of my favourite YouTubers.

  • -o '/home/brandon/Videos/YT-Subs/%(uploader)s/%(upload_date)s %(uploader)s %(title)s' tells youtube-dl where to place the videos, and with what naming paradigm to name the videos.

  • --playlist-items 1-4 Is a time saver. If this were not included, youtube-dl would go through each and every video a channel has uploaded, checking to see if it's previously been downloaded, and if not, whether it fits the upload date limitations made by --dateafter.


ytsubdl.sh - the script itself - http://pastebin.com/wQt83MdT
channellist.txt - http://pastebin.com/kw0vkaBG
archive.txt - http://pastebin.com/2HzUrhjM