A recent comment on BoingBoing asked if there was a way to download a video from youtube, such that it could be reposted elsewhere. One solution, suggested by Cory Doctorow, is to use pwnyoutube.com, but there exists a general method that works on all flash video (not just youtube), and happens to be faster than using pwnyoutube.com. Behold! For I shall demonstrate a painless use of lsof, the under-appreciated and extra-useful command line tool.

In the Internet video world, there are two kinds of creatures: streaming video (which is appropriate for live events) and buffered video (which is for recorded things, like youtube). “Buffering” means it’s actually downloading a file in the background, and if it can download a little faster than you can watch it, then everything plays smoothly. If the video pauses suddenly and restarts after a few seconds, that’s because it’s rebuffering.

youtube buffering

Have you ever noticed how the youtube progress bar slowly fills in with a pinkish color? It’s at about 20% in the picture, above. That indicates how much of the file has been buffered, and when it reaches the end, it means the file is fully downloaded. In other words, you don’t need some special plugin or service to “download a video from youtube.” Your browser does it automatically! Even better, this happens for any website that uses flash and .flv files to deliver buffered video.

The key is to use lsof (which is a mnemonic for “list open files”). I’m demonstrating this on OS X, but the process is basically the same with *nixes and Cygwin. If you don’t have lsof installed by default, just use your package manager to install it. (e.g. apt-get install lsof).

So, the magical incantation is:

lsof |grep lash

I grep for “lash” instead of “Flash” since you never know if the F will be capitalized or not, and this is the laziest way to get the desired results. Here is an example of the output:

lsof example

Notice the files FlashTmp0 and FlashTmp1? That’s where the video files are saved, so long as you keep your browser window and video tabs open. There’s no need to “download” a video that you just watched. Instead, simply copy the file straight to your Desktop:

cp /private/var/folders/.../TemporaryItems/FlashTmp1 ~/Desktop/rickroll.flv

Now, you can open the local file with VLC:

VLC example

You might need to try multiple FlashTmp files before you find the one containing the video you want (i.e. is it FlashTmp0 or FlashTmp1) but there usually aren’t many. On many non-youtube sites, this is the only way you’re going to get access to a buffered flash video (since there aren’t handy pwnyoutube.com clones for everything).

Once you have copied the file to your desktop, why not convert it to mp4 and edit it in iMovie?

ffmpeg -i rickroll.flv rickroll.mp4

ffmpeg example

And now you know how to access videos you just watched, as well as convert them into a format you can edit.