Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download Video Mac Service
--set theURL to getURLFromSafari()
--set theURL to getURLFromFirefox()
--set theURL to getURLFromChrome()
set theURL to getURLFromChromium()
set format to "((bestvideo[height>1080][vcodec!^=av01]/bestvideo[vcodec^=avc1])+bestaudio[ext=m4a])/best"
--set format to "bestvideo[vcodec^=avc1]+bestaudio[ext=m4a]/best"
set outputLocation to "~/Movies"
set maximumVideos to 5
checkForYoutubeDL()
checkForFFmpeg()
do shell script "/usr/local/bin/youtube-dl --update"
try
set theName to do shell script "/usr/local/bin/youtube-dl \"" & theURL & "\" --ignore-errors --no-check-certificate --no-playlist --playlist-end 1 --get-title"
do shell script "/usr/local/bin/youtube-dl \"" & theURL & "\" -f \"" & format & "\" --ignore-errors --no-check-certificate --ffmpeg-location /usr/local/bin/ --embed-subs --add-metadata --merge-output-format mp4 --no-playlist --playlist-end " & maximumVideos & " -o '" & outputLocation & "/%(title)s.%(ext)s'"
display notification theName with title "Download Complete!" sound name "Hero"
on error errorMessage
if errorMessage contains "Unsupported URL" or errorMessage contains "Could not send HEAD request" then
display dialog "No downloadable videos found on web page." with icon stop buttons {"OK"}
error
else if errorMessage contains "geolocation" then
display notification "This video contains geolocation restrictions. Connect to a VPN to mask your location." with title "Connect to VPN to download " & theName
error
else if errorMessage contains "too many requests" then
display dialog "Youtube has banned your IP address. Try again later." with icon stop buttons {"OK"}
error
else if errorMessage contains "SSL" then
display dialog "There was an SSL error. If you're running an old version of Mac OS, an HTTPS proxy like Squid can usually fix this problem." with icon stop buttons {"OK"}
error
end if
display dialog errorMessage with title "Unable to download video!"
error
end try
(*on getURLFromSafari()
try
tell application "Safari"
set theURL to URL of front document
--If it's possible to go back, then go back. Otherwise, close the tab instead.
if (do JavaScript "history.length" in front document) > 1 then
do JavaScript "history.back()" in front document
else
close current tab of front window
end if
return theURL
end tell
on error
do shell script "defaults write com.apple.Safari IncludeDevelopMenu -bool true"
display dialog "In Safari, please enable Developer → Allow Javascript from Apple Events" buttons {"OK"}
error
end try
end getURLFromSafari*)
(*on getURLFromFirefox()
try
set oldClipboard to the clipboard
tell application "Firefox"
activate
delay 0.2
tell application "System Events"
keystroke "l" using command down
delay 0.3
keystroke "c" using command down
delay 0.3
key code 53 --escape key
delay 0.3
keystroke "[" using command down
end tell
end tell
set theURL to (the clipboard) as text
set the clipboard to oldClipboard
return theURL
on error
display dialog "Please open \"System Preferences\" → \"Security & Privacy\" → \"Privacy\" → \"Accessibility\" and add \"Firefox.app\" to the list of apps allowed to control your computer."
error
end try
end getURLFromFirefox*)
(*on getURLFromChrome()
tell application "Google Chrome" to tell front window
try
set currTabIndex to active tab index
on error
display dialog "No tabs found. Is Google Chrome open?"
error -128
end try
tell tab currTabIndex
set currURL to URL
go back
if URL is currURL then
--Going back was not possible. Close the current tab instead.
close
end if
return currURL
end tell
end tell
end getURLFromChrome*)
on getURLFromChromium()
tell application "Chromium" to tell front window
try
set currTabIndex to active tab index
on error
display dialog "No tabs found. Is Google Chrome open?"
error -128
end try
tell tab currTabIndex
set currURL to URL
go back
if URL is currURL then
--Going back was not possible. Close the current tab instead.
close
end if
return currURL
end tell
end tell
end getURLFromChromium
on checkForYoutubeDL()
try
do shell script "/usr/local/bin/youtube-dl -h"
on error
display dialog "This service needs to install youtube-dl to continue. A copy of youtube-dl will be downloaded and placed in /usr/local/bin/." with title "Install youtube-dl?"
do shell script "mkdir -p /usr/local/bin/ && curl -L http://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl && chmod +x /usr/local/bin/youtube-dl" with administrator privileges
end try
end checkForYoutubeDL
on checkForFFmpeg()
try
do shell script "/usr/local/bin/ffmpeg -h"
on error
display dialog "This service needs to install ffmpeg to continue. A copy of ffmpeg will be downloaded and placed in /usr/local/bin/." with title "Install ffmpeg?"
do shell script "mkdir -p /usr/local/bin/ && curl -JL http://evermeet.cx/ffmpeg/getrelease/zip | tar --directory /usr/local/bin/ -xvf- -o ffmpeg && chmod +x /usr/local/bin/ffmpeg" with administrator privileges
end try
end checkForFFmpeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment