Download from YouTube

It can indeed! So much so that you can chop out the middle part of your script! :slight_smile: :wink:

set pageURL to the clipboard
tell pageURL to set url2 to "http://www.youtube.com/v/" & text ((my (offset of "=" in it)) + 1) thru -1
try
	set downloadURL to do shell script "curl -I " & url2 & " | sed -e '/^Location:/!d' -e 's|^.*?|http://youtube.com/get_video?|'"
on error
	display alert "YouTube Video Downloader error" message "The URL entered \"" & pageURL & "\" was not valid."
	return
end try

set destinationPath to quoted form of POSIX path of (choose file name with prompt "YouTube flash video file" default name "video.flv")
do shell script "curl -L " & quoted form of downloadURL & " -o " & destinationPath

hendo, VLC only got support for .flv files recently. Have you got the latest version?

Good input everyone. :cool:

Here’s my take on Qwerty’s last post:

try
	if (the clipboard) starts with "http://" then
		get the clipboard
	else
		display dialog "Enter a YouTube URL:" default answer "http://youtube.com/watch?v="
		text returned of result
	end if
	
	tell result to set downloadURL to "http://www.youtube.com/v/" & text ((my (offset of "=" in it)) + 1) thru -1
	tell result to if it contains "&" then set downloadURL to text 1 thru ((my (offset of "&" in it)) - 1)
	
	choose file name with prompt "Save YouTube flash video file as:" default name "untitled.flv"
	set outputFile to result as Unicode text
	if (text -4 through -1 of result) is not ".flv" then ¬
		set outputFile to result & ".flv"
	
	do shell script "/usr/bin/curl -I " & (quoted form of downloadURL) & " | /usr/bin/sed -e '/^Location:/!d' -e 's|^.*?|http://youtube.com/get_video?|'"
	do shell script "/usr/bin/curl -L -o " & quoted form of POSIX path of outputFile & " " & quoted form of result
	
	-- This line no longer works
	-- do shell script "/usr/bin/curl -I " & (quoted form of downloadURL) & " | /usr/bin/sed -e '/^Location:/!d' -e 's|^.*?|http://youtube.com/get_video?|' | /usr/bin/xargs /usr/bin/curl -L -o " & quoted form of POSIX path of outputFile
	
	display dialog "Script finished!" buttons {"Reveal Video File", "OK"} default button 2
	
	if (button returned of result) is "Reveal Video File" then
		tell application "Finder"
			activate
			reveal outputFile
		end tell
	end if
on error errMsg number errNum
	if errNum is not -128 then ¬
		display alert ("Error " & errNum) message errMsg buttons {"Cancel"} default button 1
	
	error number -128 -- Cancel
end try

For those not using OS X v10.4+, here’s some alternate code for error handling:

on error errMsg number errNum
	if errNum is -128 then error number -128 -- Cancel
	
	display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel"} default button 1
end try

Hi Bruce,

Mine and Qwerty’s Scripts still work, But only play in VLC, and not my QuickTime.

That maybe an issue with my QT ??

Your script does not work anymore, But to be honest, I do not remember testing it.

I removed my previous post as this was just a problem with my script, which I’ve fixed (see the edited script above).

Check out Perian.

Like Mark mentioned (much) earlier, the 3xx class of HTTP status codes are for redirection.

I noticed this little gem while playing with youtube : [b]http://cache.googlevideo.com/get_video?video_id=[/b]
Which if you just stick the id on the end it diverts only once to the file. So no need to use -I in curl first or sed

try
	if (the clipboard) starts with "http://" then
		get the clipboard
	else
		display dialog "Enter a YouTube URL:" default answer "http://youtube.com/watch?v="
		text returned of result
	end if
	
	tell result to set downloadURL to "http://cache.googlevideo.com/get_video?video_id=" & text ((my (offset of "=" in it)) + 1) thru -1
	tell result to if it contains "&" then set downloadURL to text 1 thru ((my (offset of "&" in it)) - 1)
	
	choose file name with prompt "Save YouTube flash video file as:" default name "untitled.flv"
	set outputFile to result as Unicode text
	if (text -4 through -1 of result) is not ".flv" then ¬
		set outputFile to result & ".flv"
	
	do shell script "curl -L " & quoted form of downloadURL & " -o " & POSIX path of outputFile
	
	display dialog "Script finished!" buttons {"Reveal Video File", "OK"} default button 2
	
	if (button returned of result) is "Reveal Video File" then
		tell application "Finder"
			activate
			reveal outputFile
		end tell
	end if
on error errMsg number errNum
	if errNum is not -128 then ¬
		display alert ("Error " & errNum) message errMsg buttons {"Cancel"} default button 1
	
	error number -128 -- Cancel
end try

Thanks, Its odd, I did have Perian, but no more. I suspect I killed it when experimenting on something and forgot to put it back.

I notice that its now a prefpane before I just had it as a component.

I had seen Google in there for something else, but not for this. Thanks for point that out.

Also, I’ve changed the beginning of my script slightly:

if (the clipboard) starts with "http://" and (the clipboard) contains "youtube.com/" then

That was added with the 1.0 release.

Hi everyone…

Can anyone else verify if this approach to downloading from youtube still works or if they changed something?

When I try this script, it looks as though it’s working-- and it creates a “untitled.flv” file, but it says “script finished” within about a second after giving it a valid url… The untitled.flv file ends up being 1.3kb and is blank.

Thanks.

-patrick

Yes the http://cache.googlevideo.com/get_video?video_id= one is now broken,

But the curl script still works, Use one of them,

Awesome. Thank you!

-patrick

unfortunatly this no longer works. is it just me, or did youtube change something?

This script doesn’t completely work. it wont download from youtube.

anyone?

from Mac OS X Hints
Download 720p high definition videos from YouTube…
http://www.macosxhints.com/article.php?story=20081125213451807

Tom

wow, thats awesome.

but is there a way in applescript to download regular quality videos from youtube, because the script in this thread no longer works.

thanks.

anyone?

sorry… no idea…

bump

Would not say this was a quick fix, It works… I did not set a size fomate, but this can be done very easily with the fmt tag. The script can most likely be pared down using a bit more shell… But I have got to get some sleeep. :slight_smile:

try
	
	display dialog "Enter a YouTube URL:" default answer "http://youtube.com/watch?v="
	text returned of result
	set text_returned to result
	
	tell result to set downloadURL to "http://www.youtube.com/get_video?video_id=" & text ((my (offset of "=" in it)) + 1) thru -1
	tell result to if it contains "&" then set downloadURL to text 1 thru ((my (offset of "&" in it)) - 1)
	set MaindownloadURL to do shell script "Curl -L " & text_returned & "| grep '\"t\":' "
	set oldDelims to AppleScript's text item delimiters -- get normal dilm
	set AppleScript's text item delimiters to {"\"t\": \""} --sets new dilm
	
	set vid_t_name to text item -1 of MaindownloadURL as string ---
	set AppleScript's text item delimiters to {"\","}
	set vid_t_name2 to text item 1 of vid_t_name as string
	set AppleScript's text item delimiters to oldDelims -- resets dilm
	set downloadURL to downloadURL & "&t=" & vid_t_name2 as string
	choose file name with prompt "Save YouTube flash video file as:" default name "untitled.flv"
	set outputFile to result as Unicode text
	if (text -4 through -1 of result) is not ".flv" then ¬
		set outputFile to result & ".flv"
	do shell script "curl -L " & quoted form of downloadURL & " -o " & POSIX path of outputFile
	
	display dialog "Script finished!" buttons {"Reveal Video File", "OK"} default button 2
	
	if (button returned of result) is "Reveal Video File" then
		tell application "Finder"
			activate
			reveal outputFile
		end tell
		
	end if
	
on error errMsg number errNum
	if errNum is not -128 then ¬
		display alert ("Error " & errNum) message errMsg buttons {"Cancel"} default button 1
	
	error number -128 -- Cancel
end try

This script works fine with a lot of movies but sometimes it will crash

for example with http://www.youtube.com/watch?v=vHr4SpsLrMk&feature=related

any chance of checking why not every movie is downloadable ?

Kemalski

seems like the new youtube layout breaks this. does anyone have a fix?