How to Download Youtube Videos via the Command Line

Youtube-TerminalI don’t purport this tutorial as being the ideal way to download Youtube videos to your computer. A simple Google search will lead you to an assortment of options that requires less technical expertise. However, if you’re like me, and are relatively new to the command-line (think your OS without the GUI and a mouse), this might be an interesting way to accomplish something relatively useful (download Youtube videos to your computer) and also obtain a better understanding of how your computer works.

Basic requirements

In this tutorial, I’ll be using a command-line program called youtube-dl. It requires that you have Python version 2.6, 2.7, or 3.3+ to work. You can easily check your machine’s current Python version with the following command:

python --version

The program is not platform specific so it should work for Unix, Windows, and Mac OS X. However, this tutorial will be tailored for Mac users.

Installing youtube-dl

First, click this link to download the latest version of youtube-dl. If your web browser doesn’t automatically save to file to your computer, try right-clicking the link and hitting “Save Link As.”

Next, open up Terminal on your Mac. Type in:

cd /usr/local/

NOTE: This is a hidden directory on your computer system where some of the core programs are being handled. Thus, I urge extra caution when working here.

I also like to use the ls command which outputs a list of all the files and directories I can navigate to next based on my current location. Go ahead and try it now.

Next, type into Terminal:

open bin

Click to enlarge

Click to enlarge

A new Finder window should appear containing a bunch of cryptic looking files. The reason why we have to navigate to this folder via Terminal is because by default it is a hidden system folder, meaning it can’t be reached via Finder alone. There is a Terminal command to display all hidden files by default, but it is generally advised not to do so.

authenticate

Press Authenticate and enter your login credentials

Paste your youtube-dl file now in the bin folder you are in. A pop-up will appear telling you that you don’t have permission to do so. Select Authenticate and enter your login password. Verify that the file was correctly saved to the directory.

A short intro to file permissions

File permissions determine which users can perform a certain action on specific files and directories. There are “read”(r), “write”(w), and “execute”(x) permissions and are assigned to three types of users: a) user (u) b) group (g) and c) other (o). Read the following articles for a more further details:

The reason why I mention file permissions is because you will need to edit the file permissions for the youtube-dl.py file currently residing in your bin folder. At the moment, the file only has read and write access to the user, which is you. You can view the permissions using the ls command mentioned earlier, by including the -l option, which displays extra details.

So, first navigate inside the bin folder:

cd bin

This moves you into the bin folder, where you had saved the youtube-dl file to in the previous step.

Then, type in the following…

ls -lg youtube-dl

Which should result in…

-rw-r-xr-x@ 1 tonyhue staff 139584 Oct 20 18:33 youtube-dl

In order for the program to run, you need to give yourself execution permission. Currently, youtube-dl has read and write access. To add execution permission, we will use the chmod command by typing in:

chmod a+x youtube-dl

Verify that the correct permissions for the file contain read, write, and execution permission by typing in:

ls -lg youtube-dl

Quick-and-dirty installation

Alternatively, you can have youtube-dl up and running quickly by using the following commands:

sudo wget https://github.com/downloads/rg3/youtube-dl/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+x /usr/local/bin/youtube-dl

Don’t copy-paste the entire set. Instead, enter each one at a time.

When performing this step, I also got an authentication error from Github that said the following:

ERROR: cannot verify github.com's certificate, issued by `/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV CA-1':
  Unable to locally verify the issuer's authority.
To connect to github.com insecurely, use `--no-check-certificate'.
Unable to establish SSL connection.

Just modify the first command like so:

sudo wget --no-check-certificate https://github.com/downloads/rg3/youtube-dl/youtube-dl -O /usr/local/bin/youtube-dl

Almost there!

Congrats! You’ve (hopefully) made it this far unscathed. After modifying the permissions for the youtube-dl file, you can now start downloading Youtube videos to your computer from the command-line! Just type in:

youtube-dl [youtube video link]

Don’t include the bracket parenthesis!

Things should be working properly if Terminal outputs something that looks like this:

[youtube] w639H9ZUqZw: Downloading video webpage 
[youtube] w639H9ZUqZw: Downloading video info webpage 
[youtube] w639H9ZUqZw: Extracting video information 
[download] Destination: w639H9ZUqZw.mp4 
[download] 100.0% of 9.53M at 9.06M/s ETA 00:00

If all has gone according to plan, you should be able to find your newly-downloaded video in your Users folder. Yeah, I know. Terrible place to put it, amiright?

Troubleshooting

  • wget isn’t working
  • Make sure you’ve installed it correctly.

  • Permissions problem
  • Verify that youtube-dl has execute(x) permissions by using the command inside the bin folder where the file is located:

    ls -lg youtube-dl

    Also, if you’re still in the bin folder where youtube-dl is located, you probably should navigate to a different folder like your Desktop, otherwise you would need to use sudo to bypass permission problems.

  • Windows and Unix
  • According to the author:

    In Windows save the exe version somewhere (in your PATH if you want to run it simply as youtube-dl) and run it from the command line.

    In Unix, download it, give it execution permission and copy it to one of the PATH directories (typically, /usr/local/bin).

    Also, it is recommended that you run youtube-dl -U in order to maintain the latest version of the program.

What else?

Pat yourself on the back once more! You’ve successfully downloaded a Youtube video to your computer! Youtube-dl has a number of available options to customize the video you download, such as changing the video filename to the video’s title instead of its id. Also, the program can also download videos from other sources besides Youtube, including Vimeo, Daily Motion, and Metacafe. The sky is the limit!

BTW, you should follow me on Twitter here.

  • wsch

    thanks for the instructions!