Filed under: Features, How-tos
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 "http://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
- This script was designed to work on Mac OS X Tiger / Leopard.
- 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

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
The Tuesday Night Tech Show said 8:18AM on 5-12-2008
The only downside to this is that you have to convert it at the end; there is a nice Firefox greasemonkey extension that you can install that gives you a new link that downloads the actual MP4 of the video instead; no conversion needed and better quality!
----------------------
Free iPhone-Only Ringtones
ringtoneyouriphone.blogspot.com
Reply
Macskeeball said 8:45AM on 5-12-2008
Indeed, and there is also a bookmarklet (Javascript stored as a bookmark, with the ability to act on the current page) available, for those who don't use Firefox. http://googlesystem.blogspot.com/2008/04/download-youtube-videos-as-mp4-files.html
Tom said 9:00AM on 5-12-2008
If you install perian (free) you can view FLV files through quicktime
Luigi193 said 8:25AM on 5-12-2008
I prefer just going + option + A and option clicking on the file directly...
Reply
Dinesh said 8:26AM on 5-12-2008
very nice!
Thanks! This will save me the trouble of going through another site to download the FLV!
Reply
Ryan Humphreys said 8:42AM on 5-12-2008
You can use a bookmarklet to download the MP4 directly from YouTube. Just create a bookmark with this as the URL:
javascript:if%20(document.getElementById('download-youtube-video')==null%20&&%20!!(document.location.href.match(/http:\/\/[a-zA-Z\.]*youtube\.com\/watch/)))%20{var%20yt_mp4_path='http://www.youtube.com/get_video?fmt=18&video_id='+swfArgs['video_id']+'&t='+swfArgs['t'];%20var%20div_embed=document.getElementById('watch-embed-div');div_embed.innerHTML=div_embed.innerHTML+'%3Cbr%20/%3E%20%3Cspan%20id=\'download-youtube-video\'%3E%3Ca%20href=\''+yt_mp4_path+'\'%3EDownload%20as%20MP4%3C/a%3E%20'+%20%20((navigator.userAgent.indexOf('Safari')!=-1)?'(control-click%20and%20select%20%3Ci%3EDownload%20linked%20file%20as%3C/i%3E)':('(right-click%20and%20select%20%3Ci%3ESave%20'+%20(navigator.appName=='Microsoft%20Internet%20Explorer'?'target':'link')%20+'%20as)%3C/i%3E'))+'%3C/span%3E';}void(0);
Then when you are on YouTube and want to download a video, click the bookmarklet and underneath the video info will appear a link saying Download as MP4. Then just Command-click on the link and choose Download Linked File As...
And there you have it, an iPod ready MP4 of the YouTube video. Hope this helps!
Reply
Queco Jones said 10:17AM on 5-12-2008
That works but, the file always ends up saved with a ".html" extension added to it (no matter what I name it)... any idea on how to avoid that?
TIA,
PS: Thanks! :)
Queco Jones said 8:21AM on 7-31-2008
@Ryan Humphreys: That works but, the file always ends up saved with a ".html" extension added to it (no matter what I name it)... any idea on how to avoid that?
TIA,
PS: Thanks! :)
Reply
Ryan Humphreys said 10:22AM on 5-12-2008
Just change the .html extension to .mp4 and that should work. :D
mark said 1:40PM on 5-12-2008
If you change the last "set" of the script to the following, you can automatically download .mp4 versions of the videos if they are available.
set downloadURL to "http://youtube.com/get_video?video_id=" & v_id & "&t=" & t_id & "&fmt=18"
Reply
Javier said 2:14PM on 5-18-2008
Can someone help me by teaching me how I can download the video from Tudou.com?
It is a China version of Youtube. It's got my fav astrology programs, I'm like dying to keep all of them!! =p
http://www.tudou.com/programs/view/G9AlwaVFR74/
Reply
Matthew Dunlop said 6:56AM on 5-21-2008
I tried the script a couple of times the other day (with a nice bit of Automater to automatically put it in an iTunes format using iSquint) but it now keeps bring up an error message and highlights this line---
set locationURL to do shell script "curl -I " & pageURL & " | grep ^Location | grep -o 'video_id=.*'"
Can someone help me here?
I tried it just normally in applescript but it does the same thing!
Help me please I want Youtube videos on mY iPod Touch offline!
Reply
youtube downloader said 11:37PM on 5-25-2008
Here is a great tutorial about how to download youtube videos fast and easily,http://www.ehow.com/video_2287260_handle-youtube-downloader.html,it's really worth reading.
Reply
download youtube said 12:51AM on 9-28-2008
i use this, i cant find better
http://www.indonesiaindonesia.com/downloadvideos/
Reply