i love browsing youtube and watching all different types of videos there. sometimes i stumble upon a video i want to watch later or have it stored locally on my mac. that's why i created a handy setup to automate the download and re-encode process. in this article i tell you how you can do the same.

first things first

before we can create our workflow, applescript keyboard shortcut, there a few tools we should install first:

  • homebrew, a nice packet manager)
  • youtube-dl to download videos
  • ffmpeg to transcode files
  • recommended: nodejs, osx-notifier*

*used for nice notifications in user interface

install homebrew and other tools

terminal icon

if you haven't already installed homebrew, please do so using terminal.app from your mac's utilities folder. head over to brew.sh and follow the one-step instruction to install it.

still in terminal, we are ready to install these all the other tools:

brew install youtube-dl ffmpeg node; npm install -g osx-notifier

depending on your computer's processing power, this will take some minutes. once the installation has completed feel free to close terminal.app.

scripting magic

we will use automator.app (from utilities folder) to create a new service. later-on we will use system preferences to bind a shortcut to this service.

now, open up automator.app and create a new service:

screenshot: automator, new service

from the actions panel on the left side, choose utilities - run applescript and drag it to the main area of the window.

screenshot: automator, run applescript

next, change service receives drop down to no input and paste this script into the run applescript text area:

on run {input, parameters}
	tell application "Safari" to set currentTab to current tab of window 1
	tell application "Safari" to set currentURL to URL of currentTab
	tell application "Safari" to set currentTitle to name of currentTab
	tell application "Finder" to set homeFolder to path to home folder
	do shell script "/usr/local/bin/node /usr/local/lib/node_modules/osx-notifier/bin/osx-notifier.js --type info --title \"Video Download\" --subtitle \"New video download started\" --message " & quoted form of currentTitle
	do shell script "/usr/local/bin/youtube-dl -o \"" & POSIX path of homeFolder & "Desktop/%(title)s.%(ext)s\" --ffmpeg-location /usr/local/bin/ffmpeg --recode-video mp4 " & currentURL
	do shell script "/usr/local/bin/node /usr/local/lib/node_modules/osx-notifier/bin/osx-notifier.js --type pass --title \"Video Download\" --subtitle \"Video download successful\" --message " & quoted form of currentTitle
	return input
end run

save this service with a neat name like download video using youtube-dl:

screenshot: automator, save service

binding service to keyboard shortcut

using the power of macos, we can simply create a custom keyboard shortcut to fire up the script service we just created.

screenshot: system preferences, keyboard shortcuts

inside preference pane keyboard, there is a shortcuts tab. inside this tab, choose the services section and scroll down to find the script named download video using youtube-dl.

screenshot: system preferences, keyboard, shortcuts

activate the checkbox for this script and on the same line add your preferred shortcut; i chose [ctrl] + [option] + [cmd] + [D].

screenshot: system preferences, add shortcut