|
|
import os |
|
|
import shutil |
|
|
import torch |
|
|
import cv2 |
|
|
from pathlib import Path |
|
|
import logging |
|
|
from tqdm import tqdm |
|
|
import concurrent.futures |
|
|
from threading import Lock |
|
|
import time |
|
|
|
|
|
class FileChecker: |
|
|
def __init__(self, source_dir, corrupted_dir, max_workers=32): |
|
|
self.source_dir = Path(source_dir) |
|
|
self.corrupted_dir = Path(corrupted_dir) |
|
|
self.max_workers = max_workers |
|
|
self.lock = Lock() |
|
|
|
|
|
|
|
|
self.stats = { |
|
|
'total_pt': 0, |
|
|
'total_mp4': 0, |
|
|
'corrupted_pt': 0, |
|
|
'corrupted_mp4': 0, |
|
|
'moved_files': [], |
|
|
'failed_moves': [] |
|
|
} |
|
|
|
|
|
self.setup_logging() |
|
|
|
|
|
def setup_logging(self): |
|
|
"""设置日志记录""" |
|
|
logging.basicConfig( |
|
|
level=logging.INFO, |
|
|
format='%(asctime)s - %(levelname)s - %(message)s', |
|
|
handlers=[ |
|
|
logging.FileHandler('file_check.log'), |
|
|
logging.StreamHandler() |
|
|
] |
|
|
) |
|
|
self.logger = logging.getLogger(__name__) |
|
|
|
|
|
def check_pt_file(self, file_path): |
|
|
"""检查.pt文件是否损坏""" |
|
|
try: |
|
|
|
|
|
data = torch.load(file_path, map_location='cpu') |
|
|
|
|
|
if data is None: |
|
|
return False |
|
|
return True |
|
|
except Exception as e: |
|
|
return False |
|
|
|
|
|
def check_mp4_file(self, file_path): |
|
|
"""检查.mp4文件是否损坏""" |
|
|
try: |
|
|
|
|
|
cap = cv2.VideoCapture(str(file_path)) |
|
|
if not cap.isOpened(): |
|
|
return False |
|
|
|
|
|
|
|
|
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
|
fps = cap.get(cv2.CAP_PROP_FPS) |
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
|
|
|
|
|
|
if frame_count <= 0 or fps <= 0 or width <= 0 or height <= 0: |
|
|
cap.release() |
|
|
return False |
|
|
|
|
|
|
|
|
frames_to_check = min(3, frame_count) |
|
|
for i in range(frames_to_check): |
|
|
ret, frame = cap.read() |
|
|
if not ret or frame is None: |
|
|
cap.release() |
|
|
return False |
|
|
|
|
|
cap.release() |
|
|
return True |
|
|
except Exception as e: |
|
|
return False |
|
|
|
|
|
def move_corrupted_file(self, file_path, file_type): |
|
|
"""移动损坏的文件""" |
|
|
try: |
|
|
|
|
|
relative_path = file_path.relative_to(self.source_dir) |
|
|
new_path = self.corrupted_dir / relative_path |
|
|
new_path.parent.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
shutil.move(str(file_path), str(new_path)) |
|
|
|
|
|
with self.lock: |
|
|
self.stats['moved_files'].append(str(file_path)) |
|
|
if file_type == 'pt': |
|
|
self.stats['corrupted_pt'] += 1 |
|
|
else: |
|
|
self.stats['corrupted_mp4'] += 1 |
|
|
|
|
|
self.logger.info(f"已移动损坏文件: {file_path} -> {new_path}") |
|
|
return True |
|
|
|
|
|
except Exception as e: |
|
|
with self.lock: |
|
|
self.stats['failed_moves'].append(str(file_path)) |
|
|
self.logger.error(f"移动文件失败 {file_path}: {e}") |
|
|
return False |
|
|
|
|
|
def process_pt_file(self, file_path): |
|
|
"""处理单个.pt文件""" |
|
|
with self.lock: |
|
|
self.stats['total_pt'] += 1 |
|
|
|
|
|
if not self.check_pt_file(file_path): |
|
|
self.logger.warning(f"发现损坏的 .pt 文件: {file_path}") |
|
|
return self.move_corrupted_file(file_path, 'pt') |
|
|
return True |
|
|
|
|
|
def process_mp4_file(self, file_path): |
|
|
"""处理单个.mp4文件""" |
|
|
with self.lock: |
|
|
self.stats['total_mp4'] += 1 |
|
|
|
|
|
if not self.check_mp4_file(file_path): |
|
|
self.logger.warning(f"发现损坏的 .mp4 文件: {file_path}") |
|
|
return self.move_corrupted_file(file_path, 'mp4') |
|
|
return True |
|
|
|
|
|
def process_files(self): |
|
|
"""多线程处理文件""" |
|
|
|
|
|
self.corrupted_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
pt_files = list(self.source_dir.rglob('*.pt')) |
|
|
|
|
|
|
|
|
self.logger.info(f"找到 {len(pt_files)} 个 .pt 文件") |
|
|
|
|
|
self.logger.info(f"使用 {self.max_workers} 个线程进行处理") |
|
|
|
|
|
start_time = time.time() |
|
|
|
|
|
|
|
|
if pt_files: |
|
|
self.logger.info("开始多线程检查 .pt 文件...") |
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor: |
|
|
|
|
|
future_to_file = {executor.submit(self.process_pt_file, file_path): file_path |
|
|
for file_path in pt_files} |
|
|
|
|
|
|
|
|
for future in tqdm(concurrent.futures.as_completed(future_to_file), |
|
|
total=len(pt_files), desc="检查 .pt 文件"): |
|
|
file_path = future_to_file[future] |
|
|
try: |
|
|
future.result() |
|
|
except Exception as e: |
|
|
self.logger.error(f"处理文件 {file_path} 时出错: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_time = time.time() |
|
|
processing_time = end_time - start_time |
|
|
|
|
|
|
|
|
self.print_statistics(processing_time) |
|
|
|
|
|
return self.stats |
|
|
|
|
|
def print_statistics(self, processing_time): |
|
|
"""输出统计结果""" |
|
|
self.logger.info("=" * 60) |
|
|
self.logger.info("检查完成!统计结果:") |
|
|
self.logger.info(f"处理时间: {processing_time:.2f} 秒") |
|
|
self.logger.info(f"使用线程数: {self.max_workers}") |
|
|
self.logger.info(f"总 .pt 文件数: {self.stats['total_pt']}") |
|
|
self.logger.info(f"损坏 .pt 文件数: {self.stats['corrupted_pt']}") |
|
|
self.logger.info(f"总 .mp4 文件数: {self.stats['total_mp4']}") |
|
|
self.logger.info(f"损坏 .mp4 文件数: {self.stats['corrupted_mp4']}") |
|
|
self.logger.info(f"成功移动文件数: {len(self.stats['moved_files'])}") |
|
|
self.logger.info(f"移动失败文件数: {len(self.stats['failed_moves'])}") |
|
|
|
|
|
if self.stats['total_pt'] + self.stats['total_mp4'] > 0: |
|
|
total_files = self.stats['total_pt'] + self.stats['total_mp4'] |
|
|
files_per_second = total_files / processing_time |
|
|
self.logger.info(f"平均处理速度: {files_per_second:.2f} 文件/秒") |
|
|
|
|
|
self.logger.info("=" * 60) |
|
|
|
|
|
def main(): |
|
|
|
|
|
source_dir = "/mnt/bn/yufan-dev-my/ysh/Ckpts/Lixsp11/0_final_sekai_dataset" |
|
|
corrupted_dir = "/mnt/bn/yufan-dev-my/ysh/Datasets/corrupted_files" |
|
|
max_workers = 8 |
|
|
|
|
|
print(f"源目录: {source_dir}") |
|
|
print(f"损坏文件将移动到: {corrupted_dir}") |
|
|
print(f"并发线程数: {max_workers}") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
checker = FileChecker(source_dir, corrupted_dir, max_workers) |
|
|
stats = checker.process_files() |
|
|
|
|
|
|
|
|
if stats['moved_files']: |
|
|
with open('moved_files_list.txt', 'w') as f: |
|
|
for file_path in stats['moved_files']: |
|
|
f.write(f"{file_path}\n") |
|
|
print(f"已将移动的文件列表保存到 moved_files_list.txt") |
|
|
|
|
|
|
|
|
if stats['failed_moves']: |
|
|
with open('failed_moves_list.txt', 'w') as f: |
|
|
for file_path in stats['failed_moves']: |
|
|
f.write(f"{file_path}\n") |
|
|
print(f"已将移动失败的文件列表保存到 failed_moves_list.txt") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|