Wednesday, January 6, 2010

Downloading Youtube videos in Linux

Lots of people who watch Youtube videos like to download them for viewing it offline later. On a Linux box, the best tool you can find is a small command line utility called "youtube-dl". This is available as package in fedora/debian/ubuntu/suse repositories. Even if this tool is not in your repository, you can download it directly from the project website. This script requires python, so make sure you have it. Unzip the downloaded archive and copy it to system path (or run from there itself - if you wish).

To download a video, you need to get its Youtube URL which is the URL that you see in the address bar of your browser when you play the video. It will look like this: http://www.youtube.com/watch?v=RANDOM_STRING. The RANDOM_STRING will contain alpha numeric characters and dash.

Let us see the actual use of the tool.

safeer@penguinpower:~$youtube-dl http://www.youtube.com/watch?v=QTw6iexyrNU

It will throw some output about the URL and then start downloading, showing the current download speed and ETA. When finished, it will report "Video data saved to QTw6iexyrNU.flv". The file will be saved in the current directory. If you want to save to another location or change the name of the file, use option "-o".

The default format for the saved video will be "flv" but if you use the -b option ( for best qulity ) the format will be mp4. So change your filename accordingly when using -o and -b together.

There are many other useful options including the option to give username and password for videos that can be accessed only by logged in Yuotube users. Use "youtube-dl -h" to see all the options.

One thing that you should remember about Youtube URLs is that if the url contains one or more "&" symbols, you need to take only the part on the left hand side of the first "&". For eg, if you got this URL "http://www.youtube.com/watch?v=QTw6iexyrNU&feature=related" from your browser, then the actual URL is "http://www.youtube.com/watch?v=QTw6iexyrNU". The rest are arguments to the Youtube website which will not be used by youtube-dl.

This tool has a windows GUI which can be found here.

Now for the people who want to do things their way, following method may be interesting. Open your browser and play the video from Youtube and wait for it to finish. Once the video is played to the full length, open a terminal keeping the Youtube page open. Then run this command on terminal.

safeer@penguinpower:~$ for i in $(find /tmp -type f -user $(whoami) 2>/dev/null);do [[ $(file $i) =~ "Flash" ]] && echo $i;done

/tmp/FlashRXaVqD

This file is the video you just watched, if you want to cross check you can run this file in some media player like vlc.

safeer@penguinpower:~$ vlc /tmp/FlashRXaVqD&

And it will play the video. Now save the file with a name and flv extension.

safeer@penguinpower:~$mv /tmp/FlashRXaVqD ~/chess-play.flv

Not a big deal, but it will save your bandwidth, if you want to first play the video and then only download it.

Chrome is developing a browser extension for Youtube downloading, I haven't tested it yet. Have a look into it if you are interested.

Disclaimer: Downloading Youtube content is subject to copy right and content policies of the concerned parties, the author or this blog will not be responsible for such issues.

No comments:

Post a Comment