From 3782ca672106840fe5b52963d4e6c9514a36259e Mon Sep 17 00:00:00 2001 From: j Date: Fri, 22 Mar 2024 11:33:39 +0100 Subject: [PATCH] multiple languages --- management/commands/export_subtitles.py | 28 ++++++++++++++++++---- management/commands/generate_clips.py | 13 ++++++++-- render.py | 32 ++++++++++++++++++++++--- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/management/commands/export_subtitles.py b/management/commands/export_subtitles.py index 8782c77..c954e90 100644 --- a/management/commands/export_subtitles.py +++ b/management/commands/export_subtitles.py @@ -2,25 +2,43 @@ import json import os import subprocess +import ox + from django.core.management.base import BaseCommand from django.conf import settings +from ...render import add_translations + class Command(BaseCommand): help = 'export all subtitles for translations' def add_arguments(self, parser): - parser.add_argument('--lang', action='store', dest='lang', default="", help='subtitle language') + parser.add_argument('--lang', action='store', dest='lang', default=None, help='subtitle language') def handle(self, **options): - lang = options["lang"] import annotation.models import item.models + lang = options["lang"] + if lang: + lang = lang.split(',') + tlang = lang[1:] + lang = lang[0] + else: + tlang = None + if lang == "en": + lang = None + for i in item.models.Item.objects.filter(data__type__contains='Voice Over').order_by('sort__title'): print("## %s %s" % (i.get("title"), i.public_id)) - for sub in i.annotations.all().filter(layer='subtitles').exclude(value='').filter(languages=lang).order_by("start"): - if not sub.languages: - print(sub.value.strip() + "\n") + if tlang: + value = add_translations(sub, tlang) + value = ox.strip_tags(value) + else: + value = sub.value.replace('
', '
').replace('
\n', '\n').replace('
', '\n').strip() + if sub.languages: + value = ox.strip_tags(value) + print(value.strip() + "\n") print("\n\n\n") diff --git a/management/commands/generate_clips.py b/management/commands/generate_clips.py index d7ae9f8..23bc61c 100644 --- a/management/commands/generate_clips.py +++ b/management/commands/generate_clips.py @@ -33,6 +33,15 @@ class Command(BaseCommand): def handle(self, **options): prefix = options['prefix'] + lang = options["lang"] + if lang: + lang = lang.split(',') + tlang = lang[1:] + lang = lang[0] + else: + tlang = None + if lang == "en": + lang = None clips = [] for i in item.models.Item.objects.filter(sort__type='original'): qs = item.models.Item.objects.filter(data__title=i.data['title']).exclude(id=i.id) @@ -103,8 +112,8 @@ class Command(BaseCommand): os.unlink(target) os.symlink(src, target) subs = [] - for sub in vo.annotations.filter(layer="subtitles", languages=options["lang"]).exclude(value="").order_by("start"): - sdata = get_srt(sub) + for sub in vo.annotations.filter(layer="subtitles", languages=lang).exclude(value="").order_by("start"): + sdata = get_srt(sub, lang=tlang) subs.append(sdata) voice_over[fragment_id][batch] = { "src": target, diff --git a/render.py b/render.py index 72d3cf9..f974547 100644 --- a/render.py +++ b/render.py @@ -566,9 +566,26 @@ def render_all(options): json.dump(_CACHE, fd) -def get_srt(sub, offset=0): +def add_translations(sub, lang): + value = sub.value.replace('
', '
').replace('
\n', '\n').replace('
', '\n').strip() + if sub.languages: + value = ox.strip_tags(value) + if lang: + for slang in lang: + if slang == "en": + slang = None + for tsub in sub.item.annotations.filter(layer="subtitles", start=sub.start, end=sub.end, languages=slang): + tvalue = tsub.value.replace('
', '
').replace('
\n', '\n').replace('
', '\n').strip() + if tsub.languages: + tvalue = ox.strip_tags(tvalue) + value += '\n' + tvalue + return value + +def get_srt(sub, offset=0, lang=None): sdata = sub.json(keys=['in', 'out', 'value']) - sdata['value'] = sdata['value'].replace('
', '
').replace('
\n', '\n').replace('
', '\n') + sdata['value'] = sdata['value'].replace('
', '
').replace('
\n', '\n').replace('
', '\n').strip() + if lang: + sdata['value'] = add_translations(sub, lang) if offset: sdata["in"] += offset sdata["out"] += offset @@ -592,6 +609,8 @@ def update_subtitles(options): duration = int(options['duration']) base = int(options['offset']) lang = options["lang"] + if lang and "," in lang: + lang = lang.split(',') _cache = os.path.join(prefix, "cache.json") if os.path.exists(_cache): @@ -611,8 +630,15 @@ def update_subtitles(options): vo = item.models.Item.objects.filter(data__batch__icontains=batch, data__title__startswith=fragment_id + '_').first() if vo: #print("%s => %s %s" % (clip['src'], vo, vo.get('batch'))) + if isinstance(lang, list): + tlang = lang[1:] + lang = lang[0] + else: + tlang = None + if lang == "en": + lang = None for sub in vo.annotations.filter(layer="subtitles").filter(languages=lang).exclude(value="").order_by("start"): - sdata = get_srt(sub, offset) + sdata = get_srt(sub, offset, tlang) subs.append(sdata) else: print("could not find vo for %s" % clip['src'])