Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Created December 2, 2009 20:42
Show Gist options
  • Save bollwyvl/247553 to your computer and use it in GitHub Desktop.
Save bollwyvl/247553 to your computer and use it in GitHub Desktop.
import vlc, sys, time, win32gui, random
"""
from http://www.s-anand.net/blog/automating-powerpoint-with-python/
"""
class Treemap:
def __init__(self, width, height, data, draw):
'''Treemap(width, height, data, fn) '''
self.x, self.y = 0.0, 0.0
self.scale = (float(width * height) / self.get(data, sum)) ** 0.5
self.width = float(width) / self.scale
self.height = float(height) / self.scale
self.draw = draw
self.squarify(data, [], min(self.width, self.height))
def get(self, data, fn):
return fn((x[0] for x in data))
def layoutrow(self, row):
if self.width >= self.height:
dx = self.get(row, sum) / self.height
step = self.height / len(row)
for i,v in enumerate(row):
self.draw(self.scale * self.x, self.scale * (self.y + i * step), self.scale * dx, self.scale * step, v)
self.x += dx
self.width -= dx
else:
dy = self.get(row, sum) / self.width
step = self.width / len(row)
for i,v in enumerate(row):
self.draw(self.scale * (self.x + i * step), self.scale * self.y, self.scale * step, self.scale * dy, v)
self.y += dy
self.height -= dy
def aspect(self, row, w):
s = self.get(row, sum)
return max(w*w*self.get(row, max)/s/s, s*s/w/w/self.get(row, max))
def squarify(self, children, row, w):
if not children:
if row: self.layoutrow(row)
return
c = children[0]
if not row or self.aspect(row, w) > self.aspect(row + [c], w):
self.squarify(children[1:], row + [c], w)
else:
self.layoutrow(row)
self.squarify(children, [], min(self.height, self.width))
class Feed(object):
def __init__(self, vlc_instance, mrl):
self.media = vlc_instance.media_new(mrl)
self.player = vlc_instance.media_player_new()
self.player.set_media(self.media)
self.player.play()
time.sleep(.5) #TODO: get rid of this... do it event based?
self.window = win32gui.FindWindow("VLC DirectX", None)
win32gui.SetWindowText(self.window, mrl + " %s" % random.random())
def score(self):
return random.random()
def resize(self, x, y, w, h):
win32gui.MoveWindow(self.window, x, y, w, h, True)
def treemap_video(feeds):
data = [(x.score(), x) for x in feeds]
t = Treemap(1024, 768,
data,
lambda x, y, w, h, n: n[1].resize(x, y, w, h))
def init_feeds(mrl_list, vi=None):
if vi is None:
vi = vlc.Instance()
return map(lambda x: Feed(vi, x), mrl_list)
if __name__=="__main__":
feeds = init_feeds(sys.argv[1:])
while True:
time.sleep(1)
treemap_video(feeds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment