pandora_t_for_time/management/commands/generate_clips.py

68 lines
2.8 KiB
Python
Raw Normal View History

2023-10-08 11:06:24 +00:00
import json
2023-10-08 11:07:57 +00:00
import os
from collections import defaultdict
2023-10-08 11:06:24 +00:00
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):
2023-10-08 11:07:57 +00:00
parser.add_argument('--prefix', action='store', dest='prefix', default="/srv/t_for_time", help='prefix to build clips in')
2023-10-08 11:06:24 +00:00
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:
2023-10-08 11:06:24 +00:00
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)
2023-10-09 13:10:34 +00:00
source = e.files.all()[0].data.path
2023-10-08 11:06:24 +00:00
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', [])
if "original" in clip and "foreground" in clip and "background" in clip:
clips.append(clip)
else:
print("ignoring incomplete video", i)
2023-10-08 11:06:24 +00:00
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)