ytplay – wrapper for

Due to my slightly outdated computer flash is a huge problem. So, for viewing youtube videos, is a great solution. However, I wanted something more comfortable.

ytplay wraps around, 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!

# -*- 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]
	searchargs = '+'.join(args)
	searchurl = "" + quote_plus(searchargs)
	searchresult = urlopen(searchurl).read()
	if p==None:
		print "No Videos found"
	videopageurl="" +

print "Starting youtube-dl for " + videopageurl
ytdl = Popen(" -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 + " " +
		system(opts.player + " " + + " &")

filetype ytplay (1.28 kiB, 2010-02-14)

This entry was posted in Computers, Internet and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>