(or emacs irrelevant)

Yet another youtube-dl interface for Emacs

If you haven't been living under a rock, you already know what Youtube is. It's a repository with videos of varying degree of usefulness with a terrible media player tacked on. Instead, I like to watch my videos in VLC, which comes closest to providing an Emacs-like experience among video players.

Useful VLC shortcuts

Here is a list of shortcuts that really make me stick with VLC:

  • f - toggle full-screen
  • b - toggle audio track
  • n - toggle subtitle track
  • ] - speed up play by 0.1
  • [ - slow down play by 0.1
  • M-right - forward by 15 seconds
  • M-left - backward by 15 seconds
  • C-right - forward by 60 seconds
  • C-left - backward by 60 seconds
  • M-1 - quarter of video size
  • M-2 - half of video size
  • M-3 - full video size
  • M-4 - double video size
  • C-h - toggle mouse buttons

So if you're not watching instructional videos at 1.6 speed, or skipping the Simpsons intro sequence with M-right, you're missing out.

From Youtube to VLC

youtube-dl is an excellent command-line tool for saving the videos from Youtube. It downloads the highest resolution at a usually higher speed than Youtube's player buffers. I've discovered it when I had to download a bunch of lecture videos from edX. You can install it with:

sudo pip install youtube-dl

One Emacs script to rule them all

I quickly tired of opening a shell, setting the directory, entering the command, and pasting the link. So I wrote some Elisp code that does it for me. It's nothing too sophisticated, but I've been using this version for a couple months:

(defun youtube-dl ()
  (interactive)
  (let* ((str (current-kill 0))
         (default-directory "~/Downloads")
         (proc (get-buffer-process (ansi-term "/bin/bash"))))
    (term-send-string
     proc
     (concat "cd ~/Downloads && youtube-dl " str "\n"))))

How it works:

  1. Copy the link in Firefox
  2. M-x youtube-dl.

That's it. A new *ansi-term* will open with the task of downloading the video from the link in the clipboard to ~/Downloads. I don't have to wait for the download to finish and can immediately open the video from dired. See the previous post for the description of dired process-starting setup. I can stack up multiple downloads at once if I wish in different *ansi-term*s.

This is my script. There are many like it, but this one is mine.

I did an internet search before writing this post. Apparently many others had the same idea of integrating youtube-dl into Emacs. You can use mine or any other code to generate a setup that works for you. For instance, for a while, instead of copy-pasting the URL and calling youtube-dl I used to just click the org-mode capture button in Firefox, and it would automatically create a TODO item, download the video, and put the link to the downloaded video in the TODO.

I've dropped this workflow when the yank bug surfaced. I don't yet have enough experience of working with Emacs's C code to fix it. Although, according to this excellent rant, fixing the bug is only half of the problem: getting it merged is hard. I'll see how it goes with my latest tiny patch. So far it has been ignored, but it is only two days old as of now.