Skip to content

Commit

Permalink
Fixes for python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicer86 committed Dec 29, 2024
1 parent e8b9e7b commit ac5bd2a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
15 changes: 5 additions & 10 deletions tests/test_subtitles_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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])
Expand Down
9 changes: 3 additions & 6 deletions twotone/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 6 additions & 12 deletions twotone/tools/subtitles_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
Expand All @@ -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 = []
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions twotone/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit ac5bd2a

Please sign in to comment.