From dff582960f5cc38fc2a107e4047a5b4df5d759a3 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 25 Aug 2017 11:12:32 +0200 Subject: [PATCH] multi lingual --- playout/play | 11 +++++++++-- playout/update.py | 6 +++++- subtitles.py | 40 +++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/playout/play b/playout/play index ea27427..c5ad671 100644 --- a/playout/play +++ b/playout/play @@ -1,6 +1,13 @@ #!/bin/bash cd "`dirname "$0"`" -gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend 'false' -gsettings set org.gnome.settings-daemon.plugins.power idle-dim 'false' +gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend false +gsettings set org.gnome.desktop.screensaver lock-enabled false +gsettings set org.gnome.desktop.session idle-delay 0 +gsettings set org.gnome.settings-daemon.plugins.power idle-dim false gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing' +gsettings set org.gnome.desktop.background color-shading-type 'solid' +gsettings set org.gnome.desktop.background primary-color '#888888' +gsettings set org.gnome.desktop.background secondary-color '#888888' +gsettings set org.gnome.desktop.background picture-options 'none' + exec ./play.py diff --git a/playout/update.py b/playout/update.py index 7e8635e..9a25388 100755 --- a/playout/update.py +++ b/playout/update.py @@ -16,6 +16,9 @@ lock = '/tmp/update.lock' folder = os.path.abspath(os.path.dirname(__file__)) os.chdir(folder) +lang = '1080p' +lang = 'no-en' + def get_subtitle(url, name): try: r = requests.get(url, stream=True) @@ -25,6 +28,7 @@ def get_subtitle(url, name): except: pass + if os.path.exists(lock): sys.exit(0) with open(lock, 'w') as f: @@ -55,7 +59,7 @@ for i in range(10): mtime = time.mktime(datetime.datetime.strptime(r.headers.get("Last-Modified"), "%a, %d %b %Y %X GMT").timetuple()) os.utime(name, (mtime, mtime)) ox.avinfo(name)['duration'] - get_subtitle(url.replace('.mp4', '.srt'), name.replace('.mp4', '.srt')) + get_subtitle(url.replace('1080p.mp4', lang + '.srt'), name.replace('.mp4', '.srt')) except KeyboardInterrupt: os.unlink(lock) sys.exit(-1) diff --git a/subtitles.py b/subtitles.py index 6107e27..68d5ac1 100755 --- a/subtitles.py +++ b/subtitles.py @@ -2,6 +2,7 @@ import os import sys import json +import re import subprocess from collections import defaultdict import string @@ -65,7 +66,7 @@ def get_subtitles(items, id): if item['title'].startswith(id): return deepcopy(item['subtitles']) -def render_subtitles(item_json, output_json, output_srt, lang=None): +def render_subtitles(item_json, output_json, output_srt, lang): with open(item_json) as fd: item = json.load(fd) @@ -89,22 +90,27 @@ def render_subtitles(item_json, output_json, output_srt, lang=None): 'out': sub_out, 'value': [], } - if lang is None: + if isinstance(lang, list): subs[sub_id]['ids'] = [] - is_korean = 'lang="ko"' in sub['value'] - value = sub['value'].replace('
', '') - value = value.replace('', '').replace('', '').strip() + subs[sub_id]['languages'] = [] + + slang = re.compile('span lang="(..)"').findall(sub['value']) + value = sub['value'].replace('
', '').strip() + if slang: + slang = slang[0] + value = value.replace('', '').replace('', '').strip() + else: + slang = 'en' # just use strip_tags? # value = ox.strip_tags(ox.decode_html(sub['value'])) - if lang is None: - if is_korean: + if isinstance(lang, list) and slang in lang: + if lang.index(slang) == 0: subs[sub_id]['value'].insert(0, value) else: subs[sub_id]['value'].append(value) subs[sub_id]['ids'].append(sub['id']) - elif lang == 'ko' and is_korean: - subs[sub_id]['value'].append(value) - elif lang == 'en' and not is_korean: + subs[sub_id]['languages'].append(slang) + elif slang == lang: subs[sub_id]['value'].append(value) position += clip['duration'] subs = sorted(subs.values(), key=lambda c: (c['in'], c['out'])) @@ -112,6 +118,11 @@ def render_subtitles(item_json, output_json, output_srt, lang=None): sub['value'] = '\n'.join(sub['value']) subtitles.append(sub) + if isinstance(lang, list): + fixme = [sub for sub in subs if set(sub['languages']) != set(lang)] + if fixme: + print('split/merge overlaps') + if output_srt: with open(output_srt, 'wb') as fd: fd.write(ox.srt.encode(subtitles)) @@ -134,8 +145,11 @@ if __name__ == '__main__': for item_json in files: prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.' output_json = prefix + '1080p.json' - output_srt = prefix + '10800.srt' - render_subtitles(item_json, output_json, output_srt) - for lang in ('en', 'ko'): + output_srt = prefix + '1080p.srt' + render_subtitles(item_json, output_json, output_srt, ['ko', 'en']) + output_json = prefix + 'no-en.json' + output_srt = prefix + 'no-en.srt' + render_subtitles(item_json, output_json, output_srt, ['no', 'en']) + for lang in ('en', 'ko', 'no'): output_json = prefix + lang + '.json' render_subtitles(item_json, output_json, None, lang)