Youtube Downloader in Python

I was surprised to read an article on BBC that stated that YouTube had eaten substantially into television's already declining viewership. Many others did not even find it worth taking note of because they had envisioned that YouTube would be a disruptive application (true in more than a sense). An even more surprising fact was that digital TV had failed to live up to people's expectations. On the other hand YouTube's popularity keeps increasing exponentially. Some benefits of YouTube are:

  1. Interactivity:The user is the king. He gets to watch what he loves. He can play a critique and collaborate with people irrespective of geographical locations.
  2. Lack of Censorship:Probably one of the biggest advantages of YouTube. Such services are a blessing for people in countries like China, Iran etc. It is their window to the outside world. (more so with the BBC and YouTube deal in place)
  3. Personalization:Services in YouTube can be personalized. Unlike TV, YouTube displays unobstrusive and contextual ads. Just try locating ads on its site and you would take a while to realise their presence.
  4. Copyright and Copyleft:This particular aspect was in spotlight recently, thanks to the Viacom episode. Why do producers keep cribbing about the losses incurred by the industry? They (probably don't realise that through such media) can showcase their work to truly global audiences and get much needed publicity for (almost) free. How can anyone enjoy a movie or a music video (in such a small screen) online? The producers need to understand that internet tariffs are dearer than movie tickets or music cd's (atleast in developing countries). Rather than crying foul, they must concentrate on improving the quality of their work. They should make their work compelling enough that it'll drag people off their PC's, into movie halls. The other advantage for users is the availability of copyleft (rather Creative Commons) material. These profess sharing and improvization of work(truly Web 2.0).

An analogy that I can think of

"Lots of book publishers protested against the release of e-books fearing loss of revenue. But indirectly e-books have helped the book publishers. A potential customer will skim through a few pages or a few chapters (if he is patient enough and no one would risk going blind reading an e-book) and then decide if it is worth the paper edition. This process is quite to similar to the one that happens in a book shop. If you don't believe me check out Lulu or Apress."

Now coming to the point of why I wrote the YouTube downloader. Well a couple of reasons inspired me to go ahead:

  1. Keeping archives of some real cool videos. There are some videos on YouTube that ought to be treasured. Why go through the pain of re-searching and then waiting for the video to stream? Once on the disk, play them whenever required. Also what is the guarantee that the video will stay online for long? It could be pulled off if some suckers have their say.
  2. To show off my programming skills (or rather Python's power to make a dumb ass programmer look kewl). Frankly, I love challenges and problem solving.

Before I post the code I need to give credit to Takashi Ohida of http://video.qooqle.jp/dl/ for his online YouTube downloader. I downloaded his PHP source and adapted it to the commandline using Python. He saved me the pain of going through those hundreds of lines of YouTube's source. As usual my gratitude goes to Guido Van Rossum and the Python community for the beautiful Python Programming Language.

import urlgrabber,re
import urlgrabber.progress

url = raw_input("Enter the URL: ")
file_name = raw_input("Enter a filename: ")

video_id = re.compile("\?v=(.*)").findall(url)[0]
a = urlgrabber.urlread(url)
param = re.compile("watch_fullscreen\?video_id=.*?&t=(.*?)&sk").findall(a)[0]

final_url = "http://youtube.com/get_video.php?video_id="+video_id+"&t="+param

prog = urlgrabber.progress.text_progress_meter()
urlgrabber.urlgrab(final_url,file_name,progress_obj=prog)

Updated this script on September 26th. Will work till Youtube changes its tactics again.