Downloading YouTube movie clips to your local hard drive

I tried to watch this YouTube clip the other day but found that my flash player had a hard time synching the audio and video. Very annoying! And as far as I can see YouTube doesn’t provide a download link to let me download the movie and play it directly from my hard drive. I really wanted to see this clip but couldn’t stand the audio lagging behind. I figured there must be a way to hack that flash thing they are using to play the movie. It turned out to be quite easy!

First I downloaded the flash animation embedded in the page. You’ll find the URL of the SWF file in the text field labelled Embed to the right of the movie or you could have a look at the page’s HTML code. Then I used wget to download this URL:

wget 'http://www.youtube.com/v/3CiW838wNiM'

Watching the output from wget one can see that this URL actually does a redirect to another URL:

Location: /p.swf?video_id=3CiW838wNiM&eurl=&iurl=http%3A//sjl-static5.sjl.youtube.com/vi/3CiW838wNiM/2.jpg
&t=OEgsToPDskJAsFn4X26z0PYTiD7rKU DG

After wget downloaded the p.swf file (this is a flash animation file) I used flare to decompile it and had a look at the ActionScript code. The code had a number of URL strings and after reading Macromedia’s docs on the NetStream class I knew I had found the URL I was looking for. Here’s how I downloaded the movie above which I wanted to watch:

wget 'http://www.youtube.com/get_video?video_id=3CiW838wNiM&t=OEgsToPDskKC-YjD-y9Yr3q63ZzBUd1A'

The t parameter comes from the redirect URL we got as a response to the first wget. This appears to be some kind of ticket or something and seems to change over time.

After executing this wget command I had an FLV movie file on my hard drive which I could watch using mplayer without any audio problems!

2 thoughts on “Downloading YouTube movie clips to your local hard drive”

Comments are closed.