Automator-actions-for-youtube-dl

Youtube-dl Is Lovely

Youtube-dl is a lovely tool for downloading and extracting videos off of the internet… which of course is bad… shame on you!

You can also install it using homebrew.

But yeah, it’s a marvelous tool, because it is supported on dozens of sites and offers options for processing the media, say you only want the mp3 of some song:

  
    $ youtube-dl --extract-audio --audio-format mp3 $url
  

Where $url is the url of the target video.

(Now this is particularly lovely if you’re dealing with something like Soundcloud that has very long files which you can’t really listen to offline. Well maybe premium users can now? I still don’t want to install their crappy application. So anyway, you can just run this here in your terminal on a couple of podcasts and drag it to your phone, or just drag-drop via bluetooth (probably not too fast that way, though). Viola, you’ve got a couple hours of stuff to listen to.)

Anyway, even if you don’t need this for that kind of use case it’s lovely and a great alternative to using one of those weird, random, insecure browser plugins (which tend to not offer audio extraction or file conversion in their free versions). And you can still use it in your browser if you use OSX with a little brainpower.

I’ll show you how…

Automator Action

Automator, as I am always telling people, is lovely. There’s not much I love these days about osx, but the ability to add services and workflows and all that… folder actions and what have you. The problem is that most programmers don’t really need the GUI and can do their scripting from the shell if they need to so this doesn’t really trickle down to end users.

Anyway heres how to add a youtube-dl action so you can right click on the url and download it, and move the file to your ~/Music folder.

  1. Open automator - (Type COMMAND + SPACE then automator.)

  2. Choose service.

  3. Select from actions (type in the search bar) “Run Shell Script”

  4. Paste this in if you use homebrew:

  
    export PATH=/usr/local/bin:$PATH
    for f; do
    youtube-dl --extract-audio --audio-format mp3 $f
    done
    mv *.mp3 ~/Downloads #or wherever you like.  
  

If you installed youtube-dl manually you can locate the installation directory with “which youtube-dl” in the terminal and add that before $PATH in the export command above. Since Automator uses the system shell (/bin/bash by default) and /etc/paths (which SIP doesn’t let you mess with since El Captain) that’s probably the best way to do it.

If you set your paths up in your dotfiles you can also just have the shell read your dotfiles e.g.:


    source .bash_profile

You can do without the mv command or change it to send to ~/Downloads or some other appropriate folder. Rsync is useful if you want to send over a newtork (and is postentially safer if you have duplicates) and “open” on OSX will open files using itunes by default, and depending on your itunes settings may appropriately move the file for you (probably a good option if you use iTunes to manage mp3s on your device).

  1. Save the file as whatever you’d like to name this service.

  2. Final Step: Press Command + Space, type “keyboard” select the “shortcuts” tab, select the services icon, and click the check box for the automator action which you have made.

Viola! That’s it. Now you can right click (opt+click, two-finger-click) on the urls now while on Soundcloud/Youtube and at other places, select your service, and yoink your media instantly.

NOTE:

You can optionally do without the mv command, use rsync with the proper options instead (it’s potentially less destructive and can send things to a network location if you prefer, or to a server via ssh). You could also open the file with the open commmand, importing it into itunes on a mac with the default settings, or specify a workflow, such as attaching the file and emailing it to yourself.

I will add some pictures to illustrate this process when I add an update on how to do that.