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):
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):

View file

@ -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