From 4df413841baad7f01d96572977091ba2a6b1c408 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 31 Aug 2017 22:08:03 +0200 Subject: [PATCH] merge subs --- subtitles.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/subtitles.py b/subtitles.py index b9d590b..b0d0bb7 100755 --- a/subtitles.py +++ b/subtitles.py @@ -137,6 +137,8 @@ def render_subtitles(item_json, output_json, output_srt, lang): 'ids': [], 'languages': [] } + if not sub['value']: + continue if set(subs[sub_id]['languages']) != set(lang): if not subs[sub_id]['languages']: subs[sub_id]['value'] += sub['value'] @@ -172,6 +174,25 @@ def render_subtitles(item_json, output_json, output_srt, lang): if sub['value'].strip(): subtitles.append(sub) + merged = [] + p = None + for sub in subtitles: + if not p: + merged.append(sub) + p = sub + else: + if p['out'] > sub['in']: + if p['value'] == sub['value']: + p['out'] = max(p['out'], sub['out']) + else: + p['out'] = sub['in'] + merged.append(sub) + p = sub + else: + merged.append(sub) + p = sub + subtitles = merged + if output_srt: with open(output_srt, 'wb') as fd: fd.write(ox.srt.encode(subtitles))