From 74a82a52e636f2eb29b18b1b4ba915a3086c51b3 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 28 Apr 2013 12:07:49 +0000 Subject: [PATCH] only create derivatives with lower resolution. return best match if resolution does not exist for defined resolutions. --- pandora/archive/models.py | 42 +++++++++++++++++++++++++-------------- pandora/item/views.py | 10 ++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/pandora/archive/models.py b/pandora/archive/models.py index b7fbefb3..881722bb 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -466,28 +466,40 @@ class Stream(models.Model): def __unicode__(self): return u"%s/%s" % (self.file, self.name()) + def get(self, resolution, format): + streams = [] + if self.format == format: + streams.append(self) + for stream in self.derivatives.filter(format=format).order_by('-resolution'): + streams.append(stream) + stream = streams.pop(0) + while streams and streams[0].resolution >= resolution: + stream = streams.pop(0) + return stream + def path(self, name=''): return self.file.get_path(name) def extract_derivatives(self, rebuild=False): config = settings.CONFIG['video'] for resolution in sorted(config['resolutions'], reverse=True): - for f in config['formats']: - derivative, created = Stream.objects.get_or_create(file=self.file, - resolution=resolution, format=f) - - name = derivative.name() - name = os.path.join(os.path.dirname(self.media.name), name) - if created: - derivative.source = self - derivative.save() - derivative.media.name = name - derivative.encode() - derivative.save() - elif rebuild or not derivative.available: - if not derivative.media: + if resolution <= self.resolution: + for f in config['formats']: + derivative, created = Stream.objects.get_or_create(file=self.file, + resolution=resolution, format=f) + + name = derivative.name() + name = os.path.join(os.path.dirname(self.media.name), name) + if created: + derivative.source = self + derivative.save() derivative.media.name = name - derivative.encode() + derivative.encode() + derivative.save() + elif rebuild or not derivative.available: + if not derivative.media: + derivative.media.name = name + derivative.encode() return True def encode(self): diff --git a/pandora/item/views.py b/pandora/item/views.py index ba34ed58..06b8c2b6 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -856,6 +856,10 @@ def torrent(request, id, filename=None): return response def video(request, id, resolution, format, index=None): + resolution = int(resolution) + resolutions = sorted(settings.CONFIG['video']['resolutions']) + if resolution not in resolutions: + raise Http404 item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() @@ -863,12 +867,10 @@ def video(request, id, resolution, format, index=None): index = int(index) - 1 else: index = 0 - streams = Stream.objects.filter(file__item__itemId=item.itemId, - file__selected=True, - resolution=resolution, format=format).order_by('file__part', 'file__sort_path') + streams = item.streams() if index + 1 > streams.count(): raise Http404 - stream = streams[index] + stream = streams[index].get(resolution, format) if not stream.available or not stream.media: raise Http404 path = stream.media.path