refactor subtitles

This commit is contained in:
j 2017-06-17 18:00:24 +02:00
parent feeba64fb9
commit ddb318fa72
1 changed files with 19 additions and 10 deletions

View File

@ -65,7 +65,7 @@ def get_subtitles(items, id):
if item['title'].startswith(id):
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:
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()
# just use strip_tags?
# value = ox.strip_tags(ox.decode_html(sub['value']))
if is_korean:
subs[sub_id]['value'].insert(0, value)
else:
if lang is None:
if is_korean:
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]['ids'].append(sub['id'])
position += clip['duration']
subs = sorted(subs.values(), key=lambda c: (c['in'], c['out']))
for sub in subs:
sub['value'] = '\n'.join(sub['value'])
subtitles.append(sub)
with open(output_srt, 'wb') as fd:
fd.write(ox.srt.encode(subtitles))
if output_srt:
with open(output_srt, 'wb') as fd:
fd.write(ox.srt.encode(subtitles))
with open(output_json, 'w') as fd:
json.dump(subtitles, fd, indent=4, ensure_ascii=False)
@ -125,7 +131,10 @@ if __name__ == '__main__':
else:
files = glob('output/*/*.json')
for item_json in files:
prefix = 'public/' + item_json.split('/')[-1][0].lower() + item_json.split('/')[-2] + '.1080p.'
output_json = prefix + 'json'
output_srt = prefix + 'srt'
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_json = prefix + lang + '.json'
render_subtitles(item_json, output_json, None, lang)