Version 1 of Playing YouTube Videos

Updated 2018-01-21 22:08:52 by CecilWesterhof

Created by CecilWesterhof.

I created a little script to play YouTube videos with mpv. There were several reasons for this:

  • When playing in my browser it takes a lot more resources as when playing with mpv.
  • I want to play the videos in a higher speed.
  • I prefer the interface of mpv.

At this moment it is a command line script, but I want to make a Tk version also. This will add at least the following functionalities:

  • The value of the URL will be fetched from the clipboard.
  • Starting is done by the keys 1 to 0. Each having a different speed which will be fetched from a SQLite database. (Because different types of videos need different speeds.)

Any ideas for extra functionalities are welcome.

The code:

#!/usr/bin/env tclsh


proc getInput {prompt} {
    if {(${prompt} ne "") && ([string index ${prompt} end] ne " ")} {
        set prompt "${prompt}: "
    }
    puts  -nonewline ${prompt}
    flush stdout
    gets  stdin
}


while {True} {
    set URL [getInput "Enter YouTube URL (#q to exit): "]
    if {${URL} == "#q"} {
        break
    } else {
        if {[regexp {^https://www.youtube.com/watch\?v=(.){11}$} ${URL}]} {
            exec mpv --speed 1.5 ${URL} >&/dev/null
        } else {
            puts "Not a valid YouTube URL: ${URL}"
        }
    }
}

As always: comments, tips and questions are appreciated.