refactor subtitles

This commit is contained in:
j 2017-06-17 18:00:24 +02:00
parent feeba64fb9
commit ddb318fa72

View file

@ -65,7 +65,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): def render_subtitles(item_json, output_json, output_srt, lang=None):
with open(item_json) as fd: with open(item_json) as fd:
item = json.load(fd) item = json.load(fd)
@ -95,19 +95,25 @@ def render_subtitles(item_json, output_json, output_srt):
value = value.replace('<span lang="ko">', '').replace('</span>', '').strip() value = value.replace('<span lang="ko">', '').replace('</span>', '').strip()
# 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 is_korean: if lang is None:
subs[sub_id]['value'].insert(0, value) if is_korean:
else: 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]['value'].append(value) subs[sub_id]['value'].append(value)
subs[sub_id]['ids'].append(sub['id'])
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']))
for sub in subs: for sub in subs:
sub['value'] = '\n'.join(sub['value']) sub['value'] = '\n'.join(sub['value'])
subtitles.append(sub) subtitles.append(sub)
with open(output_srt, 'wb') as fd: if output_srt:
fd.write(ox.srt.encode(subtitles)) with open(output_srt, 'wb') as fd:
fd.write(ox.srt.encode(subtitles))
with open(output_json, 'w') as fd: with open(output_json, 'w') as fd:
json.dump(subtitles, fd, indent=4, ensure_ascii=False) json.dump(subtitles, fd, indent=4, ensure_ascii=False)
@ -125,7 +131,10 @@ if __name__ == '__main__':
else: else:
files = glob('output/*/*.json') files = glob('output/*/*.json')
for item_json in files: for item_json in files:
prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.1080p.' prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.'
output_json = prefix + 'json' output_json = prefix + '1080p.json'
output_srt = prefix + 'srt' output_srt = prefix + '10800.srt'
render_subtitles(item_json, output_json, output_srt) render_subtitles(item_json, output_json, output_srt)
for lang in ('en', 'ko'):
output_json = prefix + lang + '.json'
render_subtitles(item_json, output_json, None, lang)