From ddb318fa729b53a08befc9ba7f5bd56f8e099841 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 17 Jun 2017 18:00:24 +0200 Subject: [PATCH] refactor subtitles --- subtitles.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/subtitles.py b/subtitles.py index adbb0d0..533ebc1 100755 --- a/subtitles.py +++ b/subtitles.py @@ -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('', '').replace('', '').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)