cleanup streams

This commit is contained in:
j 2011-08-18 21:37:12 +02:00
parent d186ab42d8
commit 2a6a48f135
2 changed files with 30 additions and 30 deletions

View file

@ -472,6 +472,8 @@ class Stream(models.Model):
source = models.ForeignKey('Stream', related_name='derivatives', default=None, null=True) source = models.ForeignKey('Stream', related_name='derivatives', default=None, null=True)
available = models.BooleanField(default=False) available = models.BooleanField(default=False)
info = fields.DictField(default={}) info = fields.DictField(default={})
duration = models.FloatField(default=0)
aspect_ratio = models.FloatField(default=0)
@property @property
def timeline_prefix(self): def timeline_prefix(self):
@ -518,8 +520,24 @@ class Stream(models.Model):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.video and not self.info: if self.video and not self.info:
self.info = ox.avinfo(self.video.path) 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) 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): def delete_stream(sender, **kwargs):
f = kwargs['instance'] f = kwargs['instance']
if f.video: if f.video:

View file

@ -295,7 +295,6 @@ class Item(models.Model):
shutil.rmtree(path) shutil.rmtree(path)
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
self.streams.all().delete()
self.delete_files() self.delete_files()
super(Item, self).delete(*args, **kwargs) super(Item, self).delete(*args, **kwargs)
@ -391,21 +390,8 @@ class Item(models.Model):
return frames return frames
def get_stream(self): def get_stream(self):
stream = {} for s in self.streams():
videos = self.main_videos() return s.json()
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
def get_layers(self, user=None): def get_layers(self, user=None):
layers = {} layers = {}
@ -450,9 +436,8 @@ class Item(models.Model):
if not keys or 'poster' in keys: if not keys or 'poster' in keys:
i['poster'] = self.get_poster() i['poster'] = self.get_poster()
videos = self.main_videos() i['durations'] = [s.duration for s in self.streams()]
i['duration'] = sum([v.duration for v in videos]) i['duration'] = sum(i['durations'])
i['durations'] = [v.duration for v in videos]
i['apsectRatio'] = i.get('aspectratio') i['apsectRatio'] = i.get('aspectratio')
#only needed by admins #only needed by admins
@ -704,18 +689,12 @@ class Item(models.Model):
''' '''
def frame(self, position, height=128): def frame(self, position, height=128):
offset = 0 offset = 0
videos = self.main_videos() streams = self.streams()
for video in videos: for stream in streams:
if video.duration + offset < position: if stream.duration + offset < position:
offset += video.duration offset += stream.duration
else: else:
position = position - offset 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) height = min(height, stream.resolution)
path = os.path.join(settings.MEDIA_ROOT, stream.path(), path = os.path.join(settings.MEDIA_ROOT, stream.path(),
'frames', "%dp"%height, "%s.jpg"%position) '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.torrent.name = self.path('torrent/%s.torrent' % self.get('title'))
self.save() self.save()
def streams(self):
return [video.streams.filter(source=None)[0] for video in self.main_videos()]
def update_streams(self, force=False): def update_streams(self, force=False):
files = {} files = {}
for f in self.main_videos(): for f in self.main_videos():