diff --git a/management/commands/generate_clips.py b/management/commands/generate_clips.py new file mode 100644 index 0000000..ec02002 --- /dev/null +++ b/management/commands/generate_clips.py @@ -0,0 +1,40 @@ +import json + +from django.core.management.base import BaseCommand +from django.conf import settings + +import item.models +import itemlist.models + + +class Command(BaseCommand): + help = 'generate symlinks to clips and clips.json' + + def add_arguments(self, parser): + parser.add_argument('--prefix', action='store_', dest='prefix', default="/srv/t_for_time", help='prefix to build clips in') + + def handle(self, **options): + prefix = options['prefix'] + clips = [] + for i in item.models.Item.objects.filter(data__type__contains="Original"): + qs = item.models.Item.objects.filter(data__title=i.data['title']).exclude(id=i.id) + if qs.count() == 2: + clip = {} + durations = [] + for e in item.models.Item.objects.filter(data__title=i.data['title']): + source = i.files.all()[0].data.path + ext = os.path.splitext(source)[1] + type_ = e.data['type'][0].lower() + target = os.path.join(prefix, type_, i.data['title'] + ext) + os.makedirs(os.path.dirname(target), exist_ok=True) + if os.path.exists(target): + os.unlink(target) + os.symlink(source, target) + clip[type_] = target + durations.append(e.files.all()[0].duration) + clip["duration"] = min(durations) + clip['tags'] = i.data.get('tags', []) + clips.append(clip) + + with open(os.path.join(prefix, 'clips.json'), 'w') as fd: + json.dump(clips, fd, indent=2, ensure_ascii=False)