From 2a6a48f135352086f3f65f98d5f3f4a7c309e92d Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 18 Aug 2011 21:37:12 +0200 Subject: [PATCH] cleanup streams --- pandora/archive/models.py | 20 +++++++++++++++++++- pandora/item/models.py | 40 +++++++++++---------------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/pandora/archive/models.py b/pandora/archive/models.py index 08a06b7c..f7d1a659 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -472,7 +472,9 @@ class Stream(models.Model): source = models.ForeignKey('Stream', related_name='derivatives', default=None, null=True) available = models.BooleanField(default=False) info = fields.DictField(default={}) - + duration = models.FloatField(default=0) + aspect_ratio = models.FloatField(default=0) + @property def timeline_prefix(self): return os.path.join(settings.MEDIA_ROOT, self.path(), 'timeline') @@ -518,8 +520,24 @@ class Stream(models.Model): def save(self, *args, **kwargs): if self.video and not self.info: self.info = ox.avinfo(self.video.path) + self.duration = self.info.get('duration', 0) + if 'video' in self.info and self.info['video']: + self.aspect_ratio = self.info['video'][0]['width'] / self.info['video'][0]['height'] + else: + self.aspect_ratio = 128/80 super(Stream, self).save(*args, **kwargs) + def json(self): + if settings.XSENDFILE or settings.XACCELREDIRECT: + base_url = '/%s' % self.itemId + else: + base_url = os.path.dirname(self.video.url) + return { + 'duration': self.duration, + 'aspectRatio': self.aspect_ratio, + 'baseUrl': base_url + } + def delete_stream(sender, **kwargs): f = kwargs['instance'] if f.video: diff --git a/pandora/item/models.py b/pandora/item/models.py index 7baa0f34..797e437d 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -295,7 +295,6 @@ class Item(models.Model): shutil.rmtree(path) def delete(self, *args, **kwargs): - self.streams.all().delete() self.delete_files() super(Item, self).delete(*args, **kwargs) @@ -391,21 +390,8 @@ class Item(models.Model): return frames def get_stream(self): - stream = {} - videos = self.main_videos() - for video in videos: - s = video.streams.all()[0] - if s.video and s.info: - stream['duration'] = s.info['duration'] - if 'video' in s.info and s.info['video']: - stream['aspectRatio'] = s.info['video'][0]['width'] / s.info['video'][0]['height'] - else: - stream['aspectRatio'] = 128/80 - if settings.XSENDFILE or settings.XACCELREDIRECT: - stream['baseUrl'] = '/%s' % self.itemId - else: - stream['baseUrl'] = os.path.dirname(s.video.url) - return stream + for s in self.streams(): + return s.json() def get_layers(self, user=None): layers = {} @@ -450,9 +436,8 @@ class Item(models.Model): if not keys or 'poster' in keys: i['poster'] = self.get_poster() - videos = self.main_videos() - i['duration'] = sum([v.duration for v in videos]) - i['durations'] = [v.duration for v in videos] + i['durations'] = [s.duration for s in self.streams()] + i['duration'] = sum(i['durations']) i['apsectRatio'] = i.get('aspectratio') #only needed by admins @@ -704,18 +689,12 @@ class Item(models.Model): ''' def frame(self, position, height=128): offset = 0 - videos = self.main_videos() - for video in videos: - if video.duration + offset < position: - offset += video.duration + streams = self.streams() + for stream in streams: + if stream.duration + offset < position: + offset += stream.duration else: position = position - offset - stream = video.streams.filter(resolution=settings.VIDEO_RESOLUTIONS[0], - format=settings.VIDEO_FORMATS[0]) - if stream.count()>0: - stream = stream[0] - else: - return None height = min(height, stream.resolution) path = os.path.join(settings.MEDIA_ROOT, stream.path(), 'frames', "%dp"%height, "%s.jpg"%position) @@ -812,6 +791,9 @@ class Item(models.Model): self.torrent.name = self.path('torrent/%s.torrent' % self.get('title')) self.save() + def streams(self): + return [video.streams.filter(source=None)[0] for video in self.main_videos()] + def update_streams(self, force=False): files = {} for f in self.main_videos():