multi lingual

This commit is contained in:
j 2017-08-25 11:12:32 +02:00
parent c413272c7e
commit dff582960f
3 changed files with 41 additions and 16 deletions

View file

@ -1,6 +1,13 @@
#!/bin/bash #!/bin/bash
cd "`dirname "$0"`" cd "`dirname "$0"`"
gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend 'false' 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 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.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 exec ./play.py

View file

@ -16,6 +16,9 @@ lock = '/tmp/update.lock'
folder = os.path.abspath(os.path.dirname(__file__)) folder = os.path.abspath(os.path.dirname(__file__))
os.chdir(folder) os.chdir(folder)
lang = '1080p'
lang = 'no-en'
def get_subtitle(url, name): def get_subtitle(url, name):
try: try:
r = requests.get(url, stream=True) r = requests.get(url, stream=True)
@ -25,6 +28,7 @@ def get_subtitle(url, name):
except: except:
pass pass
if os.path.exists(lock): if os.path.exists(lock):
sys.exit(0) sys.exit(0)
with open(lock, 'w') as f: 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()) mtime = time.mktime(datetime.datetime.strptime(r.headers.get("Last-Modified"), "%a, %d %b %Y %X GMT").timetuple())
os.utime(name, (mtime, mtime)) os.utime(name, (mtime, mtime))
ox.avinfo(name)['duration'] 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: except KeyboardInterrupt:
os.unlink(lock) os.unlink(lock)
sys.exit(-1) sys.exit(-1)

View file

@ -2,6 +2,7 @@
import os import os
import sys import sys
import json import json
import re
import subprocess import subprocess
from collections import defaultdict from collections import defaultdict
import string import string
@ -65,7 +66,7 @@ def get_subtitles(items, id):
if item['title'].startswith(id): if item['title'].startswith(id):
return deepcopy(item['subtitles']) 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: with open(item_json) as fd:
item = json.load(fd) item = json.load(fd)
@ -89,22 +90,27 @@ def render_subtitles(item_json, output_json, output_srt, lang=None):
'out': sub_out, 'out': sub_out,
'value': [], 'value': [],
} }
if lang is None: if isinstance(lang, list):
subs[sub_id]['ids'] = [] subs[sub_id]['ids'] = []
is_korean = 'lang="ko"' in sub['value'] subs[sub_id]['languages'] = []
value = sub['value'].replace('<br>', '')
value = value.replace('<span lang="ko">', '').replace('</span>', '').strip() slang = re.compile('span lang="(..)"').findall(sub['value'])
value = sub['value'].replace('<br>', '').strip()
if slang:
slang = slang[0]
value = value.replace('<span lang="' + slang + '">', '').replace('</span>', '').strip()
else:
slang = 'en'
# just use strip_tags? # just use strip_tags?
# value = ox.strip_tags(ox.decode_html(sub['value'])) # value = ox.strip_tags(ox.decode_html(sub['value']))
if lang is None: if isinstance(lang, list) and slang in lang:
if is_korean: if lang.index(slang) == 0:
subs[sub_id]['value'].insert(0, value) subs[sub_id]['value'].insert(0, value)
else: else:
subs[sub_id]['value'].append(value) subs[sub_id]['value'].append(value)
subs[sub_id]['ids'].append(sub['id']) subs[sub_id]['ids'].append(sub['id'])
elif lang == 'ko' and is_korean: subs[sub_id]['languages'].append(slang)
subs[sub_id]['value'].append(value) elif slang == lang:
elif lang == 'en' and not is_korean:
subs[sub_id]['value'].append(value) subs[sub_id]['value'].append(value)
position += clip['duration'] position += clip['duration']
subs = sorted(subs.values(), key=lambda c: (c['in'], c['out'])) 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']) sub['value'] = '\n'.join(sub['value'])
subtitles.append(sub) 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: if output_srt:
with open(output_srt, 'wb') as fd: with open(output_srt, 'wb') as fd:
fd.write(ox.srt.encode(subtitles)) fd.write(ox.srt.encode(subtitles))
@ -134,8 +145,11 @@ if __name__ == '__main__':
for item_json in files: for item_json in files:
prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.' prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.'
output_json = prefix + '1080p.json' output_json = prefix + '1080p.json'
output_srt = prefix + '10800.srt' output_srt = prefix + '1080p.srt'
render_subtitles(item_json, output_json, output_srt) render_subtitles(item_json, output_json, output_srt, ['ko', 'en'])
for lang in ('en', 'ko'): 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' output_json = prefix + lang + '.json'
render_subtitles(item_json, output_json, None, lang) render_subtitles(item_json, output_json, None, lang)