oxdbarchive/oxdbarchive/timeline.py
2008-06-19 12:14:38 +02:00

109 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=2:sts=2:ts=2
import Image
import math
from StringIO import StringIO
from subtitles import time2ms
import oxdb_cache
lineWidth = 600
timlelineHeight = 16
rowHeight = timlelineHeight + 2 * 4
'''
returns timeline view as a png image for a given movie.
'''
def loadTimeline(movie, lines = -1):
length = int(movie.length / 1000)
l = int(math.ceil(length / lineWidth) + 1)
if lines == -1 or l < lines:
lines = l
size = (lineWidth, rowHeight * lines)
timelineColor = (64, 64, 64)
i = Image.new("RGBA", size)
for currentLine in range(0, lines):
offset = currentLine * rowHeight + 4
try:
data = oxdb_cache.loadTimeline(movie, "%02d" % (currentLine * 10))
f = StringIO(data)
t = Image.open(f)
t = t.convert('RGBA')
box = (0, offset , t.size[0], offset + t.size[1])
i.paste(t, box)
except:
width = lineWidth
if currentLine == lines -1:
width = length - (lines - 1) * lineWidth
box = ((0, offset , width, offset + timlelineHeight))
i.paste(timelineColor, box)
f = StringIO()
i.save(f, 'PNG')
return f.getvalue()
'''
returns timeline overlay as a png image for a given movie
query is used to only highlight scenes matching query
'''
def loadTimelineOverlay(movie, query, lines = -1):
background = (255,255,255,0)
marker = (255,255,0,128)
markerBorder = (255,255,0,255)
length = int(movie.length / 1000)
l = int(math.ceil(length / lineWidth) + 1)
if lines == -1 or l < lines:
lines = l
size = (lineWidth, rowHeight * lines)
mask = Image.new("RGBA", size, background)
for subtitle in movie.overlay(query):
start = int(round(time2ms(subtitle.start) / 1000))
stop = int(round(time2ms(subtitle.stop) / 1000))
if start < stop:
currentLine = math.ceil(start / lineWidth)
if currentLine <= l:
offset = currentLine * rowHeight + 4
start = start - ((currentLine) * lineWidth)
stop = stop - ((currentLine) * lineWidth)
box = ((start, offset -1, stop, offset + timlelineHeight + 1))
mask.paste(marker, box)
borderBox = ((start, offset -1, stop, offset))
mask.paste(markerBorder, borderBox)
borderBox = ((start, offset + timlelineHeight, stop, offset + timlelineHeight +1))
mask.paste(markerBorder, borderBox)
f = StringIO()
mask.save(f, 'PNG', quality=70)
return f.getvalue()
return Image.composite(image, overlay, mask)
'''
returns an image map marking all the scenes with mouse events
for a given movie.
'''
def loadTimelineImageMap(movie):
s = movie.subtitleDict
length = int(movie.length / 1000)
imageMap ='<map name="timelineImageMap">'
for key in sorted([int(k) for k in s]):
sub = s["%s" % key]
start = int(round(time2ms(sub['start']) / 1000))
stop = int(round(time2ms(sub['stop']) / 1000))
if start < stop:
currentLine = math.ceil(start / lineWidth)
offset = int(currentLine * rowHeight + 4)
start = int(start - ((currentLine) * lineWidth))
stop = int(stop - ((currentLine) * lineWidth))
box = (start, offset -1, stop, offset + timlelineHeight + 1)
area = '<area class="timelineMarker" shape="rect" coords="%s, %s, %s, %s"' % box
area += ' onMouseOver="iS(%s, %s, %s, %s)" ' % (start, stop, offset, key)
area += ' onClick="cS(%s, %s, %s, %s)" ' % (start, stop, offset, key)
area += ' onMouseOut="oS()" />'
imageMap += area
imageMap += "</map>"
return imageMap