It can indeed! So much so that you can chop out the middle part of your script!
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?
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
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.
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.
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.
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