Automatically downloading Youtube videos on Linux

     Is there a certain youtube channel that you subscribe to, or don't, but you want to download there new videos when they come out, well then you're in luck, 'cause in this post I'm going to be telling you how to achieve this. You're going to need a few scripts, but don't worry its all painless, and if I could figure out on my own, you can figure it out with my instructions. ;)
     First things first, you're going to need to have a script installed on your system called Youtube-dl. (Source and installation instructions here) and then you're going to need the URL of the youtube channel, for this tutorial I'll be using Mark Crilley's channel http://www.youtube.com/user/markcrilley Substitute this URL with the URL of the channel you want to download from. There are two ways to do the next step, one is to write a bash script that we execute with cron, the other would be to do it all in cron. I don't know if either way is better than the other, so I'll show you both and let you decide which you want to use. (For the record I'm using a separate bash script.)

     Method 1: (Using bash script)
     Create a new directory and in that directory create a new file, call it Update.sh technically you can call it anything you want, so long as it ends with the .sh extension. Copy and paste this text into the new file and save.

#!/bin/bash 

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

 # Make script crontab friendly: 
cd $(dirname $0) 

youtube-dl https://www.youtube.com/user/markcrilley -o "%(title)s.%(ext)s" --playlist-end 1 -R 50 



     Method 2: (Scripting in Cron)
    The second way to do this is entirely in cron where we'd use the exact same code. Open a terminal, (Ctrl + Alt + T) and type in crontab -e This will open your personal crontab paste in this code, (Ctrl + Shift + V)

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

* * * * * youtube-dl https://www.youtube.com/user/markcrilley -o "%(title)s.%(ext)s" --playlist-end 1 -R 50

          I'll explain the asterisks a little later.

     Both of these do about the same, so I'll only explain what's going on once.
     Well starting at the beginning, we define that it's a bash script. The next line we include for the youtube-dl script to run, as cron won't look in some directories by default, so we have to let it know that it needs to. I honestly have no idea what exactly the cd $(dirname $0) does, but it makes it work. Then lastly we have the running of the script. Replace the URL with the URL for the channel you want to download from. The "%(title)s.%(ext)s" will name the downloaded file with the videos name, and give it the correct extension. The "--playlist-end 1" will download the last video, most recent, that the channel has uploaded. and lastly the "-R 50" will retry the download fifty times, just to make sure everything does download for you.

     You should be able to double click on the .sh file you just created and have a download start shortly. Of course to see what is happening you could open a terminal in that folder and then enter ./Update.sh to run the script in the terminal.

     If you opted to make a script open up your crontab by opening a terminal and typing crontab -e You'll be greeted with a blank window that you can type in. The first thing you're going to want to do is define with the script should be run. This definition comes in the form of five groups, the five asterisks in Method 2, If you think of the asterisks are letters it may be easier to figure out what to set them as. m h d m w
  • m minute
  • h hour
  • d day of month
  • m month 1-12
  • w weekday 0-7, Sun,Mon, etc (Sunday = 0 = 7)
  • It is easy to remember if you think of the way one would say a date: Wednesday, July 29, at 10:30, then reverse the order.
In my case Mark uploads a new video every Friday, so I want to download the video at 3 am on Saturday so I'd use * 3 * * 6

    This is what I'd put as my command * 3 * * 6 /home/Nathan/downloads/mark/Update.sh
    Or if doing everything in my crontab I'd just replace the string of asterisks with * 3 * * 6 and then enter the rest of the command.

     Downsides to this system:
          It doesn't seem to work on playlists, just on channels.
          This doesn't monitor for new videos, just downloads the newest at a given time. (Technically you should be able to run the script every day, even every hour if you wanted, and it would only download the video once, as it will recognize if the file has already been downloaded, and not download it again, this only works if the downloaded file remains in the download directory.)


     As a bonus I'm going to tell you how to have youtube-dl update itself for you, so you don't have to worry about that either.

    Open the root crontab by typing sudo crontab -e and put in this code.

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

25 4 * * 6 youtube-dl --update

This will update Youtube-dl every Saturday at 4:25 am. You can change this to run every day if you want.

     Further Reading:
          Cron
          Youtube-dl

     If you have any questions don't hesitate to ask in the comments, or give me a shout out on G+

Add new comment