ytplay – wrapper for youtube-dl.py

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!

#!/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) + " &")


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>