Updated 2014-01-21 21:47:25 by AMG

Richard Suchenwirth 2013-09-04 - YouTube is a popular provider of streaming videos. With a tool like youtube-dl we can also download such a video to a file, just in case internet connection is slow, or a video might disappear in the future.

Operation is easy enough:
 1. right-click the YT title, select "Copy link target" from menu
 2. in a terminal, type: youtube-dl -o "%(stitle)s.%(ext)s"
 3. after that, paste the URL into the terminal, Return

However, I wanted it even easier. The following little script brings up a text window, and checks the clipboard every second. If its contents look like a YouTube URL, it does steps 2. and 3., and logs the video title in its window. The output of youtube-dl is logged in the terminal (on my Lubuntu system at least), so you can see the details.
 #!/usr/bin/env tclsh
 package require Tk 8.5

 proc main argv {
    global g
    array set g {urls {} text .t}
    pack [text $g(text) -wrap word] -fill both -expand 1
    $g(text) insert end "Youtube downloads from clipboard:\n\n"

    every 1000 check_clipboard
 proc every {ms body} {eval $body; after $ms [info level 0]}

 proc check_clipboard {} {
    global g
    if [catch {selection get} cb] return
    if [regexp youtube.com/watch $cb] {
        if {$cb ne "" && $cb ni $g(urls)} {
            set title [exec youtube-dl -e -o %(title)s.%(ext)s $cb]
            $g(text) insert end $title\n
            $g(text) see end
            exec youtube-dl  -o %(title)s.%(ext)s $cb &
            lappend g(urls) $cb

 main $argv

Update: in the version that came with Ubuntu 13.10, youtube-dl changed the variable %(stitle)s to %(title)s. Works now on my machine, for yourself, test which variant is better...

AMG: See also: http://www.jwz.org/hacks/#youtubedown (written in Perl)