From ac5bd2af50f11de83aceb2965cfb3309b0217c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Walenciak?= Date: Sun, 29 Dec 2024 20:27:12 +0100 Subject: [PATCH] Fixes for python 3.10 --- tests/test_subtitles_fixer.py | 15 +++++---------- twotone/tools/merge.py | 9 +++------ twotone/tools/subtitles_fixer.py | 18 ++++++------------ twotone/tools/utils.py | 6 ++---- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/tests/test_subtitles_fixer.py b/tests/test_subtitles_fixer.py index 6b7bb70..5efbf64 100644 --- a/tests/test_subtitles_fixer.py +++ b/tests/test_subtitles_fixer.py @@ -47,8 +47,7 @@ def create_broken_video_with_too_long_last_subtitle(output_video_path: str, inpu file.write(f"1\n\n") file.write(f"2\n") - file.write(f"{utils.ms_to_time(1000) - } --> {utils.ms_to_time((length + 10) * 1000)}\n") + file.write(f"{utils.ms_to_time(1000)} --> {utils.ms_to_time((length + 10) * 1000)}\n") file.write(f"2\n") utils.generate_mkv(input_video, output_video_path, [ @@ -82,8 +81,7 @@ def setUp(self): def test_dry_run_is_respected(self): with TestDataWorkingDirectory() as td: output_video_path = f"{td.path}/test_video.mkv" - create_broken_video_with_scaled_subtitle_timings(output_video_path, f"{ - current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") + create_broken_video_with_scaled_subtitle_timings(output_video_path, f"{current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") hashes_before = hashes(td.path) twotone.execute(["subtitles_fix", td.path]) @@ -94,8 +92,7 @@ def test_dry_run_is_respected(self): def test_video_with_scaled_subtitle_timings_fixing(self): with TestDataWorkingDirectory() as td: output_video_path = f"{td.path}/test_video.mkv" - create_broken_video_with_scaled_subtitle_timings(output_video_path, f"{ - current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") + create_broken_video_with_scaled_subtitle_timings(output_video_path, f"{current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") hashes_before = hashes(td.path) twotone.execute(["-r", "subtitles_fix", td.path]) @@ -111,8 +108,7 @@ def test_video_with_scaled_subtitle_timings_fixing(self): def test_video_with_too_long_last_subtitle_fixing(self): with TestDataWorkingDirectory() as td: output_video_path = f"{td.path}/test_video.mkv" - create_broken_video_with_too_long_last_subtitle(output_video_path, f"{ - current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") + create_broken_video_with_too_long_last_subtitle(output_video_path, f"{current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") hashes_before = hashes(td.path) twotone.execute(["-r", "subtitles_fix", td.path]) @@ -128,8 +124,7 @@ def test_video_with_too_long_last_subtitle_fixing(self): def test_deal_with_incompatible_videos(self): with TestDataWorkingDirectory() as td: output_video_path = f"{td.path}/test_video.mkv" - create_broken_video_with_incompatible_subtitles(output_video_path, f"{ - current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") + create_broken_video_with_incompatible_subtitles(output_video_path, f"{current_path}/videos/sea-waves-crashing-on-beach-shore-4793288.mp4") hashes_before = hashes(td.path) twotone.execute(["-r", "subtitles_fix", td.path]) diff --git a/twotone/tools/merge.py b/twotone/tools/merge.py index 379385f..75f9518 100644 --- a/twotone/tools/merge.py +++ b/twotone/tools/merge.py @@ -150,8 +150,7 @@ def _convert_subtitle(self, video_fps: str, subtitle: utils.SubtitleFile) -> [ut utils.fix_subtitles_fps(input_file, output_file, fps) else: - raise RuntimeError(f"ffmpeg exited with unexpected error:\n{ - status.stderr.decode('utf-8')}") + raise RuntimeError(f"ffmpeg exited with unexpected error:\n{status.stderr.decode('utf-8')}") converted_subtitle = utils.SubtitleFile( output_file, subtitle.language, "utf-8") @@ -197,8 +196,7 @@ def _merge(self, input_video: str, subtitles: [utils.SubtitleFile]): prepared_subtitles = [] for subtitle in sorted_subtitles: - logging.debug(f"\tregister subtitle [{subtitle.language}]: { - subtitle.path}") + logging.debug(f"\tregister subtitle [{subtitle.language}]: {subtitle.path}") self._register_input(subtitle.path) # Subtitles are buggy sometimes, use ffmpeg to fix them. @@ -246,8 +244,7 @@ def _process_dir(self, path: str): # this is a cumbersome situation so just don't allow it unique_names = set(Path(video).stem for video in video_files) if len(unique_names) != len(video_files): - logging.warning(f"Two video files with the same name found in { - cd}. This is not supported, skipping whole directory.") + logging.warning(f"Two video files with the same name found in {cd}. This is not supported, skipping whole directory.") continue subtitles_finder = self._aggressive_subtitle_search if len( diff --git a/twotone/tools/subtitles_fixer.py b/twotone/tools/subtitles_fixer.py index e14d83f..a7b2796 100644 --- a/twotone/tools/subtitles_fixer.py +++ b/twotone/tools/subtitles_fixer.py @@ -21,8 +21,7 @@ def __init__(self, really_fix: bool): def _print_broken_videos(broken_videos_info: [(utils.VideoInfo, [int])]): logging.info(f"Found {len(broken_videos_info)} broken videos:") for broken_video in broken_videos_info: - logging.info(f"{len(broken_video[1])} broken subtitle(s) in { - broken_video[0].path} found") + logging.info(f"{len(broken_video[1])} broken subtitle(s) in {broken_video[0].path} found") def _no_resolver(self, video_track: utils.VideoTrack, content: str): logging.error("Cannot fix the file, no idea how to do it.") @@ -125,17 +124,14 @@ def _repair_videos(self, broken_videos_info: [(utils.VideoInfo, [int])]): if status: if self._do_fix: # remove all subtitles from video - logging.debug( - "Removing existing subtitles from file") + logging.debug("Removing existing subtitles from file") video_without_subtitles = video_file + ".nosubtitles.mkv" - utils.start_process( - "mkvmerge", ["-o", video_without_subtitles, "-S", video_file]) + utils.start_process("mkvmerge", ["-o", video_without_subtitles, "-S", video_file]) # add fixed subtitles to video logging.debug("Adding fixed subtitles to file") temporaryVideoPath = video_file + ".fixed.mkv" - utils.generate_mkv( - input_video=video_without_subtitles, output_path=temporaryVideoPath, subtitles=subtitles) + utils.generate_mkv(input_video=video_without_subtitles, output_path=temporaryVideoPath, subtitles=subtitles) # overwrite broken video with fixed one os.replace(temporaryVideoPath, video_file) @@ -158,8 +154,7 @@ def diff(a, b): video_length = video_info.video_tracks[0].length if video_length is None: - logging.warning( - f"File {video_file} has unknown lenght. Cannot proceed.") + logging.warning(f"File {video_file} has unknown lenght. Cannot proceed.") return None broken_subtitiles = [] @@ -168,8 +163,7 @@ def diff(a, b): subtitle = video_info.subtitles[i] if not subtitle.format == "subrip": - logging.warning(f"Cannot analyse subtitle #{i} of { - video_file}: unsupported format '{subtitle.format}'") + logging.warning(f"Cannot analyse subtitle #{i} of {video_file}: unsupported format '{subtitle.format}'") continue lenght = subtitle.length diff --git a/twotone/tools/utils.py b/twotone/tools/utils.py index 2fe1caf..c0df834 100644 --- a/twotone/tools/utils.py +++ b/twotone/tools/utils.py @@ -217,8 +217,7 @@ def get_video_full_info(path: str) -> str: process = start_process("ffprobe", args) if process.returncode != 0: - raise RuntimeError(f"ffprobe exited with unexpected error:\n{ - process.stderr.decode('utf-8')}") + raise RuntimeError(f"ffprobe exited with unexpected error:\n{process.stderr.decode('utf-8')}") output_lines = process.stdout output_json = json.loads(output_lines) @@ -310,8 +309,7 @@ def generate_mkv(input_video: str, output_path: str, subtitles: [SubtitleFile]): if result.returncode != 0: if os.path.exists(output_path): os.remove(output_path) - raise RuntimeError(f"{cmd} exited with unexpected error:\n{ - result.stderr.decode('utf-8')}") + raise RuntimeError(f"{cmd} exited with unexpected error:\n{result.stderr.decode('utf-8')}") if not os.path.exists(output_path): logging.error("Output file was not created")