CLI Magic: Enhance your YouTube viewing pleasure

1212

Author: Joe Barr

Publishing your own video on YouTube, or watching other people’s videos, is all the rage these days. Why are we talking about YouTube, in a column about the CLI? Because this week we’re writing about youtube-dl, a clever little CLI tool that’s easy to install and use to fetch YouTube videos.

Youtube-dl is a Python script, licensed under the “non-copyleft” free software MIT/X11 license. It is not platform-specific; it can run under Linux, Mac OS X, or Windows platforms so long as a recent — 2.4 or later — version of Python is installed.

Installation consists of viewing a page in your browser and then saving it to your hard drive. Once that is done, you can check the MD5 checksum on the download page to make sure you saved an undefiled version. Assuming you have md5sum installed on your system, enter the following command at the CLI:

md5sum youtube-dl

You should see the following output:


faae21b5ffaa31a6ee5f06b77226d4ed youtube-dl

That’s a match with the sum on the download page, so you’re good to go. The MD5 checksum will change, of course, as the program is updated.

The next step is to make the file executable. As downloaded, youtube-dl permissions are probably “-rw-r–r–“, so you wouldn’t be able to execute the file as-is. To make it executable for the owner, enter the following command at the CLI in the directory containing the file:

chmod 744 youtube-dl

Then, as root, move the script into a directory in your path, such as /usr/bin. That’s all there is to the install.

Using it is just as easy. All you need to do is enter the command youtube-dl followed by the URL of the video you want to fetch:

$ youtube-dl http://www.youtube.com/watch?v=p_YMigZmUuk
Retrieving video webpage... done.
Extracting video URL parameters... done.
Retrieving video data... 4590k of 4590k done.

Unless you specify the filename using the -o option, youtube-dl will save the file as videoid.flv; if your URL is http://youtube.com/watch?v=5Da9sc6YDBo, the video ID is 5Da9sc6YDBo.

Once you have the file, you can play it using MPlayer or another video player that supports ffmpeg. The youtube-dl page says that VLC should also work, but it refused to play the saved video on my systems.

Youtube-dl has a few simple command-line options. You can display all of the options by running youtube-dl -h:

$ youtube-dl -h
usage: youtube-dl [options] video_url

options:
  -h, --help            print this help text and exit
  -v, --version         print program version and exit
  -u USERNAME, --username=USERNAME
                        account username
  -p PASSWORD, --password=PASSWORD
                        account password
  -o FILE, --output=FILE
                        output video file name

In addition to the -o option, which allows you to change the video file name, you can also specify your YouTube username and password, if they’re required to download certain content.

That’s the whole thing. Youtube-dl is a small, focused, and very usable CLI tool that enhances your video enjoyment.