From 1d73c869da62b3d1fc117357cc1e43716e30f6c2 Mon Sep 17 00:00:00 2001 From: Hannes Date: Fri, 20 Mar 2026 00:48:16 +0100 Subject: [PATCH] V 1.1 - tqdm progress --- main.py | 254 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 141 insertions(+), 113 deletions(-) diff --git a/main.py b/main.py index b688457..7dad5f6 100644 --- a/main.py +++ b/main.py @@ -33,134 +33,162 @@ def mux_audio_into_output(Quality_Video, audio_file, output_dir): return final_output def process_one(German_Video: Path, Quality_Video: Path, output_dir: Path, offset: Optional[int] = None) -> bool: + steps = [ + "Probe video", + "Compute offset", + "Extract pre beginning", + "Extract german audio", + "Extract post end", + "Combine audio", + "Mux", + "Cleanup", + "Final check", + ] - print("probe_video") - # --- Probe video --- - fps, duration, _, vstart = probe_video(German_Video) - fps2, duration2, _, vstart2 = probe_video(Quality_Video) + weights = [ + 1, + 3, + 1, + 2, + 1, + 2, + 2, + 1, + 2, + ] - if duration <= 0: - raise ValueError("Duration of German_Video must be greater than 0") - if duration2 <= 0: - raise ValueError("Duration of Quality_Video must be greater than 0") + with tqdm(total=len(steps), desc=Quality_Video.name, leave=False) as pbar: + for step, w in zip(steps, weights): + pbar.set_description(step) + if step == "Probe video": + # --- Probe video --- + fps, duration, _, vstart = probe_video(German_Video) + fps2, duration2, _, vstart2 = probe_video(Quality_Video) - print("Compute offset") - # --- Offset --- - if offset == None: - offset0 = corelate_videos(Quality_Video, German_Video, fps2) - else: - offset0 = offset - if offset0 > duration or offset0 > duration2: - raise ValueError("Offset needs to be smaller than the video durations") - print(f"Offset: {offset0}s") + if duration <= 0: + raise ValueError("Duration of German_Video must be greater than 0") + if duration2 <= 0: + raise ValueError("Duration of Quality_Video must be greater than 0") - print("Extract pre beginning") - german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka" - quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka" - quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka" + if step == "Compute offset": + # --- Offset --- + if offset == None: + offset0 = corelate_videos(Quality_Video, German_Video, fps2) + else: + offset0 = offset + if offset0 > duration or offset0 > duration2: + raise ValueError("Offset needs to be smaller than the video durations") + pbar.set_postfix_str(f"Offset: {offset0}s") - # Clear old faulty files, ger is gearantied to be replaced - if os.path.isfile(quality_audio_file1): - os.remove(quality_audio_file1) - if os.path.isfile(quality_audio_file2): - os.remove(quality_audio_file2) + if step == "Extract pre beginning": + german_audio_file = output_dir / f"{Path(Quality_Video).stem}_german_audio.mka" + quality_audio_file1 = output_dir / f"{Path(Quality_Video).stem}_original_audio1.mka" + quality_audio_file2 = output_dir / f"{Path(Quality_Video).stem}_original_audio2.mka" - ### Get the first part till german starts (if german does not start before file) - start2 = vstart2 - end2 = start2 + offset0 - if end2 - start2 > 0.05: - if DEBUG: print(f"Cutting audio ({quality_audio_file1}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s") - try: - extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file1) - except subprocess.CalledProcessError as e: - raise BrokenPipeError( - f"{e}\n" - f"Trying to extract Audio from {Quality_Video} {start2}-{end2} " - f"to {quality_audio_file2} to have a duration of {end2-start2}, " - f"while the audio has a length of {get_audio_duration(Quality_Video)}" - ) + # Clear old faulty files, ger is gearantied to be replaced + if os.path.isfile(quality_audio_file1): + os.remove(quality_audio_file1) + if os.path.isfile(quality_audio_file2): + os.remove(quality_audio_file2) - print("Extract german audio") + ### Get the first part till german starts (if german does not start before file) + start2 = vstart2 + end2 = start2 + offset0 + if end2 - start2 > 0.05: + if DEBUG: print(f"Cutting audio ({quality_audio_file1}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s") + try: + extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file1) + except subprocess.CalledProcessError as e: + raise BrokenPipeError( + f"{e}\n" + f"Trying to extract Audio from {Quality_Video} {start2}-{end2} " + f"to {quality_audio_file2} to have a duration of {end2-start2}, " + f"while the audio has a length of {get_audio_duration(Quality_Video)}" + ) - ### Get the German Audio - if offset0 < 0: - start = vstart - offset0 - else: - start = vstart - end = min(vstart + duration, start + duration2) - if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s") - try: - extract_audio_segment_file(German_Video, start, end, german_audio_file) - except subprocess.CalledProcessError as e: - raise BrokenPipeError( - f"{e}\n" - f"Trying to extract Audio from {German_Video} {start}-{end} " - f"to {german_audio_file} to have a duration of {end-start}, " - f"while the audio has a length of {get_audio_duration(German_Video)}" - ) + if step == "Extract german audio": - print("Extract post end") + ### Get the German Audio + if offset0 < 0: + start = vstart - offset0 + else: + start = vstart + end = min(vstart + duration, start + duration2) + if DEBUG: print(f"Cutting audio ({german_audio_file}) (from timestamp {start:.6f}s) to timestamp {end:.6f}s") + try: + extract_audio_segment_file(German_Video, start, end, german_audio_file) + except subprocess.CalledProcessError as e: + raise BrokenPipeError( + f"{e}\n" + f"Trying to extract Audio from {German_Video} {start}-{end} " + f"to {german_audio_file} to have a duration of {end-start}, " + f"while the audio has a length of {get_audio_duration(German_Video)}" + ) - ### Get the end from the first original audio if it is longer than the german part - start2 = vstart2 + end - # end2 = start2 + (duration2 - duration - (n1 / fps)) - end2 = vstart2 + get_audio_duration(Quality_Video) - if end2 - start2 > 0.5: - if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s") - try: - extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file2) - except subprocess.CalledProcessError as e: - raise BrokenPipeError( - f"{e}\n" - f"Trying to extract Audio from {Quality_Video} {start2}-{end2} " - f"to {quality_audio_file2} to have a duration of {end2-start2}, " - f"while the audio has a length of {get_audio_duration(Quality_Video)}" - ) + if step == "Extract post end": - print("Combine audio") + ### Get the end from the first original audio if it is longer than the german part + start2 = vstart2 + end + # end2 = start2 + (duration2 - duration - (n1 / fps)) + end2 = vstart2 + get_audio_duration(Quality_Video) + if end2 - start2 > 0.5: + if DEBUG: print(f"Cutting audio ({quality_audio_file2}) (from timestamp {start2:.6f}s) to timestamp {end2:.6f}s") + try: + extract_audio_segment_file(Quality_Video, start2, end2, quality_audio_file2) + except subprocess.CalledProcessError as e: + raise BrokenPipeError( + f"{e}\n" + f"Trying to extract Audio from {Quality_Video} {start2}-{end2} " + f"to {quality_audio_file2} to have a duration of {end2-start2}, " + f"while the audio has a length of {get_audio_duration(Quality_Video)}" + ) - # Combine the audio files - if os.path.isfile(quality_audio_file1): - stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file) - if os.path.isfile(quality_audio_file2): - stick_together_audio(german_audio_file, quality_audio_file2, german_audio_file) + if step == "Combine audio": - print("Mux") + # Combine the audio files + if os.path.isfile(quality_audio_file1): + stick_together_audio(quality_audio_file1, german_audio_file, german_audio_file) + if os.path.isfile(quality_audio_file2): + stick_together_audio(german_audio_file, quality_audio_file2, german_audio_file) - # --- Mux trimmed audio --- - final_file = mux_audio_into_output(Quality_Video, german_audio_file, output_dir) + if step == "Mux": - print("Cleanup") + # --- Mux trimmed audio --- + final_file = mux_audio_into_output(Quality_Video, german_audio_file, output_dir) - if os.path.isfile(quality_audio_file1): - os.remove(quality_audio_file1) - if os.path.isfile(quality_audio_file2): - os.remove(quality_audio_file2) + if step == "Cleanup": - print("Final check") - try: - files_acceptable, op_ed_offset, stream_num = check_intro_outro_sync(final_file) - except: - new_name = final_file.with_name(f"[NO CHAPTER]_{final_file.name}") - final_file.rename(new_name) - if os.path.isfile(german_audio_file): - os.remove(path=german_audio_file) - return False + if os.path.isfile(quality_audio_file1): + os.remove(quality_audio_file1) + if os.path.isfile(quality_audio_file2): + os.remove(quality_audio_file2) - if files_acceptable and isinstance(op_ed_offset, float) and isinstance(stream_num, int): - print(f"✔ {final_file.name} | offset={op_ed_offset:.4f}") - if np.abs(op_ed_offset) > (1/fps2) and os.path.isfile(german_audio_file): - replace_with_shifted_mka(final_file, german_audio_file, stream_num, op_ed_offset) - if os.path.isfile(german_audio_file): - os.remove(path=german_audio_file) - return True - else: - print(f"✖ FAILED: {final_file.name}") - if os.path.isfile(german_audio_file): - os.remove(path=german_audio_file) - new_name = final_file.with_name(f"[FAILED]_{final_file.name}") - final_file.rename(new_name) - return False + if step == "Final check": + try: + files_acceptable, op_ed_offset, stream_num = check_intro_outro_sync(final_file) + except: + new_name = final_file.with_name(f"[NO CHAPTER]_{final_file.name}") + final_file.rename(new_name) + if os.path.isfile(german_audio_file): + os.remove(path=german_audio_file) + return False + + if files_acceptable and isinstance(op_ed_offset, float) and isinstance(stream_num, int): + tqdm.write(f"✔ {final_file.name} | offset={op_ed_offset:.4f}") + if np.abs(op_ed_offset) > (1/fps2) and os.path.isfile(german_audio_file): + replace_with_shifted_mka(final_file, german_audio_file, stream_num, op_ed_offset) + if os.path.isfile(german_audio_file): + os.remove(path=german_audio_file) + return True + else: + tqdm.write(f"✖ FAILED: {final_file.name}") + if os.path.isfile(german_audio_file): + os.remove(path=german_audio_file) + new_name = final_file.with_name(f"[FAILED]_{final_file.name}") + final_file.rename(new_name) + return False + + pbar.update(w) def main(): @@ -171,7 +199,7 @@ def main(): episodes = range(start_num, episode_num + 1) - for i in episodes: + for i in tqdm(episodes, desc="Episodes"): german_pattern = f"Folder0/S01E{i:02}*.mkv" if os.path.isfile(german_pattern): German_Video = [german_pattern] @@ -192,9 +220,9 @@ def main(): if not process_one(Path(German_Video[0]), Path(Quality_Video[0]), Path(output_dir)): fault_catch.append([Path(German_Video[0]), Path(Quality_Video[0])]) if fault_catch: - print("\nFailed Videos:") + tqdm.write("\nFailed Videos:") for ger, orig in fault_catch: - print(f"{ger} + {orig}") + tqdm.write(f"{ger} + {orig}") if __name__ == '__main__': if len(sys.argv) == 3: