pandora_t_for_time/management/commands/generate_clips.py
2023-10-19 15:52:35 +01:00

68 lines
2.9 KiB
Python

import json
import os
from collections import defaultdict
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']):
if 'type' not in e.data:
print("ignoring invalid video", e)
source = e.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', [])
clip['editingtags'] = i.data.get('editingtags', [])
if "original" in clip and "foreground" in clip and "background" in clip:
clips.append(clip)
else:
print("ignoring incomplete video", i)
with open(os.path.join(prefix, 'clips.json'), 'w') as fd:
json.dump(clips, fd, indent=2, ensure_ascii=False)
voice_over = defaultdict(dict)
for vo in item.models.Item.objects.filter(
data__type__contains="Voice Over",
):
fragment_id = int(vo.get('title').split('_')[0])
source = vo.files.all()[0]
batch = vo.get('batch')[0].replace('Text-', '')
src = source.data.path
target = os.path.join(prefix, 'voice_over', batch, '%s.wav' % fragment_id)
os.makedirs(os.path.dirname(target), exist_ok=True)
if os.path.exists(target):
os.unlink(target)
os.symlink(src, target)
voice_over[fragment_id][batch] = {
"src": target,
"duration": source.duration
}
with open(os.path.join(prefix, 'voice_over.json'), 'w') as fd:
json.dump(voice_over, fd, indent=2, ensure_ascii=False)