MCPcopy
hub / github.com/nikopueringer/CorridorKey / organize_target

Function organize_target

clip_manager.py:1006–1063  ·  view source on GitHub ↗

Organizes a specific folder. 1. If loose video -> Rename to Input.ext (if safe). 2. If sequence -> Move to Input/. 3. Ensure AlphaHint and VideoMamaMaskHint folders exist.

(target_dir: str)

Source from the content-addressed store, hash-verified

1004
1005
1006def organize_target(target_dir: str) -> None:
1007 """
1008 Organizes a specific folder.
1009 1. If loose video -> Rename to Input.ext (if safe).
1010 2. If sequence -> Move to Input/.
1011 3. Ensure AlphaHint and VideoMamaMaskHint folders exist.
1012 """
1013 logger.info(f"Organizing Target: {target_dir}")
1014
1015 if not os.path.exists(target_dir):
1016 logger.error(f"Target directory not found: {target_dir}")
1017 return
1018
1019 # Check for loose video
1020 # Strategy: Find largest video file that ISN'T named Input.*
1021 candidates = [f for f in os.listdir(target_dir) if is_video_file(f)]
1022 candidates = [f for f in candidates if not os.path.splitext(f)[0].lower() == "input"]
1023
1024 if candidates and not os.path.exists(os.path.join(target_dir, "Input")):
1025 # If multiple, pick largest (heuristic for 'Main Plate')
1026 candidates.sort(key=lambda f: os.path.getsize(os.path.join(target_dir, f)), reverse=True)
1027 main_clip = candidates[0]
1028 ext = os.path.splitext(main_clip)[1]
1029
1030 try:
1031 shutil.move(os.path.join(target_dir, main_clip), os.path.join(target_dir, f"Input{ext}"))
1032 logger.info(f"Renamed '{main_clip}' to 'Input{ext}'")
1033 except Exception as e:
1034 logger.error(f"Failed to rename '{main_clip}': {e}")
1035
1036 # Check for Image Sequence (Flat)
1037 # Only if Input folder doesn't exist and Input video doesn't exist
1038 has_input_dir = os.path.isdir(os.path.join(target_dir, "Input"))
1039 has_input_video = any(
1040 is_video_file(f) and os.path.basename(f).lower().startswith("input") for f in os.listdir(target_dir)
1041 )
1042
1043 if not has_input_dir and not has_input_video:
1044 all_files = sorted(glob.glob(os.path.join(target_dir, "*")))
1045 image_files = [f for f in all_files if is_image_file(f)]
1046
1047 if len(image_files) > 0:
1048 try:
1049 input_subdir = os.path.join(target_dir, "Input")
1050 os.makedirs(input_subdir)
1051 for img in image_files:
1052 shutil.move(img, os.path.join(input_subdir, os.path.basename(img)))
1053 logger.info(
1054 f"Organized: Moved {len(image_files)} images in '{os.path.basename(target_dir)}' to 'Input/'"
1055 )
1056 except Exception as e:
1057 logger.error(f"Failed to organize sequence in '{target_dir}': {e}")
1058
1059 # Create Hints
1060 for hint in ["AlphaHint", "VideoMamaMaskHint"]:
1061 hint_path = os.path.join(target_dir, hint)
1062 if not os.path.exists(hint_path):
1063 os.makedirs(hint_path)

Calls 2

is_video_fileFunction · 0.70
is_image_fileFunction · 0.70