only create derivatives with lower resolution. return best match if resolution does not exist for defined resolutions.

This commit is contained in:
j 2013-04-28 12:07:49 +00:00
parent 03bebfd623
commit 74a82a52e6
2 changed files with 33 additions and 19 deletions

View file

@ -466,28 +466,40 @@ class Stream(models.Model):
def __unicode__(self): def __unicode__(self):
return u"%s/%s" % (self.file, self.name()) 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=''): def path(self, name=''):
return self.file.get_path(name) return self.file.get_path(name)
def extract_derivatives(self, rebuild=False): def extract_derivatives(self, rebuild=False):
config = settings.CONFIG['video'] config = settings.CONFIG['video']
for resolution in sorted(config['resolutions'], reverse=True): for resolution in sorted(config['resolutions'], reverse=True):
for f in config['formats']: if resolution <= self.resolution:
derivative, created = Stream.objects.get_or_create(file=self.file, for f in config['formats']:
resolution=resolution, format=f) derivative, created = Stream.objects.get_or_create(file=self.file,
resolution=resolution, format=f)
name = derivative.name() name = derivative.name()
name = os.path.join(os.path.dirname(self.media.name), name) name = os.path.join(os.path.dirname(self.media.name), name)
if created: if created:
derivative.source = self derivative.source = self
derivative.save() derivative.save()
derivative.media.name = name
derivative.encode()
derivative.save()
elif rebuild or not derivative.available:
if not derivative.media:
derivative.media.name = name 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 return True
def encode(self): def encode(self):

View file

@ -856,6 +856,10 @@ def torrent(request, id, filename=None):
return response return response
def video(request, id, resolution, format, index=None): 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) item = get_object_or_404(models.Item, itemId=id)
if not item.access(request.user): if not item.access(request.user):
return HttpResponseForbidden() return HttpResponseForbidden()
@ -863,12 +867,10 @@ def video(request, id, resolution, format, index=None):
index = int(index) - 1 index = int(index) - 1
else: else:
index = 0 index = 0
streams = Stream.objects.filter(file__item__itemId=item.itemId, streams = item.streams()
file__selected=True,
resolution=resolution, format=format).order_by('file__part', 'file__sort_path')
if index + 1 > streams.count(): if index + 1 > streams.count():
raise Http404 raise Http404
stream = streams[index] stream = streams[index].get(resolution, format)
if not stream.available or not stream.media: if not stream.available or not stream.media:
raise Http404 raise Http404
path = stream.media.path path = stream.media.path