Advertisement

AppleScript: download YouTube videos

Keeping with the "self-built apps," I'm going to show you how to build your own YouTube downloader. Sure you could use applications like TubeSock, but where's the fun in that? This application will download the YouTube flash video from a pasted link, and all with a little AppleScript.


Continue reading to learn how to build the YouTube downloader.




Building the script

Just copy the following script into the Script Editor.app (/Applications/AppleScript/Script Editor.app):

set pageURL to display dialog "Enter the URL of the YouTube video's page:" default answer ""

set AppleScript's text item delimiters to "?v="
set v_id to item 2 of text items of text returned of pageURL
set pageURL to "https://www.youtube.com/v/" & v_id

set locationURL to do shell script "curl -I " & pageURL & " | grep ^Location | grep -o 'video_id=.*'"
set AppleScript's text item delimiters to "&t="
set t_id to item 2 of text items of locationURL

set downloadURL to "http://youtube.com/get_video?video_id=" & v_id & "&t=" & t_id

open location downloadURL



Saving the script
Let's save this AppleScript as an AppleScript application. To do this, just click File > Save. Type in a name for your appliction and choose "Application Bundle" from the "Format" drop-down box.



Running the script
To run the YouTube downloader, double-click on the AppleScript application that you just created. When the script starts, it will display a dialog asking you for a YouTube URL (this is the URL that you copy from the address bar, not from the YouTube embed). Paste the URL and click "OK." The application will then start running and run a couple background processes -- in about 1-2 minutes (depending on internet connection / Mac speed) your default browser will begin downloading a file named "get_video.flv."

Once the download has finished, you can convert the flash video into something that you can look at natively. I recommend a free application called iSquint -- you can drop the flash video onto the iSquint app and convert it into numerous formats including iPod and Apple TV.

Additional notes

  1. This script was designed to work on Mac OS X Tiger / Leopard.

  2. While this script was designed and tested to work with Safari 3.0 and Firefox, it can be used with other browsers; however, if you are experiencing difficulties with your default browser, try Safari or Firefox