Due to my slightly outdated computer flash is a huge problem. So, for viewing youtube videos, youtube-dl.py is a great solution. However, I wanted something more comfortable.
ytplay wraps around youtube-dl.py, it searches for a video, orders youtube-dl to get it, and starts playing it in vlc.
Usage is simple:
> ytplay foobar
will search youtube for videos about foobar and play the first one found.
The code is still quite dirty, especially the subprocess part. However, it should work, have fun!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/usr/bin/python # -*- coding: utf-8 -*- # parser.add_option('-P', '--play', action='store_true', # dest='play', help='play in vlc', default=False) from optparse import OptionParser from subprocess import Popen, PIPE from urllib2 import urlopen from urllib import quote_plus from re import search, match from os import system parser = OptionParser(version='2010.02.13') parser.add_option('-p', '--player', dest='player', metavar='PROGRAM', help='media player to use', default='vlc') (opts, args) = parser.parse_args() if match("http://",args[0]): videopageurl = args[0] else: searchargs = '+'.join(args) searchurl = "http://www.youtube.com/results?search_type=&aq=f&oq=&search_query=" + quote_plus(searchargs) searchresult = urlopen(searchurl).read() p=search(r'(watch\?[^"]*)',searchresult) if p==None: print "No Videos found" exit() videopageurl="http://www.youtube.com/" + p.group(1) print "Starting youtube-dl for " + videopageurl ytdl = Popen("youtube-dl.py -t -b " + videopageurl,shell=True,stdout=PIPE) while not ytdl.stdout.closed: line = ytdl.stdout.readline() print line, if line == '': exit() p = search(r'\[download\] Destination: (\S*)',line) if p != None: print "Starting player: " + opts.player + " " + p.group(1) system(opts.player + " " + p.group(1) + " &") |