Download from YouTube

i need an applescript to download videos from youtube, i can use wget. but if i have the user insert the youtube video url into the script, how would i download it?

First off, how do you download them using wget? Would you be able to provide an example or two?

i dont know, thats what i’m asking. i’m saying is there a way i can use wget to download them?

use google to find out things you don’t know.

“wget” “youtube”

cheers.

CAVEAT: i have NO idea if this will work. i can’t test because i am at work and youtube is off limits (firewalled). however, i read one of the links above and thought this might be a good start:

set youTubeScript to "#!/bin/bash
wget -c -S -O $2.flv [url=http://youtube.com/get_video.php\\?curl]http://youtube.com/get_video.php\\?`curl[/url] -s $1 | grep player2.swf | cut -f2 -d? | cut -f1 -d\"` && ffmpeg -i $2.flv -ab 56 -ar 22050 -b 500 -s 320x240 $2.mpg && exit 0"

set ytUrl to (display dialog "What is the URL of the video you want to download?" default answer "")

set myFile to (display dialog "What name would you like to save the file as?" default answer "")

do shell script youTubeScript & space & ytUrl & space & myFile

display dialog "Your file " & myFile & " is ready to view!"

EDITED to add: Duh! sorry folks, to use the above script you’ll have to download and install ‘ffmpeg’. here are some instructions for doing so:

http://stephenjungels.com/jungels.net/articles/ffmpeg-howto.html

cheers.

YouTube seem to have made it intentionally difficult to download their videos.
waltr, I’ve tried the method that you use, and unfortunately it doesn’t work. I discovered from this page that extra stuff (more than just the video id number) needs to be included in the url to stop YouTube.com pretending that the video doesn’t exist.

Anyway, give this script a try:

set pageURL to text returned of (display dialog "Enter the URL of the YouTube video's page:" default answer "")
set destinationPath to POSIX path of (choose file name with prompt "YouTube flash video file" default name "video.flv")

set url2 to "http://www.youtube.com/v/" & last word of pageURL
set locationURL to do shell script "curl -I " & url2 & " | grep ^Location | grep -o 'video_id=.*'"
set downloadURL to "http://youtube.com/get_video?" & locationURL

do shell script "curl -o " & destinationPath & space & quoted form of downloadURL

This uses curl to download the video. To make it so that your default browser downloads it instead, change the last line to:

open location downloadURL

To play the resulting file, I can recommend MPlayer. VLC seems to crash.

wierd both of those methods dont work.

Hmm, it did for me. Could you give me an example page link?

http://www.youtube.com/watch?v=B8H29jU8Wrs

@Qwerty - “set url2 to “http://www.youtube.com/v/” & last word of pageURL”

I think this line of code could have some potential errors if the last word contains " - ". I tried using your script on a video that had a dash, and it only gave me part of the word.

I came up with something like this.


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

However this is still pretty naive since it fails to save the file as a .flv, and doesn’t allow the user to set a filename. I couldn’t get any of the previous scripts to work using curl, maybe someone can add to this to make it a bit more functional. I hope that helps.

  • Jose

Thanks jofalcon for picking that out! I’ve also realised I forgot a ‘text returned of’ back there. :confused:
This version should now overcome that hyphen problem, whilst simultaneously accepting the video id just by itself too:

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

tell pageURL to set url2 to "http://www.youtube.com/v/" & text ((my (offset of "=" in it)) + 1) thru -1
set locationURL to do shell script "curl -I " & url2 & " | grep ^Location | grep -o 'video_id=.*'"
set downloadURL to "http://youtube.com/get_video?" & locationURL

open location downloadURL

As for saving with a different name, my curl version did work for me, but I did also try using the URL Access Scripting app, but it kept coming up with a -303 error. So, I don’t know what I’m doing wrong there. :confused:

Anyway, I’ve tested the above and it works for me, so here’s hoping it does for you too. :slight_smile:

Qwerty -

Thanks for the update script. I didn’t realize you could just that first link to download from. I read online that you need a “video_id” and “t_id”, so I worked to pull those out individually. I tested the script on a couple of videos with dash’s in them, and it worked perfectly.

Maybe we could work on getting this to save as a .flv, now.

Great script!

these all work fine using open location, but using wget i get the 404 error, why doesnt it work, i need to use wget for this unless theres another thing i can use to download it…

Working with Qwerty Denzel’s Script.
I have used Curl.

Remember with urls like these there are redirects. the -L option in curl can handle them.


-- Copy the Url to the youtube into the clipboard and then run the script.
set pageURL to the clipboard --<

--Or if you want a dialogue asking for the url , comment out the above line ( --< )
--and  remove the comments ( --> ) from the line below.

-->set pageURL to text returned of (display dialog "Enter the URL of the YouTube video's page:" default answer "")
tell pageURL to set url2 to "http://www.youtube.com/v/" & text ((my (offset of "=" in it)) + 1) thru -1
set locationURL to do shell script "curl -I " & url2 & " | grep ^Location | grep -o 'video_id=.*'"
set downloadURL to "http://youtube.com/get_video?" & locationURL

set gtheUrl to do shell script "curl -IL " & quoted form of downloadURL & "| grep ^Location "
set oldDelims to AppleScript's text item delimiters -- get normal dilm
set AppleScript's text item delimiters to {"Location: "} --sets new dilm
set line_start to text item -1 of gtheUrl as string ---

set AppleScript's text item delimiters to oldDelims -- resets dilm

do shell script "curl " & line_start & " -o ~/documents/Youtube_1.flv "  -- downloads the file, may take some time.
display dialog "Your Tube has downloaded"

Nice, Mark (and QD);

I’ve added this to yours, Mark;

set pageURL to the clipboard

Then, all you have to do is copy the url of the page and go

And; I’ve found that they play quite nicely in VLC.

thanks guys it worked!

You can convert them to AVI, MPG or whatever using VisualHub

Good idea, I have edited the script to show this

But I’ve never seen one I wanted to keep. :wink:
I was more interested in Mark’s process than the result.

i cant open the flv movies and i have macromedia flash, flashplayer and vlc