Skip to content

Commit

Permalink
Refactor disc info extraction in disc.py
Browse files Browse the repository at this point in the history
Simplify title identification and MVC video information extraction by consolidating functions and refactoring logic to improve readability and efficiency.
  • Loading branch information
cbusillo committed Jul 1, 2024
1 parent 8e7965f commit e8bb28c
Showing 1 changed file with 23 additions and 43 deletions.
66 changes: 23 additions & 43 deletions bd_to_avp/modules/disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,55 +48,35 @@ def get_disc_and_mvc_video_info() -> DiscInfo:
if disc_name_match:
disc_info.name = sanitize_filename(disc_name_match.group(1))

mvc_video_matches = list(
re.finditer(
r"SINFO:\d+,1,19,0,\"(\d+x\d+)\".*?SINFO:\d+,1,21,0,\"(.*?)\"",
output,
re.DOTALL,
)
)

if not mvc_video_matches:
print("No MVC video found in disc info.")
raise ValueError("No MVC video found in disc info.")

first_match = mvc_video_matches[0]
disc_info.resolution = first_match.group(1)
disc_info.frame_rate = first_match.group(2)
if "/" in disc_info.frame_rate:
disc_info.frame_rate = disc_info.frame_rate.split(" ")[0]
disc_info.main_title_number = get_first_mvc_title(output)

mvc_titles = get_mvc_title_numbers_and_durations(output)
main_feature_index = get_longest_mvc_title(mvc_titles)

disc_info.main_title_number = main_feature_index
mvc_video_match = re.search(
rf"SINFO:{disc_info.main_title_number},1,19,0,\"(\d+x\d+)\".*?SINFO:{disc_info.main_title_number},1,21,0,\"(.*?)\"",
output,
re.DOTALL,
)
if mvc_video_match:
disc_info.resolution = mvc_video_match.group(1)
disc_info.frame_rate = mvc_video_match.group(2)
if "/" in disc_info.frame_rate:
disc_info.frame_rate = disc_info.frame_rate.split(" ")[0]
else:
print(f"Warning: Could not find resolution and frame rate for title {disc_info.main_title_number}")

return disc_info


def get_mvc_title_numbers_and_durations(output: str) -> dict:
mvc_titles = {}
def get_first_mvc_title(output: str) -> int:
current_title = None
for line in output.splitlines():
if "Mpeg4-MVC-3D" in line:
title_number_match = re.search(r"TINFO:(\d+),", line)
duration_match = re.search(r"TINFO:\d+,9,0,\"(\d+:\d+:\d+)\"", output)
if title_number_match and duration_match:
title_number = int(title_number_match.group(1))
duration = duration_match.group(1)
mvc_titles[title_number] = duration
return mvc_titles


def get_longest_mvc_title(mvc_titles: dict) -> int:
longest_duration = 0
longest_title = 0
for title, duration in mvc_titles.items():
h, m, s = map(int, duration.split(":"))
total_seconds = h * 3600 + m * 60 + s
if total_seconds > longest_duration:
longest_duration = total_seconds
longest_title = title
return longest_title
if line.startswith("TINFO:"):
title_match = re.search(r"TINFO:(\d+),", line)
if title_match:
current_title = int(title_match.group(1))
elif "MVC" in line and current_title is not None:
return current_title

return 0


def rip_disc_to_mkv(output_folder: Path, disc_info: DiscInfo, language_code: str) -> None:
Expand Down

0 comments on commit e8bb28c

Please sign in to comment.