From 2b3f666f6b48fb4334abf80d09c30504140aa4e2 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sun, 6 Sep 2020 19:53:23 +0200 Subject: [PATCH] manual fix scenes --- data/strange_stars/data.py | 3 ++ main.py | 91 +++++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 data/strange_stars/data.py diff --git a/data/strange_stars/data.py b/data/strange_stars/data.py new file mode 100644 index 0000000..042b94f --- /dev/null +++ b/data/strange_stars/data.py @@ -0,0 +1,3 @@ +FINAL_TITLE = "strange_stars" +DE_VIDEO = "https://www.youtube.com/watch?v=-1FvAEaE0fc" +EN_VIDEO = "https://www.youtube.com/watch?v=p_8yK2kmxoo" \ No newline at end of file diff --git a/main.py b/main.py index 24a9084..b163e90 100644 --- a/main.py +++ b/main.py @@ -6,14 +6,23 @@ from compare import linear_compare TIME_SEP = " --> " DECIMAL_SEP = '.' HMS_SEP = ':' +SCENE_BREAK = '-------' +EMPTY_IDX = '...' + SYNC_STEP = 30 FONT_SIZE = 18 WORD_BREAK = 60 MAX_ITER = 0 -SHOULD_REMOVE_TEMPS = False -SHOULD_CUT_VIDEOS = True +SHOULD_DOWNLOAD = False +SHOULD_SYNC = False +SHOULD_TIME = False +SHOULD_FIX_CUTS = False +SHOULD_CUT = True +SHOULD_CONCAT = True + +SHOULD_REMOVE_TEMPS = True SHOULD_COPY_CODEC = False # FINAL_TITLE = "plastic" @@ -62,6 +71,7 @@ def cut_videos(idx, en_start, en_end, de_start, de_end, en_txt, de_txt): print("cut_{:02d}: en: {:6.2f} -> {:6.2f}, de: {:6.2f} -> {:6.2f}".format( idx, en_start, en_end, de_start, de_end, )) + run_os_cmd("mkdir -p tmp") with open('tmp/concat_{}.txt'.format(idx), 'w+') as f: f.write('file de_{}.mp4\n'.format(idx)) f.write('file en_{}.mp4\n'.format(idx)) @@ -117,6 +127,8 @@ def time_subs(syncs): en_subs = parse_subs('subs.en.vtt') latest_de_idx = 0 iterable = en_subs[:MAX_ITER] if MAX_ITER else en_subs + with open('fix.txt', 'w+') as f: + f.write('') for i, en_sub in enumerate(iterable): if latest_de_idx >= len(de_subs): print("{:6.2f} {:6.2f} {:>6} {:>6} {:>90} {:>90}".format( @@ -125,34 +137,72 @@ def time_subs(syncs): )) continue else: - print("{:6.2f} {:6.2f} {:6.2f} {:6.2f} {:>90} {:>90}".format( + print("{:6.2f} {:6.2f} {:6.2f} {:6.2f} {:>90} {:<90}".format( en_sub[0], en_sub[1], de_subs[latest_de_idx][0], de_subs[latest_de_idx][1], - en_sub[2], de_subs[latest_de_idx][2], + en_sub[2], + de_subs[latest_de_idx][2], )) - de_first_start = de_subs[latest_de_idx][0] + with open('fix.txt', 'a+') as f: + f.write("{:03} ... {:>90} | {:<90}\n".format(i, en_sub[2], '')) + with open('fix.txt', 'a+') as f: + f.write("... {:03} {:>90} | {:<90}\n".format(latest_de_idx, '', de_subs[latest_de_idx][2])) de_txt = de_subs[latest_de_idx][2] latest_de_idx += 1 en_sub_start, en_sub_end = en_sub[0], en_sub[1] - de_last_end = de_subs[latest_de_idx - 1][1] while latest_de_idx < len(de_subs): de_next_sub_start, de_next_sub_end, txt = de_subs[latest_de_idx] en_next_sub_start = iterable[i + 1][0] sync_time = get_delta_at_time(en_sub_end, syncs) if de_next_sub_end + sync_time < en_sub_end or de_next_sub_end + sync_time < en_next_sub_start: + with open('fix.txt', 'a+') as f: + f.write("... {:03} {:>90} | {:<90}\n".format(latest_de_idx, '', txt)) latest_de_idx += 1 de_txt += txt - print("{:>6} {:>6} {:6.2f} {:6.2f} {:>90} {:>90}".format( + print("{:>6} {:>6} {:6.2f} {:6.2f} {:>90} {:<90}".format( '', '', de_next_sub_start, de_next_sub_end, - '', txt, + '', + txt, )) else: - de_last_end = de_subs[latest_de_idx - 1][1] break - if SHOULD_CUT_VIDEOS: - cut_videos(i, en_sub[0], en_sub[1], de_first_start, de_last_end, en_sub[2], de_txt) + with open('fix.txt', 'a+') as f: + f.write('{}\n'.format(SCENE_BREAK)) return len(iterable) +def process_scene(scene_idx, scene): + if len(scene['en']) == 0 or len(scene['de']) == 0: + return + en_start, en_end = scene['en'][0][0], scene['en'][-1][1] + de_start, de_end = scene['de'][0][0], scene['de'][-1][1] + en_txt = ' '.join([entry[2] for entry in scene['en']]) + de_txt = ' '.join([entry[2] for entry in scene['de']]) + cut_videos(scene_idx, en_start, en_end, de_start, de_end, en_txt, de_txt) + + +def do_cut_videos(): + de_subs = parse_subs('subs.de.vtt') + en_subs = parse_subs('subs.en.vtt') + scene_idx, scene = 0, {'en': [], 'de': []} + with open('fix.txt') as f: + for line in f.readlines(): + if len(line) < 7: + continue + line = line[:7] + if line == SCENE_BREAK: + process_scene(scene_idx, scene) + scene_idx, scene = scene_idx + 1, {'en': [], 'de': []} + continue + en_idx, de_idx = line.split() + if en_idx != EMPTY_IDX: + en_sub = en_subs[int(en_idx)] + scene['en'].append(en_sub) + if de_idx != EMPTY_IDX: + de_sub = de_subs[int(de_idx)] + scene['de'].append(de_sub) + return scene_idx + + def concat_all_videos(max_iters): with open('tmp/concat.txt', 'w+') as f: for idx in range(max_iters): @@ -195,11 +245,20 @@ def sync(): def main(): - download_vids_and_subs() - syncs = sync() - syncs = [(0, 0)] - max_iters = time_subs(syncs) - concat_all_videos(max_iters) + if SHOULD_DOWNLOAD: + download_vids_and_subs() + if SHOULD_SYNC: + syncs = sync() + else: + syncs = [(0, 0)] + if SHOULD_TIME: + max_iters = time_subs(syncs) + if SHOULD_FIX_CUTS: + exit() + if SHOULD_CUT: + max_iters = do_cut_videos() + if SHOULD_CONCAT: + concat_all_videos(max_iters) if __name__ == "__main__":