strip timeline

This commit is contained in:
j 2010-09-08 15:38:44 +02:00
parent 152d2f7cbe
commit 8947e51243
5 changed files with 85 additions and 12 deletions

View file

@ -260,7 +260,7 @@ def average_color(prefix):
for i in range(0, len(pixels)): for i in range(0, len(pixels)):
p = np.sum(pixels[i], axis=0) / frames p = np.sum(pixels[i], axis=0) / frames
color += p color += p
return list(color) return list(map(float, color))
def get_distance(rgb0, rgb1): def get_distance(rgb0, rgb1):
dst = math.sqrt(pow(rgb0[0] - rgb1[0], 2) + pow(rgb0[0] - rgb1[0], 2) + pow(rgb0[0] - rgb1[0], 2)) dst = math.sqrt(pow(rgb0[0] - rgb1[0], 2) + pow(rgb0[0] - rgb1[0], 2) + pow(rgb0[0] - rgb1[0], 2))
@ -292,3 +292,69 @@ def cuts(prefix):
cuts.append(frame / fps) cuts.append(frame / fps)
return cuts return cuts
def divide(num, by):
# >>> divide(100, 3)
# [33, 33, 34]
arr = []
div = int(num / by)
mod = num % by
for i in range(by):
arr.append(div + (i > by - 1 - mod))
return arr
def strip_timeline(movie, cuts, info, prefix):
_debug = False
duration = info['duration']
video_height = info['video'][0]['height']
video_width = info['video'][0]['width']
video_ratio = video_width / video_height
line_image = []
timeline_height = 64
timeline_width = 1500
fps = 25
frames = duration * fps
if cuts[0] != 0:
cuts.insert(0, 0)
cuts = map(lambda x: int(round(x * fps)), cuts)
for frame in range(frames):
i = int(frame / timeline_width)
x = frame % timeline_width
if x == 0:
timeline_width = min(timeline_width, frames - frame)
timeline_image = Image.new('RGB', (timeline_width, timeline_height))
if frame in cuts:
c = cuts.index(frame)
duration = cuts[c + 1] - cuts[c]
stills = math.ceil(duration / (video_width * timeline_height / video_height))
widths = divide(duration, stills)
still = frame
if _debug:
print widths, duration, stills, cuts[c], cuts[c + 1]
for s in range(int(stills)):
still_ratio = widths[s] / timeline_height
if video_ratio > still_ratio:
width = int(round(video_height * still_ratio))
left = int((video_width - width) / 2)
box = (left, 0, left + width, video_height)
else:
height = int(round(video_width / still_ratio))
top = int((video_height - height) / 2)
box = (0, top, video_width, top + height)
if _debug:
print frame, 'cut', c, 'still', s, still, 'width', widths[s], box
#FIXME: why does this have to be still+1?
frame_image = Image.open(movie.frame((still+1)/fps))
frame_image = frame_image.crop(box).resize((widths[s], timeline_height), Image.ANTIALIAS)
for x_ in range(widths[s]):
line_image.append(frame_image.crop((x_, 0, x_ + 1, timeline_height)))
still += widths[s]
timeline_image.paste(line_image[frame], (x, 0))
if x == timeline_width - 1:
timeline_file = '%sstrip.64.%04d.png' % (prefix, i)
if _debug:
print 'writing', timeline_file
timeline_image.save(timeline_file)

View file

@ -415,7 +415,7 @@ class Movie(models.Model):
@property @property
def timeline_prefix(self): def timeline_prefix(self):
return os.path.join('stream', movieid_path(self.movieId), 'timeline') return os.path.join(settings.MEDIA_ROOT, 'stream', movieid_path(self.movieId), 'timeline')
def updateStreams(self): def updateStreams(self):
files = {} files = {}
@ -440,9 +440,10 @@ class Movie(models.Model):
subprocess.Popen(cmd) subprocess.Popen(cmd)
stream.save() stream.save()
extract.timeline(stream.video.path, os.path.join(settings.MEDIA_ROOT, self.timeline_prefix)) extract.timeline(stream.video.path, self.timeline_prefix)
stream.extract_derivatives() stream.extract_derivatives()
self.metadata['cuts'] = extract.cuts(self.timeline_prefix)
self.metadata['average_color'] = extract.average_color(self.timeline_prefix)
#something with poster #something with poster
self.available = True self.available = True
self.save() self.save()

View file

