From 76aa590bbbdf6ccd627581b330f851374863b7cd Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 29 May 2012 17:02:59 +0200 Subject: [PATCH] add manage.py command to extract derivatives, fixes #783 --- .../commands/extract_derivatives.py | 27 +++++++++++++++++++ pandora/archive/models.py | 6 +++-- pandora/archive/tasks.py | 8 ++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 pandora/archive/management/commands/extract_derivatives.py diff --git a/pandora/archive/management/commands/extract_derivatives.py b/pandora/archive/management/commands/extract_derivatives.py new file mode 100644 index 00000000..37d7e9d1 --- /dev/null +++ b/pandora/archive/management/commands/extract_derivatives.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from django.core.management.base import BaseCommand +from django.conf import settings +import os + +import monkey_patch.models +from ... import models +from ...tasks import extract_derivatives + +class Command(BaseCommand): + """ + """ + help = 'extract derivatives, run this to recreate all derivatives. i.e after adding new resolutions' + args = '' + option_list = BaseCommand.option_list + ( + make_option('--rebuild', action='store_true', dest='rebuild', + default=False, help='reencode all derivatives again'), + make_option('--forground', action='store_true', dest='forground', + default=False, help='dont dispatch encoding to celery but run in forground'), + ) + def handle(self, **options): + for s in models.Stream.objects.filter(source=None): + if options['forground']: + extract_derivatives(s.item.id, options['rebuild']) + else: + extract_derivatives.delay(s.item.id, options['rebuild']) diff --git a/pandora/archive/models.py b/pandora/archive/models.py index 564db8d4..49d576d7 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -409,7 +409,7 @@ class Stream(models.Model): def path(self, name=''): return self.file.get_path(name) - def extract_derivatives(self): + def extract_derivatives(self, rebuild=False): config = settings.CONFIG['video'] for resolution in config['resolutions']: for f in config['formats']: @@ -422,6 +422,8 @@ class Stream(models.Model): derivative.video.name = os.path.join(os.path.dirname(self.video.name), name) derivative.encode() derivative.save() + else if rebuild or not derivative.available: + derivative.encode() return True def encode(self): @@ -440,7 +442,7 @@ class Stream(models.Model): extract.timeline(self.video.path, self.timeline_prefix) self.cuts = tuple(extract.cuts(self.timeline_prefix)) self.color = tuple(extract.average_color(self.timeline_prefix)) - self.volume= extract.average_volume(self.timeline_prefix) + self.volume = extract.average_volume(self.timeline_prefix) self.save() def save(self, *args, **kwargs): diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index 8fd59044..80cb9f74 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -104,6 +104,14 @@ def process_stream(fileId): file.item.update_timeline() return True +@task(queue="encoding") +def extract_derivatives(fileId, rebuild=False): + file = models.File.objects.get(id=fileId) + streams = file.streams.filter(source=None) + if streams.count() > 0: + stream[0].extract_derivatives(rebuild) + return True + @task(queue="encoding") def update_stream(id): s = models.Stream.objects.get(pk=id)