Skip to content

Commit

Permalink
Add function to parse and calculate crop parameters
Browse files Browse the repository at this point in the history
A new function 'parse_crop_params' has been added to parse the crop parameters. This function uses regex to parse the crop string and returns a tuple if it matches the pattern. The 'detect_crop_parameters' function is now also improved to calculate and return composite crop parameters. It now gathers all crop parameters, finds the maximum dimensions, and the minimum offsets, and composites the largest frame. If no crop parameters are found, it returns an empty string.
  • Loading branch information
cbusillo committed Jun 20, 2024
1 parent 4e62ad6 commit 16b3e49
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions bd_to_avp/modules/video.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import atexit
import re
from pathlib import Path

import ffmpeg
Expand Down Expand Up @@ -141,6 +142,13 @@ def combine_to_mv_hevc(
raise RuntimeError("Failed to combine stereo HEVC streams to MV-HEVC.")


def parse_crop_params(crop_string: str) -> tuple[int, int, int, int] | None:
match = re.match(r"(\d+):(\d+):(\d+):(\d+)", crop_string)
if match:
return tuple(map(int, match.groups())) # type: ignore
return None


def detect_crop_parameters(
video_path: Path,
start_seconds: int = 600,
Expand All @@ -149,6 +157,7 @@ def detect_crop_parameters(
print("Detecting crop parameters...")
if not config.crop_black_bars:
return ""

stream = ffmpeg.input(str(video_path), ss=start_seconds)
stream = ffmpeg.output(
stream,
Expand All @@ -166,12 +175,29 @@ def detect_crop_parameters(
print(e.stderr.decode("utf-8"))
raise

crop_param_lines = []
crop_params: list[tuple[int, int, int, int]] = []
for output_line in output:
if "crop=" in output_line:
crop_param_lines.append(output_line.split("crop=")[1].split(" ")[0])
crop_param = output_line.split("crop=")[1].split(" ")[0]
parsed_params = parse_crop_params(crop_param)
if parsed_params:
crop_params.append(parsed_params)

if not crop_params:
return ""

# Find the maximum dimensions
max_width = max(param[0] for param in crop_params)
max_height = max(param[1] for param in crop_params)

# Find the minimum x and y offsets
min_x = min(param[2] for param in crop_params)
min_y = min(param[3] for param in crop_params)

# Composite the largest frame
composite_crop = f"{max_width}:{max_height}:{min_x}:{min_y}"

return max(crop_param_lines, key=len, default="")
return composite_crop


def upscale_file(input_path: Path) -> None:
Expand Down

0 comments on commit 16b3e49

Please sign in to comment.