@ -10,7 +10,7 @@ urlpatterns = patterns("backend.views",
(r'^(?P<id>.*)/(?P<profile>.*.mp4)$', 'video'), (r'^(?P<id>.*)/(?P<profile>.*.mp4)$', 'video'),
(r'^(?P<id>.*)/poster\.(?P<size>\d+)\.jpg$', 'poster'), (r'^(?P<id>.*)/poster\.(?P<size>\d+)\.jpg$', 'poster'),
(r'^(?P<id>.*)/poster\.jpg$', 'poster'), (r'^(?P<id>.*)/poster\.jpg$', 'poster'),
(r'^(?P<id>.*)/timelines/timeline\.(?P<size>\d+)\.(?P<position>\d+)\.png$', 'timeline'), (r'^(?P<id>.*)/timelines/(?P<timeline>.+)\.(?P<size>\d+)\.(?P<position>\d+)\.png$', 'timeline'),
(r'^(?P<id>.*)/data/(?P<data>.+)\.json$', 'data'), (r'^(?P<id>.*)/data/(?P<data>.+)\.json$', 'data'),
(r'^api/$', 'api'), (r'^api/$', 'api'),
) )

View file

@ -540,6 +540,8 @@ def data(request, id, data):
response = {} response = {}
if data == 'video': if data == 'video':
response = movie.get_stream() response = movie.get_stream()
if data == 'cuts':
response = movie.metadata.get('cuts', {})
return render_to_json_response(response) return render_to_json_response(response)
#media delivery #media delivery
@ -570,9 +572,12 @@ def poster(request, id, size=128):
poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png') poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png')
return HttpFileResponse(poster_path, content_type='image/jpeg') return HttpFileResponse(poster_path, content_type='image/jpeg')
def timeline(request, id, size, position): def timeline(request, id, timeline, size, position):
movie = get_object_or_404(models.Movie, movieId=id) movie = get_object_or_404(models.Movie, movieId=id)
timeline = os.path.join(settings.MEDIA_ROOT, '%s.%s.%04d.png' %(movie.timeline_prefix, size, int(position))) if timeline == 'strip':
timeline = '%s.%s.%04d.png' %(movie.timeline_prefix[:-8] + 'strip', size, int(position))
else:
timeline = '%s.%s.%04d.png' %(movie.timeline_prefix, size, int(position))
return HttpFileResponse(timeline, content_type='image/png') return HttpFileResponse(timeline, content_type='image/png')
def video(request, id, profile): def video(request, id, profile):
@ -580,7 +585,5 @@ def video(request, id, profile):
stream = get_object_or_404(movie.streams, profile=profile) stream = get_object_or_404(movie.streams, profile=profile)
path = stream.video.path path = stream.video.path
content_type = path.endswith('.mp4') and 'video/mp4' or 'video/webm' content_type = path.endswith('.mp4') and 'video/mp4' or 'video/webm'
#url = 'http://127.0.0.1/pandora_media' + path[len(settings.MEDIA_ROOT):]
#return redirect(url)
return HttpFileResponse(path, content_type=content_type) return HttpFileResponse(path, content_type=content_type)

View file

@ -23,7 +23,8 @@ $(function() {
posterFrame = 1515, posterFrame = 1515,
points = [2059, 2748], points = [2059, 2748],
videoId = document.location.hash.substring(1), videoId = document.location.hash.substring(1),
videoUrl = "/" + videoId + "/96p." + ($.support.video.webm ? "webm": "mp4"); videoUrl = "/" + videoId + "/96p." + ($.support.video.webm ? "webm": "mp4").
stripTimeline = false;
$.getJSON("/" + videoId + "/data/video.json", function(video) { $.getJSON("/" + videoId + "/data/video.json", function(video) {
var duration = video.duration, var duration = video.duration,
@ -33,6 +34,8 @@ $(function() {
position = duration/2; position = duration/2;
videoWidth += videoWidth%2; videoWidth += videoWidth%2;
videoUrl = video.baseUrl + "/96p." + ($.support.video.webm ? "webm": "mp4");
//resizeVideoPlayers(pageWidth); //resizeVideoPlayers(pageWidth);
Ox.Editor = function(options, self) { Ox.Editor = function(options, self) {
@ -577,7 +580,7 @@ $(function() {
if (!self.$tiles[v]) { if (!self.$tiles[v]) {
self.$tiles[v] = $("<img>") self.$tiles[v] = $("<img>")
.attr({ .attr({
src: "/" + self.options.videoId + "/timelines/" + (window.location.hash == "#strip" ? "strip" : "timeline") + ".64." + v + ".png" src: "/" + self.options.videoId + "/timelines/" + (stripTimeline ? "strip" : "timeline") + ".64." + v + ".png"
}) })
.css({ .css({
left: (v * self.tileWidth) + "px" left: (v * self.tileWidth) + "px"