Skip to content

Commit

Permalink
Refactor color depth detection to work on iso, mkv, and mts from the …
Browse files Browse the repository at this point in the history
…process function
  • Loading branch information
cbusillo committed Jul 1, 2024
1 parent 8345023 commit 0c23380
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
17 changes: 9 additions & 8 deletions bd_to_avp/modules/disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class DiscInfo:
@dataclass
class TitleInfo:
index: int
duration: int = 0 # in seconds
duration: int = 0
has_mvc: bool = False
resolution: str = ""
frame_rate: str = ""
resolution: str | None = None
frame_rate: str | None = None


def parse_makemkv_output(output: str) -> tuple[str, list[TitleInfo]]:
Expand Down Expand Up @@ -75,9 +75,10 @@ def get_disc_and_mvc_video_info() -> DiscInfo:
disc_info = DiscInfo(name=filename)

ffmpeg_probe_output = ffmpeg.probe(source)["streams"][0]
disc_info.resolution = f"{ffmpeg_probe_output.get('width', 1920)}x{ffmpeg_probe_output.get('height',1080)}"
disc_info.frame_rate = ffmpeg_probe_output.get("avg_frame_rate")
disc_info.color_depth = 10 if "10" in ffmpeg_probe_output.get("pix_fmt") else 8
if all(key in ffmpeg_probe_output for key in ["width", "height"]):
disc_info.resolution = f"{ffmpeg_probe_output.get('width')}x{ffmpeg_probe_output.get('height')}"
if "avg_frame_rate" in ffmpeg_probe_output:
disc_info.frame_rate = ffmpeg_probe_output.get("avg_frame_rate")
if ffmpeg_probe_output.get("field_order") != "progressive":
disc_info.is_interlaced = True
return disc_info
Expand All @@ -95,8 +96,8 @@ def get_disc_and_mvc_video_info() -> DiscInfo:

longest_mvc_title = max(mvc_titles, key=lambda x: x.duration)
disc_info.main_title_number = longest_mvc_title.index
disc_info.resolution = longest_mvc_title.resolution
disc_info.frame_rate = longest_mvc_title.frame_rate
disc_info.resolution = longest_mvc_title.resolution or disc_info.resolution
disc_info.frame_rate = longest_mvc_title.frame_rate or disc_info.frame_rate
if "/" in disc_info.frame_rate:
disc_info.frame_rate = disc_info.frame_rate.split(" ")[0]

Expand Down
2 changes: 2 additions & 0 deletions bd_to_avp/modules/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
create_mv_hevc_file,
detect_crop_parameters,
create_upscaled_file,
get_video_color_depth,
)


Expand Down Expand Up @@ -60,6 +61,7 @@ def process_each() -> None:
raise FileExistsError(f"Output file already exists for {disc_info.name}. Use --overwrite to replace.")

mkv_output_path = create_mkv_file(output_folder, disc_info, config.language_code)
disc_info.color_depth = get_video_color_depth(mkv_output_path)
crop_params = detect_crop_parameters(mkv_output_path)
audio_output_path, video_output_path = create_mvc_and_audio(disc_info.name, mkv_output_path, output_folder)
create_srt_from_mkv(mkv_output_path, output_folder)
Expand Down
10 changes: 4 additions & 6 deletions bd_to_avp/modules/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ def create_left_right_files(
) -> tuple[Path, Path]:
left_eye_output_path = output_folder / f"{disc_info.name}_left_movie.mov"
right_eye_output_path = output_folder / f"{disc_info.name}_right_movie.mov"
if color_depth := get_video_color_depth(mvc_video):
disc_info.color_depth = color_depth
if config.start_stage.value <= Stage.CREATE_LEFT_RIGHT_FILES.value:
split_mvc_to_stereo(
mvc_video,
Expand All @@ -234,18 +232,18 @@ def create_left_right_files(
return left_eye_output_path, right_eye_output_path


def get_video_color_depth(input_path: Path) -> int | None:
def get_video_color_depth(input_path: Path) -> int:
try:
probe = ffmpeg.probe(str(input_path), select_streams="v:0", show_entries="stream=pix_fmt")
streams = probe.get("streams", [])
if streams:
pix_fmt = streams[0].get("pix_fmt")
if "10le" in pix_fmt or "10be" in pix_fmt:
return 10
return None
return DiscInfo.color_depth
except ffmpeg.Error:
print(f"Error getting video color depth, using default of {DiscInfo().color_depth}")
return None
print(f"Error getting video color depth, using default of {DiscInfo.color_depth}")
return DiscInfo.color_depth


def create_mv_hevc_file(
Expand Down

0 comments on commit 0c23380

Please sign in to comment.