Wednesday, June 10, 2009

Thinking out of the BoX !! Download YouTube Videos : The Linux Way !


In this article I will outline a few methods to download YouTube Videos to your system and getting them to play in your favorite player. We will go in a step-wise way :

A. Getting the Video

1. Online Tools

There are some sites like KeepVid.com which allow users to input a YouTube link and they would give you the download link. So get the download link from this site and download the flv file.
2. Firefox Plugin
There is a firefox extension which allows you to download YouTube videos as you watch them. I haven’t tried this.
3. Command Line Tools
This is the safest and best way (for CLI junkies :-D ). Download the script YouTube-dl (thanks to Ricardo Garcia Gonzalez for this nice script). Just put that script in your ~/bin or /usr/bin directories or somewhere in the application path.
To download the flv video do this :
youtube-dl
e.g : youtube-dl http://www.youtube.com/watch?v=D1R-jKKp3NA

B. Playing the video

1. Use VLC Player or MPlayer or Kaffeine to play the flash video.
2. If the above method doesn’t work out (It din’t in my case)..Then go the difficult way :
* Get ffmpeg for your system. SuSE users can get it here.
* Convert the flv video to avi (or any other format) using ffmpeg :
ffmpeg -i my_flash_video.flv the_avi_file.avi
* Now drag and drop the avi file into your favorite video player n watch the video :-)

Still unsatisfied ?! “Dude I’m a *nix user. I can’t sit and do all these steps to get a simple video playing !!” No probs. We will write a shell script to do all this work for you.

Note : I found the downloader in youtube-dl quite slow. So I tweaked it a bit(I don’t know python :-D ) to use an external downloader (axel rocks!). Open the file youtube-dl in your favorite editor and change the following lines :

282 # Abort here if in simulate mode
283 if cmdl_opts.simulate:
284 sys.exit()

to the following :

282 # Abort here if in simulate mode
283 if cmdl_opts.simulate:
284 cond_print(video_data.geturl())
285 sys.exit()

That will make the script output the downloadable video link to the standard output (when run in simulate mode). Now write the shell script :
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Usage : $0 "
echo "e.g : $0 http://www.youtube.com/watch?v=D1R-jKKp3NA steve_jobs"
else
outputfile=".avi"
todnload=`youtube-dl -s $1 | tail -n 1 -`
echo "Got the file.."$todnload
axel -n 100 $todnload -o $2".flv" # wget -c $todnload -O $2".flv" .if you don't use axel
echo "Download Completed..."
ffmpeg -i $2".flv" $2$outputfile # get the avi file
fi

Done. Now paste that code into a file utube in your path. Then run utube as :
utube http://www.youtube.com/watch?v=D1R-jKKp3NA steve_jobs

This will give 2 files steve_jobs.flv and steve_jobs.avi ;)
Just drag the *.avi to kaffeine and enjoy the speech by Steve Jobs !

No comments:

Post a Comment