|
| 1 | +from comparevideosmodule import process_folder |
| 2 | +import os |
| 3 | +import time |
| 4 | +import argparse |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | + |
| 8 | +def process_folders(base_folder, mode, threshold): |
| 9 | + """ |
| 10 | + Processes folders based on the selected mode. |
| 11 | +
|
| 12 | + Args: |
| 13 | + base_folder (str): Base folder to process. |
| 14 | + mode (str): "check_extras_folder", "check_movie_folder", or "check_folder". |
| 15 | + threshold (float): The percentage threshold for considering partial matches. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + defaultdict: Matches grouped by folder. |
| 19 | + """ |
| 20 | + all_matches = defaultdict(list) |
| 21 | + |
| 22 | + # For "check_extras_folder" and "check_movie_folder", subfolders are identical |
| 23 | + subfolders = [ |
| 24 | + os.path.join(base_folder, subfolder) |
| 25 | + for subfolder in os.listdir(base_folder) |
| 26 | + if os.path.isdir(os.path.join(base_folder, subfolder)) |
| 27 | + ] |
| 28 | + |
| 29 | + # For "check_folder", the subfolders list will just contain the base_folder |
| 30 | + if mode == "check_folder": |
| 31 | + subfolders = [base_folder] |
| 32 | + |
| 33 | + total_folders = len(subfolders) |
| 34 | + |
| 35 | + for index, folder_path in enumerate(subfolders, start=1): |
| 36 | + print(f"\n\nProcessing folder: {folder_path} (Folder {index} of {total_folders})\n") |
| 37 | + |
| 38 | + if mode == "check_extras_folder": |
| 39 | + # Process "Extras" folder inside each movie folder |
| 40 | + extras_folder = os.path.join(folder_path, "Extras") |
| 41 | + if os.path.exists(extras_folder): |
| 42 | + matches = process_folder(extras_folder, mode, threshold) |
| 43 | + if matches: |
| 44 | + all_matches[extras_folder].extend(matches) |
| 45 | + elif mode == "check_movie_folder" or mode == "check_folder": |
| 46 | + # Process all video files in the given folder (and subfolders for check_movie_folder) |
| 47 | + matches = process_folder(folder_path, mode, threshold) |
| 48 | + if matches: |
| 49 | + all_matches[folder_path].extend(matches) |
| 50 | + |
| 51 | + return all_matches |
| 52 | + |
| 53 | + |
| 54 | +def validate_threshold(value): |
| 55 | + """ |
| 56 | + Validates that the provided threshold is a float between 0 and 100. |
| 57 | + """ |
| 58 | + try: |
| 59 | + f_value = float(value) |
| 60 | + except ValueError: |
| 61 | + raise argparse.ArgumentTypeError(f"{value} is not a valid float.") |
| 62 | + if not (0 <= f_value <= 100): |
| 63 | + raise argparse.ArgumentTypeError(f"{value} is out of range. Must be between 0 and 100.") |
| 64 | + return f_value |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + parser = argparse.ArgumentParser( |
| 69 | + description="Process video folders for duplicate detection and comparison.", |
| 70 | + formatter_class=argparse.RawTextHelpFormatter # Preserve formatting in help text |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "folder_path", |
| 74 | + type=str, |
| 75 | + help="Path to the folder to process." |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--mode", |
| 79 | + type=str, |
| 80 | + choices=["check_folder", "check_extras_folder", "check_movie_folder"], |
| 81 | + required=True, |
| 82 | + help=( |
| 83 | + "Specifies the mode of operation:\n\n" |
| 84 | + "1. check_folder:\n" |
| 85 | + " Compares all videos contained in this folder and all its subfolders for duplicates.\n\n" |
| 86 | + "2. check_extras_folder:\n" |
| 87 | + " Looks specifically for videos in 'Extras' subfolders within the specified folder structure.\n" |
| 88 | + " The program will scan each folder within the specified top-level directory and\n" |
| 89 | + " process any 'Extras' subfolders it finds.\n" |
| 90 | + " Example folder structure:\n\n" |
| 91 | + " Movies\n" |
| 92 | + " ├── Movie 1\n" |
| 93 | + " │ └── Extras\n" |
| 94 | + " ├── Movie 2\n" |
| 95 | + " │ └── Extras\n" |
| 96 | + " └── Movie 3\n" |
| 97 | + " └── Extras\n\n" |
| 98 | + "3. check_movie_folder:\n" |
| 99 | + " Compares all videos directly within movie folders, including their subfolders.\n" |
| 100 | + " The program will iterate through all subdirectories within the specified top-level\n" |
| 101 | + " directory and process videos in each folder and its subfolders.\n" |
| 102 | + " Example folder structure:\n\n" |
| 103 | + " Movies\n" |
| 104 | + " ├── Movie 1\n" |
| 105 | + " │ ├── Video1.mkv\n" |
| 106 | + " │ ├── Video2.mkv\n" |
| 107 | + " │ └── Subfolder\n" |
| 108 | + " ├── Movie 2\n" |
| 109 | + " │ └── Video3.mkv\n" |
| 110 | + " └── Movie 3\n" |
| 111 | + " ├── Video4.mkv\n" |
| 112 | + " └── Subfolder\n\n" |
| 113 | + ), |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--threshold", |
| 117 | + type=validate_threshold, |
| 118 | + default=95.0, |
| 119 | + help=( |
| 120 | + "Optional: Percentage threshold for considering partial matches (default: 95%%). " |
| 121 | + "The program checks if the first 95%% or the last 95%% of the video file matches with another file." |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + start = time.time() |
| 128 | + |
| 129 | + all_matches = process_folders(args.folder_path, args.mode, args.threshold) |
| 130 | + |
| 131 | + if not all_matches: |
| 132 | + print("\n\nNo duplicates or matches found in any folder.\n") |
| 133 | + else: |
| 134 | + print("\n\nCombined Matches:") |
| 135 | + print("-------------------------\n") |
| 136 | + for folder, matches in all_matches.items(): |
| 137 | + print(f"Matches found in {folder}:") |
| 138 | + for small, large in matches: |
| 139 | + print(f"- {small} is part of or matches {large} by more than {args.threshold}%.") |
| 140 | + print() |
| 141 | + |
| 142 | + end = time.time() |
| 143 | + print(f"Time elapsed: {end - start}s.") |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments