diff --git a/video_cleaner.py b/video_cleaner.py index 5643bda..5dbc38e 100644 --- a/video_cleaner.py +++ b/video_cleaner.py @@ -156,33 +156,28 @@ class VideoCleaner: print(f"Scanning {input_path}...") - while True: - all_videos = [] - # 每次循环都获取最新列表 - for root, dirs, files in os.walk(input_path): - if "processed" in dirs: dirs.remove("processed") - for file in files: - if file.lower().endswith(self.supported_extensions): - full_path = Path(root) / file - all_videos.append(full_path) + # 逐个目录递归处理,避免一次性加载所有文件到内存 + for root, dirs, files in os.walk(input_path): + if "processed" in dirs: + dirs.remove("processed") - if not all_videos: - print("No more files to process. Exiting.") - break + # 筛选当前目录下的视频文件 + current_dir_videos = [] + for file in files: + if file.lower().endswith(self.supported_extensions): + current_dir_videos.append(Path(root) / file) - # 按修改时间排序,取最旧的一个 - all_videos.sort(key=lambda x: os.path.getmtime(x)) + if not current_dir_videos: + continue + + # 对当前目录的文件按时间排序处理 + current_dir_videos.sort(key=lambda x: os.path.getmtime(x)) - processed_in_this_loop = False - for target_file in all_videos: - if self.process_video_file(target_file, processed_dir, input_path): - processed_in_this_loop = True - break # 处理了一个,重新获取列表 - - if not processed_in_this_loop: - # 如果列表里剩下的所有文件都还没到 30 天,那就退出 - print("All remaining files are newer than the age limit. Exiting.") - break + print(f"Processing directory: {root} ({len(current_dir_videos)} files)") + for target_file in current_dir_videos: + # 处理文件。如果 process_video_file 返回 False, + # 通常是因为文件太新(未到 30 天),则跳过当前文件 + self.process_video_file(target_file, processed_dir, input_path) if __name__ == "__main__": import argparse